2021-08-19 12:33:41 +03:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { dirname } from 'path';
|
2018-04-13 00:06:18 +03:00
|
|
|
import * as Koa from 'koa';
|
|
|
|
import * as send from 'koa-send';
|
2019-03-18 06:20:49 +02:00
|
|
|
import * as rename from 'rename';
|
2019-12-31 10:23:47 +02:00
|
|
|
import * as tmp from 'tmp';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { serverLogger } from '../index';
|
|
|
|
import { contentDisposition } from '@/misc/content-disposition';
|
|
|
|
import { DriveFiles } from '@/models/index';
|
|
|
|
import { InternalStorage } from '@/services/drive/internal-storage';
|
|
|
|
import { downloadUrl } from '@/misc/download-url';
|
|
|
|
import { detectType } from '@/misc/get-file-info';
|
|
|
|
import { convertToJpeg, convertToPngOrJpeg } from '@/services/drive/image-processor';
|
|
|
|
import { GenerateVideoThumbnail } from '@/services/drive/generate-video-thumbnail';
|
2021-08-19 12:33:41 +03:00
|
|
|
|
|
|
|
//const _filename = fileURLToPath(import.meta.url);
|
|
|
|
const _filename = __filename;
|
|
|
|
const _dirname = dirname(_filename);
|
2018-05-03 14:03:14 +03:00
|
|
|
|
2021-08-19 12:33:41 +03:00
|
|
|
const assets = `${_dirname}/../../server/file/assets/`;
|
2018-05-04 11:59:51 +03:00
|
|
|
|
2019-11-24 10:09:32 +02:00
|
|
|
const commonReadableHandlerGenerator = (ctx: Koa.Context) => (e: Error): void => {
|
2019-02-03 11:16:57 +02:00
|
|
|
serverLogger.error(e);
|
2018-05-03 14:03:14 +03:00
|
|
|
ctx.status = 500;
|
2019-12-19 18:39:59 +02:00
|
|
|
ctx.set('Cache-Control', 'max-age=300');
|
2018-05-03 14:03:14 +03:00
|
|
|
};
|
2018-04-13 00:06:18 +03:00
|
|
|
|
2019-11-24 10:09:32 +02:00
|
|
|
export default async function(ctx: Koa.Context) {
|
2019-04-07 15:50:36 +03:00
|
|
|
const key = ctx.params.key;
|
2018-04-13 00:06:18 +03:00
|
|
|
|
|
|
|
// Fetch drive file
|
2019-04-07 15:50:36 +03: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 00:06:18 +03:00
|
|
|
|
|
|
|
if (file == null) {
|
|
|
|
ctx.status = 404;
|
2019-12-19 18:39:59 +02:00
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
2019-01-22 14:42:05 +02:00
|
|
|
await send(ctx as any, '/dummy.png', { root: assets });
|
2018-04-13 00:06:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-31 10:23:47 +02:00
|
|
|
const isThumbnail = file.thumbnailAccessKey === key;
|
|
|
|
const isWebpublic = file.webpublicAccessKey === key;
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (!file.storedInternal) {
|
2019-12-31 10:23:47 +02: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);
|
|
|
|
|
2020-01-12 09:40:58 +02:00
|
|
|
const { mime, ext } = await detectType(path);
|
2019-12-31 10:23:47 +02:00
|
|
|
|
|
|
|
const convertFile = async () => {
|
|
|
|
if (isThumbnail) {
|
2020-01-12 09:40:58 +02:00
|
|
|
if (['image/jpeg', 'image/webp'].includes(mime)) {
|
2019-12-31 10:23:47 +02:00
|
|
|
return await convertToJpeg(path, 498, 280);
|
2020-01-12 09:40:58 +02:00
|
|
|
} else if (['image/png'].includes(mime)) {
|
2020-02-14 04:40:45 +02:00
|
|
|
return await convertToPngOrJpeg(path, 498, 280);
|
2020-01-12 09:40:58 +02:00
|
|
|
} else if (mime.startsWith('video/')) {
|
2019-12-31 10:23:47 +02:00
|
|
|
return await GenerateVideoThumbnail(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: fs.readFileSync(path),
|
|
|
|
ext,
|
2020-01-12 09:40:58 +02:00
|
|
|
type: mime,
|
2019-12-31 10:23:47 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const image = await convertFile();
|
|
|
|
ctx.body = image.data;
|
2020-01-01 19:45:05 +02:00
|
|
|
ctx.set('Content-Type', image.type);
|
2019-12-31 10:23:47 +02:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
|
|
|
} catch (e) {
|
2021-09-03 15:00:44 +03:00
|
|
|
serverLogger.error(e.statusCode);
|
2019-12-31 10:23:47 +02:00
|
|
|
|
2021-09-03 15:00:44 +03:00
|
|
|
if (typeof e.statusCode === 'number' && e.statusCode >= 400 && e.statusCode < 500) {
|
|
|
|
ctx.status = e.statusCode;
|
2019-12-31 10:23:47 +02:00
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
|
|
|
} else {
|
|
|
|
ctx.status = 500;
|
|
|
|
ctx.set('Cache-Control', 'max-age=300');
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 14:19:14 +03:00
|
|
|
ctx.status = 204;
|
2019-12-19 18:39:59 +02:00
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
2018-04-17 14:04:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-04 00:20:41 +02:00
|
|
|
if (isThumbnail || isWebpublic) {
|
2020-01-18 20:38:22 +02:00
|
|
|
const { mime, ext } = await detectType(InternalStorage.resolvePath(key));
|
2020-01-04 00:20:41 +02:00
|
|
|
const filename = rename(file.name, {
|
|
|
|
suffix: isThumbnail ? '-thumb' : '-web',
|
|
|
|
extname: ext ? `.${ext}` : undefined
|
|
|
|
}).toString();
|
|
|
|
|
2019-12-19 18:39:59 +02:00
|
|
|
ctx.body = InternalStorage.read(key);
|
2020-01-04 00:20:41 +02:00
|
|
|
ctx.set('Content-Type', mime);
|
2019-12-19 18:39:59 +02:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
2020-01-04 00:20:41 +02:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', filename));
|
2018-05-03 14:03:14 +03:00
|
|
|
} else {
|
2019-04-12 19:43:22 +03:00
|
|
|
const readable = InternalStorage.read(file.accessKey!);
|
2019-04-07 15:50:36 +03:00
|
|
|
readable.on('error', commonReadableHandlerGenerator(ctx));
|
|
|
|
ctx.body = readable;
|
2019-12-19 18:39:59 +02:00
|
|
|
ctx.set('Content-Type', file.type);
|
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
2020-01-04 00:20:41 +02:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', file.name));
|
2018-05-03 14:03:14 +03:00
|
|
|
}
|
2018-04-13 00:06:18 +03:00
|
|
|
}
|