mizzkey/src/server/file/send-drive-file.ts

101 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-04-12 23:06:18 +02:00
import * as Koa from 'koa';
import * as send from 'koa-send';
import * as mongodb from 'mongodb';
import * as rename from 'rename';
import DriveFile, { getDriveFileBucket } from '../../models/drive-file';
import DriveFileThumbnail, { getDriveFileThumbnailBucket } from '../../models/drive-file-thumbnail';
import DriveFileWebpublic, { getDriveFileWebpublicBucket } from '../../models/drive-file-webpublic';
import { serverLogger } from '..';
2018-05-04 10:59:51 +02:00
const assets = `${__dirname}/../../server/file/assets/`;
2019-01-22 13:42:05 +01:00
const commonReadableHandlerGenerator = (ctx: Koa.BaseContext) => (e: Error): void => {
serverLogger.error(e);
ctx.status = 500;
};
2018-04-12 23:06:18 +02:00
2019-01-22 13:42:05 +01:00
export default async function(ctx: Koa.BaseContext) {
2018-04-12 23:06:18 +02:00
// Validate id
if (!mongodb.ObjectID.isValid(ctx.params.id)) {
ctx.throw(400, 'incorrect id');
return;
}
const fileId = new mongodb.ObjectID(ctx.params.id);
// Fetch drive file
const file = await DriveFile.findOne({ _id: fileId });
if (file == null) {
ctx.status = 404;
2019-01-22 13:42:05 +01:00
await send(ctx as any, '/dummy.png', { root: assets });
2018-04-12 23:06:18 +02:00
return;
}
2018-04-17 13:04:19 +02:00
if (file.metadata.deletedAt) {
ctx.status = 410;
2019-01-22 13:42:05 +01:00
await send(ctx as any, '/tombstone.png', { root: assets });
return;
}
2018-07-23 22:04:43 +02:00
if (file.metadata.withoutChunks) {
ctx.status = 204;
2018-04-17 13:04:19 +02:00
return;
}
2018-05-03 13:12:08 +02:00
const sendRaw = async () => {
if (file.metadata && file.metadata.accessKey && file.metadata.accessKey != ctx.query['original']) {
ctx.status = 403;
return;
}
2018-05-03 13:12:08 +02:00
const bucket = await getDriveFileBucket();
const readable = bucket.openDownloadStream(fileId);
readable.on('error', commonReadableHandlerGenerator(ctx));
ctx.set('Content-Type', file.contentType);
ctx.body = readable;
};
if ('thumbnail' in ctx.query) {
2018-08-16 00:17:04 +02:00
const thumb = await DriveFileThumbnail.findOne({
'metadata.originalId': fileId
});
if (thumb != null) {
ctx.set('Content-Type', 'image/jpeg');
ctx.set('Content-Disposition', `filename="${rename(file.filename, { suffix: '-thumb', extname: '.jpeg' })}"`);
2018-08-16 00:17:04 +02:00
const bucket = await getDriveFileThumbnailBucket();
ctx.body = bucket.openDownloadStream(thumb._id);
} else {
if (file.contentType.startsWith('image/')) {
ctx.set('Content-Disposition', `filename="${file.filename}"`);
await sendRaw();
} else {
ctx.status = 404;
await send(ctx as any, '/dummy.png', { root: assets });
}
}
} else if ('web' in ctx.query) {
const web = await DriveFileWebpublic.findOne({
'metadata.originalId': fileId
});
if (web != null) {
ctx.set('Content-Type', file.contentType);
ctx.set('Content-Disposition', `filename="${rename(file.filename, { suffix: '-web' })}"`);
const bucket = await getDriveFileWebpublicBucket();
ctx.body = bucket.openDownloadStream(web._id);
} else {
ctx.set('Content-Disposition', `filename="${file.filename}"`);
await sendRaw();
}
} else {
if ('download' in ctx.query) {
ctx.set('Content-Disposition', `attachment; filename="${file.filename}`);
}
2018-04-12 23:06:18 +02:00
2018-05-03 13:12:08 +02:00
await sendRaw();
}
2018-04-12 23:06:18 +02:00
}