2018-04-13 06:06:18 +09:00
|
|
|
import * as Koa from 'koa';
|
|
|
|
import * as send from 'koa-send';
|
2019-03-18 13:20:49 +09:00
|
|
|
import * as rename from 'rename';
|
2019-02-03 18:16:57 +09:00
|
|
|
import { serverLogger } from '..';
|
2019-03-18 15:23:45 +09:00
|
|
|
import { contentDisposition } from '../../misc/content-disposition';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { DriveFiles } from '../../models';
|
|
|
|
import { InternalStorage } from '../../services/drive/internal-storage';
|
2018-05-03 20:03:14 +09:00
|
|
|
|
2018-05-04 17:59:51 +09:00
|
|
|
const assets = `${__dirname}/../../server/file/assets/`;
|
|
|
|
|
2019-01-22 21:42:05 +09:00
|
|
|
const commonReadableHandlerGenerator = (ctx: Koa.BaseContext) => (e: Error): void => {
|
2019-02-03 18:16:57 +09:00
|
|
|
serverLogger.error(e);
|
2018-05-03 20:03:14 +09:00
|
|
|
ctx.status = 500;
|
|
|
|
};
|
2018-04-13 06:06:18 +09:00
|
|
|
|
2019-01-22 21:42:05 +09:00
|
|
|
export default async function(ctx: Koa.BaseContext) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const key = ctx.params.key;
|
2018-04-13 06:06:18 +09:00
|
|
|
|
|
|
|
// Fetch drive file
|
2019-04-07 21:50:36 +09:00
|
|
|
const file = await DriveFiles.createQueryBuilder('file')
|
|
|
|
.where('file.accessKey = :accessKey', { accessKey: key })
|
|
|
|
.orWhere('file.thumbnailAccessKey = :thumbnailAccessKey', { thumbnailAccessKey: key })
|
|
|
|
.orWhere('file.webpublicAccessKey = :webpublicAccessKey', { webpublicAccessKey: key })
|
|
|
|
.getOne();
|
2018-04-13 06:06:18 +09:00
|
|
|
|
|
|
|
if (file == null) {
|
|
|
|
ctx.status = 404;
|
2019-01-22 21:42:05 +09:00
|
|
|
await send(ctx as any, '/dummy.png', { root: assets });
|
2018-04-13 06:06:18 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (!file.storedInternal) {
|
2018-05-25 20:19:14 +09:00
|
|
|
ctx.status = 204;
|
2018-04-17 20:04:19 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const isThumbnail = file.thumbnailAccessKey === key;
|
|
|
|
const isWebpublic = file.webpublicAccessKey === key;
|
|
|
|
|
|
|
|
if (isThumbnail) {
|
|
|
|
ctx.set('Content-Type', 'image/jpeg');
|
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', `${rename(file.name, { suffix: '-thumb', extname: '.jpeg' })}`));
|
|
|
|
ctx.body = InternalStorage.read(key);
|
|
|
|
} else if (isWebpublic) {
|
2019-07-05 17:44:23 +09:00
|
|
|
ctx.set('Content-Type', file.type === 'image/apng' ? 'image/png' : file.type);
|
2019-04-07 21:50:36 +09:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', `${rename(file.name, { suffix: '-web' })}`));
|
|
|
|
ctx.body = InternalStorage.read(key);
|
2018-05-03 20:03:14 +09:00
|
|
|
} else {
|
|
|
|
if ('download' in ctx.query) {
|
2019-04-07 21:50:36 +09:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('attachment', `${file.name}`));
|
2018-05-03 20:03:14 +09:00
|
|
|
}
|
2018-04-13 06:06:18 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
const readable = InternalStorage.read(file.accessKey!);
|
2019-04-07 21:50:36 +09:00
|
|
|
readable.on('error', commonReadableHandlerGenerator(ctx));
|
|
|
|
ctx.set('Content-Type', file.type);
|
|
|
|
ctx.body = readable;
|
2018-05-03 20:03:14 +09:00
|
|
|
}
|
2018-04-13 06:06:18 +09:00
|
|
|
}
|