hostr/api/routes/file.js

125 lines
3 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
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.save();
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-06 13:39:42 +01:00
this.body = uploader.toJSON();
uploader.completeEvent();
uploader.malwareScan();
2015-07-09 23:01:43 +01:00
}
export function* list() {
const Files = this.db.Files;
let status = 'active';
if (this.request.query.trashed) {
status = 'trashed';
} else if (this.request.query.all) {
2016-06-06 13:39:42 +01:00
status = { $in: ['active', 'trashed'] };
2015-07-09 23:01:43 +01:00
}
let limit = 20;
if (this.request.query.perpage === '0') {
limit = false;
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
}
let skip = 0;
if (this.request.query.page) {
2015-08-23 22:12:32 +01:00
skip = parseInt(this.request.query.page - 1, 10) * limit;
2015-07-09 23:01:43 +01:00
}
const queryOptions = {
2016-06-06 13:39:42 +01:00
limit, skip, sort: [['time_added', 'desc']],
2015-07-09 23:01:43 +01:00
hint: {
2016-06-06 13:39:42 +01:00
owner: 1, status: 1, time_added: -1,
2015-08-23 22:12:32 +01:00
},
2015-07-09 23:01:43 +01:00
};
2016-06-06 13:39:42 +01:00
const userFiles = yield Files.find({
owner: this.user.id, status }, queryOptions).toArray();
2015-08-09 17:21:39 +01:00
this.statsd.incr('file.list', 1);
2015-07-09 23:01:43 +01:00
this.body = userFiles.map(formatFile);
}
2015-08-22 16:16:15 +01:00
export function* get() {
2015-07-09 23:01:43 +01:00
const Files = this.db.Files;
const Users = this.db.Users;
2016-06-06 13:39:42 +01:00
const file = yield Files.findOne({ _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-06 13:39:42 +01:00
const user = yield Users.findOne({ _id: file.owner });
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) {
const Files = this.db.Files;
const status = this.request.body.trashed ? 'trashed' : 'active';
2016-06-06 13:39:42 +01:00
yield Files.updateOne({ _id: this.params.id, owner: this.user.id },
{ $set: { status } }, { w: 1 });
2015-07-09 23:01:43 +01:00
}
}
2015-08-22 16:16:15 +01:00
export function* del() {
2016-06-06 13:39:42 +01:00
yield this.db.Files.updateOne({ _id: this.params.id, owner: this.db.objectId(this.user.id) },
{ $set: { status: 'deleted' } }, { w: 1 });
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();
});
}