hostr/api/routes/file.js

132 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-09-01 14:09:52 +02:00
import redis from 'redis';
2016-05-25 21:03:07 +01:00
2016-06-19 10:14:47 -07:00
import models from '../../models';
2015-07-09 23:01:43 +01:00
import { formatFile } from '../../lib/format';
2016-06-06 13:39:42 +01:00
import Uploader from '../../lib/uploader';
2015-07-09 23:01:43 +01:00
2015-08-30 18:35:05 +02:00
const redisUrl = process.env.REDIS_URL;
2015-07-09 23:01:43 +01:00
export function* post(next) {
if (!this.request.is('multipart/*')) {
2016-06-06 13:39:42 +01:00
yield next;
return;
2015-07-09 23:01:43 +01:00
}
2016-06-06 13:39:42 +01:00
const uploader = new Uploader(this);
2015-07-09 23:01:43 +01:00
2016-06-06 15:37:00 +01:00
yield uploader.checkLimit();
2016-06-06 13:39:42 +01:00
yield uploader.accept();
2016-06-06 15:37:00 +01:00
2016-06-06 13:39:42 +01:00
uploader.acceptedEvent();
2016-06-06 15:37:00 +01:00
2016-06-06 13:39:42 +01:00
uploader.receive();
2016-06-06 15:37:00 +01:00
2016-06-06 13:39:42 +01:00
yield uploader.promise;
2015-07-09 23:01:43 +01:00
2016-06-06 13:39:42 +01:00
uploader.processingEvent();
2015-07-09 23:01:43 +01:00
2016-06-06 13:39:42 +01:00
yield uploader.sendToSFTP();
yield uploader.processImage();
2016-05-25 21:03:07 +01:00
2016-06-06 13:39:42 +01:00
yield uploader.finalise();
2016-05-25 21:03:07 +01:00
2015-07-09 23:01:43 +01:00
this.status = 201;
2016-06-19 10:14:47 -07:00
this.body = formatFile(uploader.file);
2016-06-06 13:39:42 +01:00
uploader.completeEvent();
uploader.malwareScan();
2015-07-09 23:01:43 +01:00
}
export function* list() {
let limit = 20;
if (this.request.query.perpage === '0') {
2016-06-19 10:14:47 -07:00
limit = 1000;
2015-08-23 22:12:32 +01:00
} else if (this.request.query.perpage > 0) {
limit = parseInt(this.request.query.perpage / 1, 10);
2015-07-09 23:01:43 +01:00
}
2016-06-19 10:14:47 -07:00
let offset = 0;
2015-07-09 23:01:43 +01:00
if (this.request.query.page) {
2016-06-19 10:14:47 -07:00
offset = parseInt(this.request.query.page - 1, 10) * limit;
2015-07-09 23:01:43 +01:00
}
2016-06-19 10:14:47 -07:00
const files = yield models.file.findAll({
where: {
userId: this.user.id,
status: 'active',
2015-08-23 22:12:32 +01:00
},
2016-06-19 10:14:47 -07:00
order: '"createdAt" DESC',
offset,
limit,
});
2015-07-09 23:01:43 +01:00
2015-08-09 17:21:39 +01:00
this.statsd.incr('file.list', 1);
2016-06-19 10:14:47 -07:00
this.body = files.map(formatFile);
2015-07-09 23:01:43 +01:00
}
2015-08-22 16:16:15 +01:00
export function* get() {
2016-06-19 10:14:47 -07:00
const file = yield models.file.findOne({
where: {
id: this.params.id,
status: {
$in: ['active', 'uploading'],
},
},
});
2015-07-09 23:01:43 +01:00
this.assert(file, 404, '{"error": {"message": "File not found", "code": 604}}');
2016-06-19 10:14:47 -07:00
const user = yield file.getUser();
2015-07-09 23:01:43 +01:00
this.assert(user && !user.banned, 404, '{"error": {"message": "File not found", "code": 604}}');
2015-08-09 17:21:39 +01:00
this.statsd.incr('file.get', 1);
2015-07-09 23:01:43 +01:00
this.body = formatFile(file);
}
2015-08-22 16:16:15 +01:00
export function* put() {
2015-07-09 23:01:43 +01:00
if (this.request.body.trashed) {
2016-06-19 10:14:47 -07:00
const file = yield models.file.findOne({
where: {
id: this.params.id,
userId: this.user.id,
},
});
file.status = this.request.body.trashed ? 'trashed' : 'active';
yield file.save();
2015-07-09 23:01:43 +01:00
}
}
2015-08-22 16:16:15 +01:00
export function* del() {
2016-06-19 10:14:47 -07:00
const file = yield models.file.findOne({
where: {
id: this.params.id,
userId: this.user.id,
},
});
this.assert(file, 401, '{"error": {"message": "File not found", "code": 604}}');
file.status = 'deleted';
yield file.save();
2016-06-06 13:39:42 +01:00
const event = { type: 'file-deleted', data: { id: this.params.id } };
yield this.redis.publish(`/file/${this.params.id}`, JSON.stringify(event));
yield this.redis.publish(`/user/${this.user.id}`, JSON.stringify(event));
2015-08-09 17:21:39 +01:00
this.statsd.incr('file.delete', 1);
2015-08-22 16:16:15 +01:00
this.status = 204;
2015-07-09 23:01:43 +01:00
this.body = '';
}
export function* events() {
2015-09-01 14:09:52 +02:00
const pubsub = redis.createClient(redisUrl);
2015-08-08 20:37:49 +01:00
pubsub.on('ready', () => {
2015-07-09 23:01:43 +01:00
pubsub.subscribe(this.path);
2015-08-08 20:37:49 +01:00
});
2015-07-09 23:01:43 +01:00
2015-08-08 20:37:49 +01:00
pubsub.on('message', (channel, message) => {
2015-07-09 23:01:43 +01:00
this.websocket.send(message);
2015-08-08 20:37:49 +01:00
});
2015-08-23 22:12:32 +01:00
this.websocket.on('close', () => {
2015-07-09 23:01:43 +01:00
pubsub.quit();
});
}