Migrate from koa-route to koa-router

This commit is contained in:
Jonathan Cremin 2015-08-22 16:16:15 +01:00
parent 8474cca94c
commit a4bf3dd51d
17 changed files with 206 additions and 291 deletions

View file

@ -20,39 +20,39 @@ const hotlinkCheck = function(file, userAgent, referrer) {
return !userAgentCheck(userAgent) && !file.width && (!referrer || !(referrer.match(/^https:\/\/hostr.co/) || referrer.match(/^http:\/\/localhost:4040/)))
};
export function* get(id, name, size) {
const file = yield this.db.Files.findOne({_id: id, 'file_name': name, 'status': 'active'});
export function* get() {
const file = yield this.db.Files.findOne({_id: this.params.id, 'file_name': this.params.name, 'status': 'active'});
this.assert(file, 404);
if (hotlinkCheck(file, this.headers['user-agent'], this.headers['referer'])) {
return this.redirect('/' + id);
return this.redirect('/' + file._id);
}
if (!file.width && this.request.query.warning != 'on') {
return this.redirect('/' + id);
return this.redirect('/' + file._id);
}
if (file.malware) {
let alert = this.request.query.alert;
if (!alert || !alert.match(/i want to download malware/i)) {
return this.redirect('/' + id);
return this.redirect('/' + file._id);
}
}
let localPath = path.join(storePath, file._id[0], file._id + '_' + file.file_name);
let remotePath = path.join(file._id[0], file._id + '_' + file.file_name);
if (size > 0) {
localPath = path.join(storePath, file._id[0], size, file._id + '_' + file.file_name);
remotePath = path.join(size, file._id + '_' + file.file_name);
if (this.params.size > 0) {
localPath = path.join(storePath, file._id[0], this.params.size, file._id + '_' + file.file_name);
remotePath = path.join(this.params.size, file._id + '_' + file.file_name);
}
console.log(localPath);
if (file.malware) {
this.statsd.incr('file.malware.download', 1);
}
let type = 'application/octet-stream';
if (file.width > 0) {
if (size) {
if (this.params.size) {
this.statsd.incr('file.view', 1);
}
type = mime.lookup(file.file_name);
@ -71,19 +71,18 @@ export function* get(id, name, size) {
this.body = yield hostrFileStream(localPath, remotePath);
}
export function* resized(size, id, name) {
yield get.call(this, id, name, size);
export function* resized() {
yield get.call(this);
}
export function* landing(id, next) {
if (id === 'config.js') {
return yield next;
}
const file = yield this.db.Files.findOne({_id: id});
export function* landing() {
const file = yield this.db.Files.findOne({_id: this.params.id, status: 'active'});
this.assert(file, 404);
if(userAgentCheck(this.headers['user-agent'])) {
return yield get.call(this, file._id, file.file_name);
this.params.name = file.file_name;
return yield get.call(this);
}
console.log('incr')
this.statsd.incr('file.landing', 1);
const formattedFile = formatFile(file);
yield this.render('file', {file: formattedFile});

View file

@ -6,7 +6,7 @@ export function* signin() {
}
this.statsd.incr('auth.attempt', 1);
const user = yield authenticate(this, this.request.body.email, this.request.body.password);
const user = yield authenticate.call(this, this.request.body.email, this.request.body.password);
if(!user) {
this.statsd.incr('auth.failure', 1);
return yield this.render('signin', {error: 'Invalid login details', csrf: this.csrf});
@ -14,7 +14,7 @@ export function* signin() {
return yield this.render('signin', {error: 'Your account hasn\'t been activated yet. Check your for an activation email.', csrf: this.csrf});
} else {
this.statsd.incr('auth.success', 1);
yield setupSession(this, user);
yield setupSession.call(this, user);
this.redirect('/');
}
}
@ -36,7 +36,7 @@ export function* signup() {
const email = this.request.body.email;
const password = this.request.body.password;
try {
yield signupUser(this, email, password, ip);
yield signupUser.call(this, email, password, ip);
} catch (e) {
return yield this.render('signup', {error: e.message, csrf: this.csrf});
}
@ -50,23 +50,23 @@ export function* forgot(token) {
const Users = this.db.Users;
if (this.request.body.email) {
var email = this.request.body.email;
yield sendResetToken(this, email);
yield sendResetToken.call(this, email);
this.statsd.incr('auth.reset.request', 1);
return yield this.render('forgot', {message: 'We\'ve sent an email with a link to reset your password. Be sure to check your spam folder if you it doesn\'t appear within a few minutes', token: null, csrf: this.csrf});
} else if (token && this.request.body.password) {
if (this.request.body.password.length < 7) {
return yield this.render('forgot', {error: 'Password needs to be at least 7 characters long.', token: token, csrf: this.csrf});
}
const tokenUser = yield validateResetToken(this, token);
const tokenUser = yield validateResetToken.call(this, token);
var userId = tokenUser._id;
yield updatePassword(this, userId, this.request.body.password);
yield updatePassword.call(this, userId, this.request.body.password);
yield Reset.remove({_id: userId});
const user = yield Users.findOne({_id: userId});
yield setupSession(this, user);
yield setupSession.call(this, user);
this.statsd.incr('auth.reset.success', 1);
this.redirect('/');
} else if (token.length) {
const tokenUser = yield validateResetToken(this, token);
const tokenUser = yield validateResetToken.call(this, token);
if (!tokenUser) {
this.statsd.incr('auth.reset.fail', 1);
return yield this.render('forgot', {error: 'Invalid password reset token. It might be expired, or has already been used.', token: null, csrf: this.csrf});
@ -88,7 +88,7 @@ export function* logout() {
export function* activate(code) {
if (yield activateUser(this, code)) {
if (yield activateUser.call(this, code)) {
this.statsd.incr('auth.activation', 1);
}
this.redirect('/');