hostr/api/routes/file.js

106 lines
2.4 KiB
JavaScript
Raw Permalink 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
2018-06-02 15:50:39 +00:00
export async function post(ctx, next) {
if (!ctx.request.is('multipart/*')) {
await next();
2016-06-06 13:39:42 +01:00
return;
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
const uploader = new Uploader(ctx);
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
await uploader.checkLimit();
2016-06-06 15:37:00 +01:00
2018-06-02 15:50:39 +00:00
await uploader.accept();
await uploader.processImage();
await uploader.finalise();
2016-06-06 15:37:00 +01:00
2018-06-02 15:50:39 +00:00
ctx.status = 201;
ctx.body = formatFile(uploader.file);
2016-06-06 13:39:42 +01:00
uploader.completeEvent();
uploader.malwareScan();
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function list(ctx) {
2015-07-09 23:01:43 +01:00
let limit = 20;
2018-06-02 15:50:39 +00:00
if (ctx.request.query.perpage === '0') {
2016-06-19 10:14:47 -07:00
limit = 1000;
2018-06-02 15:50:39 +00:00
} else if (ctx.request.query.perpage > 0) {
limit = parseInt(ctx.request.query.perpage / 1, 10);
2015-07-09 23:01:43 +01:00
}
2016-06-19 10:14:47 -07:00
let offset = 0;
2018-06-02 15:50:39 +00:00
if (ctx.request.query.page) {
offset = parseInt(ctx.request.query.page - 1, 10) * limit;
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
const files = await models.file.findAll({
2016-06-19 10:14:47 -07:00
where: {
2018-06-02 15:50:39 +00:00
userId: ctx.user.id,
2016-08-07 14:38:05 +01:00
processed: true,
2015-08-23 22:12:32 +01:00
},
2018-06-02 15:50:39 +00:00
order: [
2018-06-02 18:07:00 +00:00
['createdAt', 'DESC'],
],
2016-06-19 10:14:47 -07:00
offset,
limit,
});
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
ctx.statsd.incr('file.list', 1);
ctx.body = files.map(formatFile);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function get(ctx) {
const file = await models.file.findOne({
2016-06-19 10:14:47 -07:00
where: {
2018-06-02 15:50:39 +00:00
id: ctx.params.id,
2016-06-19 10:14:47 -07:00
},
});
2018-06-02 15:50:39 +00:00
ctx.assert(file, 404, '{"error": {"message": "File not found", "code": 604}}');
const user = await file.getUser();
ctx.assert(user && !user.banned, 404, '{"error": {"message": "File not found", "code": 604}}');
ctx.statsd.incr('file.get', 1);
ctx.body = formatFile(file);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function del(ctx) {
const file = await models.file.findOne({
2016-06-19 10:14:47 -07:00
where: {
2018-06-02 15:50:39 +00:00
id: ctx.params.id,
userId: ctx.user.id,
2016-06-19 10:14:47 -07:00
},
});
2018-06-02 15:50:39 +00:00
ctx.assert(file, 401, '{"error": {"message": "File not found", "code": 604}}');
await file.destroy();
const event = { type: 'file-deleted', data: { id: ctx.params.id } };
await ctx.redis.publish(`/file/${ctx.params.id}`, JSON.stringify(event));
await ctx.redis.publish(`/user/${ctx.user.id}`, JSON.stringify(event));
ctx.statsd.incr('file.delete', 1);
ctx.status = 204;
ctx.body = '';
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function events(ctx) {
2015-09-01 14:09:52 +02:00
const pubsub = redis.createClient(redisUrl);
2015-08-08 20:37:49 +01:00
pubsub.on('ready', () => {
2018-06-02 15:50:39 +00:00
pubsub.subscribe(ctx.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) => {
2018-06-02 15:50:39 +00:00
ctx.websocket.send(message);
2015-08-08 20:37:49 +01:00
});
2018-06-02 15:50:39 +00:00
ctx.websocket.on('close', () => {
2015-07-09 23:01:43 +01:00
pubsub.quit();
});
}