hostr/web/routes/file.js

111 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';
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) {
return referrer && referrerRegexes.some((regex) => referrer.match(regex));
}
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
2015-08-22 16:16:15 +01:00
export function* get() {
2016-06-06 15:37:00 +01:00
const file = yield this.db.Files.findOne({
_id: this.params.id,
file_name: this.params.name,
status: 'active',
});
2015-07-09 23:01:43 +01:00
this.assert(file, 404);
2015-08-09 17:21:39 +01:00
2016-06-06 15:37:00 +01:00
if (!hotlinkCheck(file, this.headers['user-agent'], this.headers.referer)) {
this.redirect(`/${file._id}`);
return;
2015-08-09 17:21:39 +01:00
}
2015-08-23 22:12:32 +01:00
if (!file.width && this.request.query.warning !== 'on') {
2016-06-06 15:37:00 +01:00
this.redirect(`/${file._id}`);
return;
2015-08-09 01:11:48 +01:00
}
2015-08-09 17:21:39 +01:00
if (file.malware) {
2015-08-23 22:12:32 +01:00
const alert = this.request.query.alert;
2015-08-09 17:21:39 +01:00
if (!alert || !alert.match(/i want to download malware/i)) {
2016-06-06 15:37:00 +01:00
this.redirect(`/${file._id}`);
return;
2015-08-09 17:21:39 +01:00
}
}
2016-06-06 15:37:00 +01:00
let localPath = join(storePath, file._id[0], `${file._id}_${file.file_name}`);
let remotePath = join(file._id[0], `${file._id}_${file.file_name}`);
2015-08-22 16:16:15 +01:00
if (this.params.size > 0) {
2016-06-06 15:37:00 +01:00
localPath = join(storePath, file._id[0], this.params.size, `${file._id}_${file.file_name}`);
remotePath = join(file._id[0], this.params.size, `${file._id}_${file.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) {
this.statsd.incr('file.malware.download', 1);
}
2015-07-09 23:01:43 +01:00
let type = 'application/octet-stream';
if (file.width > 0) {
2015-08-22 16:16:15 +01:00
if (this.params.size) {
2015-08-09 17:21:39 +01:00
this.statsd.incr('file.view', 1);
}
2015-07-09 23:01:43 +01:00
type = mime.lookup(file.file_name);
2015-08-09 17:21:39 +01:00
} else {
this.statsd.incr('file.download', 1);
}
if (userAgentCheck(this.headers['user-agent'])) {
2016-06-06 15:37:00 +01:00
this.set('Content-Disposition', `attachment; filename=${file.file_name}`);
2015-07-09 23:01:43 +01:00
}
this.set('Content-type', type);
this.set('Expires', new Date(2020, 1).toISOString());
this.set('Cache-control', 'max-age=2592000');
2015-08-22 18:48:34 +01:00
if (!this.params.size || (this.params.size && this.params.size > 150)) {
this.db.Files.updateOne(
2016-06-06 15:37:00 +01:00
{ _id: file._id },
{ $set: { last_accessed: Math.ceil(Date.now() / 1000) }, $inc: { downloads: 1 } },
{ w: 0 });
2015-08-22 18:48:34 +01:00
}
2015-08-22 18:40:23 +01:00
2015-07-09 23:01:43 +01:00
this.body = yield hostrFileStream(localPath, remotePath);
}
2015-08-22 16:16:15 +01:00
export function* resized() {
yield get.call(this);
2015-07-09 23:01:43 +01:00
}
2015-08-22 16:16:15 +01:00
export function* landing() {
2016-06-06 15:37:00 +01:00
const file = yield this.db.Files.findOne({ _id: this.params.id, status: 'active' });
2015-07-09 23:01:43 +01:00
this.assert(file, 404);
2015-08-23 22:12:32 +01:00
if (userAgentCheck(this.headers['user-agent'])) {
2015-08-22 16:16:15 +01:00
this.params.name = file.file_name;
2016-06-06 15:37:00 +01:00
yield get.call(this);
return;
2015-07-09 23:01:43 +01:00
}
2015-08-22 18:24:39 +01:00
2015-08-09 17:21:39 +01:00
this.statsd.incr('file.landing', 1);
2015-07-09 23:01:43 +01:00
const formattedFile = formatFile(file);
2016-06-06 15:37:00 +01:00
yield this.render('file', { file: formattedFile });
2015-07-09 23:01:43 +01:00
}