hostr/web/routes/file.js

119 lines
3 KiB
JavaScript
Raw Normal View History

2016-06-06 15:37:00 +01:00
import { join } from 'path';
2015-07-09 23:01:43 +01:00
import mime from 'mime-types';
2016-06-19 10:14:47 -07:00
import models from '../../models';
2015-07-09 23:01:43 +01:00
import hostrFileStream from '../../lib/hostr-file-stream';
import { formatFile } from '../../lib/format';
2015-08-30 18:35:05 +02:00
const storePath = process.env.UPLOAD_STORAGE_PATH;
2015-07-09 23:01:43 +01:00
2016-06-06 15:37:00 +01:00
const referrerRegexes = [
/^https:\/\/hostr.co/,
/^https:\/\/localhost.hostr.co/,
/^http:\/\/localhost:4040/,
];
2015-08-23 22:12:32 +01:00
function userAgentCheck(userAgent) {
if (!userAgent) {
2015-07-09 23:01:43 +01:00
return false;
}
return userAgent.match(/^(wget|curl|vagrant)/i);
2015-08-23 22:12:32 +01:00
}
2015-07-09 23:01:43 +01:00
2016-06-06 15:37:00 +01:00
function referrerCheck(referrer) {
2018-06-02 18:07:00 +00:00
return referrer && referrerRegexes.some(regex => referrer.match(regex));
2016-06-06 15:37:00 +01:00
}
2015-08-23 22:12:32 +01:00
function hotlinkCheck(file, userAgent, referrer) {
2016-06-06 15:37:00 +01:00
return userAgentCheck(userAgent) || file.width || referrerCheck(referrer);
2015-08-23 22:12:32 +01:00
}
2015-08-09 01:11:48 +01:00
2018-06-02 15:50:39 +00:00
export async function get(ctx) {
if (ctx.params.size && ['150', '970'].indexOf(ctx.params.size) < 0) {
ctx.throw(404);
2016-06-06 17:41:00 +01:00
return;
}
2018-06-02 15:50:39 +00:00
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,
name: ctx.params.name,
2016-06-19 10:14:47 -07:00
},
2016-06-06 15:37:00 +01:00
});
2018-06-02 15:50:39 +00:00
ctx.assert(file, 404);
2015-08-09 17:21:39 +01:00
2018-06-02 15:50:39 +00:00
if (!hotlinkCheck(file, ctx.headers['user-agent'], ctx.headers.referer)) {
ctx.redirect(`/${file.id}`);
2016-06-06 15:37:00 +01:00
return;
2015-08-09 17:21:39 +01:00
}
2018-06-02 15:50:39 +00:00
if (!file.width && ctx.request.query.warning !== 'on') {
ctx.redirect(`/${file.id}`);
2016-06-06 15:37:00 +01:00
return;
2015-08-09 01:11:48 +01:00
}
2015-08-09 17:21:39 +01:00
if (file.malware) {
2018-06-02 18:07:00 +00:00
const { alert } = ctx.request.query;
2015-08-09 17:21:39 +01:00
if (!alert || !alert.match(/i want to download malware/i)) {
2018-06-02 15:50:39 +00:00
ctx.redirect(`/${file.id}`);
2016-06-06 15:37:00 +01:00
return;
2015-08-09 17:21:39 +01:00
}
}
2016-06-19 10:14:47 -07:00
let localPath = join(storePath, file.id[0], `${file.id}_${file.name}`);
let remotePath = join(file.id[0], `${file.id}_${file.name}`);
2018-06-02 15:50:39 +00:00
if (ctx.params.size > 0) {
localPath = join(storePath, file.id[0], ctx.params.size, `${file.id}_${file.name}`);
remotePath = join(file.id[0], ctx.params.size, `${file.id}_${file.name}`);
2015-07-09 23:01:43 +01:00
}
2015-08-22 18:24:39 +01:00
2015-08-09 17:21:39 +01:00
if (file.malware) {
2018-06-02 15:50:39 +00:00
ctx.statsd.incr('file.malware.download', 1);
2015-08-09 17:21:39 +01:00
}
2015-07-09 23:01:43 +01:00
let type = 'application/octet-stream';
if (file.width > 0) {
2018-06-02 15:50:39 +00:00
if (ctx.params.size) {
ctx.statsd.incr('file.view', 1);
2015-08-09 17:21:39 +01:00
}
2016-06-19 10:14:47 -07:00
type = mime.lookup(file.name);
2015-08-09 17:21:39 +01:00
} else {
2018-06-02 15:50:39 +00:00
ctx.statsd.incr('file.download', 1);
2015-08-09 17:21:39 +01:00
}
2018-06-02 15:50:39 +00:00
if (userAgentCheck(ctx.headers['user-agent'])) {
ctx.set('Content-Disposition', `attachment; filename=${file.name}`);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
ctx.set('Content-type', type);
ctx.set('Expires', new Date(2020, 1).toISOString());
ctx.set('Cache-control', 'max-age=2592000');
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
if (!ctx.params.size || (ctx.params.size && ctx.params.size > 150)) {
2016-06-19 10:14:47 -07:00
models.file.accessed(file.id);
2015-08-22 18:48:34 +01:00
}
2015-08-22 18:40:23 +01:00
2018-06-02 15:50:39 +00:00
ctx.body = await hostrFileStream(localPath, remotePath);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function resized(ctx) {
await get.call(ctx);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function landing(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);
if (userAgentCheck(ctx.headers['user-agent'])) {
ctx.params.name = file.name;
await get.call(ctx);
2016-06-06 15:37:00 +01:00
return;
2015-07-09 23:01:43 +01:00
}
2015-08-22 18:24:39 +01:00
2018-06-02 15:50:39 +00:00
ctx.statsd.incr('file.landing', 1);
2015-07-09 23:01:43 +01:00
const formattedFile = formatFile(file);
2018-06-02 15:50:39 +00:00
await ctx.render('file', { file: formattedFile });
2015-07-09 23:01:43 +01:00
}