2018-04-12 23:06:18 +02:00
|
|
|
import * as Koa from 'koa';
|
|
|
|
import * as send from 'koa-send';
|
2019-03-18 05:20:49 +01:00
|
|
|
import * as rename from 'rename';
|
2019-12-31 09:23:47 +01:00
|
|
|
import * as tmp from 'tmp';
|
|
|
|
import * as fs from 'fs';
|
2019-02-03 10:16:57 +01:00
|
|
|
import { serverLogger } from '..';
|
2019-03-18 07:23:45 +01:00
|
|
|
import { contentDisposition } from '../../misc/content-disposition';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { DriveFiles } from '../../models';
|
|
|
|
import { InternalStorage } from '../../services/drive/internal-storage';
|
2019-12-31 09:23:47 +01:00
|
|
|
import { downloadUrl } from '../../misc/donwload-url';
|
|
|
|
import { detectMine } from '../../misc/detect-mine';
|
|
|
|
import { convertToJpeg, convertToPng, convertToGif, convertToApng } from '../../services/drive/image-processor';
|
|
|
|
import { GenerateVideoThumbnail } from '../../services/drive/generate-video-thumbnail';
|
2018-05-03 13:03:14 +02:00
|
|
|
|
2018-05-04 10:59:51 +02:00
|
|
|
const assets = `${__dirname}/../../server/file/assets/`;
|
|
|
|
|
2019-11-24 09:09:32 +01:00
|
|
|
const commonReadableHandlerGenerator = (ctx: Koa.Context) => (e: Error): void => {
|
2019-02-03 10:16:57 +01:00
|
|
|
serverLogger.error(e);
|
2018-05-03 13:03:14 +02:00
|
|
|
ctx.status = 500;
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.set('Cache-Control', 'max-age=300');
|
2018-05-03 13:03:14 +02:00
|
|
|
};
|
2018-04-12 23:06:18 +02:00
|
|
|
|
2019-11-24 09:09:32 +01:00
|
|
|
export default async function(ctx: Koa.Context) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const key = ctx.params.key;
|
2018-04-12 23:06:18 +02:00
|
|
|
|
|
|
|
// Fetch drive file
|
2019-04-07 14:50:36 +02: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-12 23:06:18 +02:00
|
|
|
|
|
|
|
if (file == null) {
|
|
|
|
ctx.status = 404;
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-31 09:23:47 +01:00
|
|
|
const isThumbnail = file.thumbnailAccessKey === key;
|
|
|
|
const isWebpublic = file.webpublicAccessKey === key;
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (!file.storedInternal) {
|
2019-12-31 09:23:47 +01:00
|
|
|
if (file.isLink && file.uri) { // 期限切れリモートファイル
|
|
|
|
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
|
|
|
|
tmp.file((e, path, fd, cleanup) => {
|
|
|
|
if (e) return rej(e);
|
|
|
|
res([path, cleanup]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await downloadUrl(file.uri, path);
|
|
|
|
|
|
|
|
const [type, ext] = await detectMine(path);
|
|
|
|
|
|
|
|
const convertFile = async () => {
|
|
|
|
if (isThumbnail) {
|
|
|
|
if (['image/jpeg', 'image/webp'].includes(type)) {
|
|
|
|
return await convertToJpeg(path, 498, 280);
|
|
|
|
} else if (['image/png'].includes(type)) {
|
|
|
|
return await convertToPng(path, 498, 280);
|
|
|
|
} else if (['image/gif'].includes(type)) {
|
|
|
|
return await convertToGif(path);
|
|
|
|
} else if (['image/apng', 'image/vnd.mozilla.apng'].includes(type)) {
|
|
|
|
return await convertToApng(path);
|
|
|
|
} else if (type.startsWith('video/')) {
|
|
|
|
return await GenerateVideoThumbnail(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: fs.readFileSync(path),
|
|
|
|
ext,
|
|
|
|
type,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const image = await convertFile();
|
|
|
|
ctx.body = image.data;
|
2020-01-01 18:45:05 +01:00
|
|
|
ctx.set('Content-Type', image.type);
|
2019-12-31 09:23:47 +01:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
|
|
|
} catch (e) {
|
|
|
|
serverLogger.error(e);
|
|
|
|
|
|
|
|
if (typeof e == 'number' && e >= 400 && e < 500) {
|
|
|
|
ctx.status = e;
|
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
|
|
|
} else {
|
|
|
|
ctx.status = 500;
|
|
|
|
ctx.set('Cache-Control', 'max-age=300');
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:19:14 +02:00
|
|
|
ctx.status = 204;
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
2018-04-17 13:04:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (isThumbnail) {
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.body = InternalStorage.read(key);
|
2019-04-07 14:50:36 +02:00
|
|
|
ctx.set('Content-Type', 'image/jpeg');
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
2019-04-07 14:50:36 +02:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', `${rename(file.name, { suffix: '-thumb', extname: '.jpeg' })}`));
|
|
|
|
} else if (isWebpublic) {
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.body = InternalStorage.read(key);
|
2019-07-05 10:44:23 +02:00
|
|
|
ctx.set('Content-Type', file.type === 'image/apng' ? 'image/png' : file.type);
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
2019-04-07 14:50:36 +02:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', `${rename(file.name, { suffix: '-web' })}`));
|
2018-05-03 13:03:14 +02:00
|
|
|
} else {
|
2019-04-12 18:43:22 +02:00
|
|
|
const readable = InternalStorage.read(file.accessKey!);
|
2019-04-07 14:50:36 +02:00
|
|
|
readable.on('error', commonReadableHandlerGenerator(ctx));
|
|
|
|
ctx.body = readable;
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.set('Content-Type', file.type);
|
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', `${file.name}`));
|
2018-05-03 13:03:14 +02:00
|
|
|
}
|
2018-04-12 23:06:18 +02:00
|
|
|
}
|