2022-02-27 11:07:39 +09:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { dirname } from 'node:path';
|
|
|
|
import Koa from 'koa';
|
2022-03-12 15:13:11 +09:00
|
|
|
import send from 'koa-send';
|
2022-02-27 11:07:39 +09:00
|
|
|
import rename from 'rename';
|
2019-12-31 17:23:47 +09:00
|
|
|
import * as tmp from 'tmp';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { serverLogger } from '../index.js';
|
|
|
|
import { contentDisposition } from '@/misc/content-disposition.js';
|
|
|
|
import { DriveFiles } from '@/models/index.js';
|
|
|
|
import { InternalStorage } from '@/services/drive/internal-storage.js';
|
|
|
|
import { downloadUrl } from '@/misc/download-url.js';
|
|
|
|
import { detectType } from '@/misc/get-file-info.js';
|
2022-04-28 11:14:03 +09:00
|
|
|
import { convertToWebp, convertToJpeg, convertToPng } from '@/services/drive/image-processor.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { GenerateVideoThumbnail } from '@/services/drive/generate-video-thumbnail.js';
|
|
|
|
import { StatusError } from '@/misc/fetch.js';
|
|
|
|
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
|
2021-08-19 18:33:41 +09:00
|
|
|
|
2022-02-27 11:07:39 +09:00
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
2021-08-19 18:33:41 +09:00
|
|
|
const _dirname = dirname(_filename);
|
2018-05-03 20:03:14 +09:00
|
|
|
|
2021-08-19 18:33:41 +09:00
|
|
|
const assets = `${_dirname}/../../server/file/assets/`;
|
2018-05-04 17:59:51 +09:00
|
|
|
|
2019-11-24 17:09:32 +09:00
|
|
|
const commonReadableHandlerGenerator = (ctx: Koa.Context) => (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;
|
2019-12-20 01:39:59 +09:00
|
|
|
ctx.set('Cache-Control', 'max-age=300');
|
2018-05-03 20:03:14 +09:00
|
|
|
};
|
2018-04-13 06:06:18 +09:00
|
|
|
|
2022-01-01 22:25:30 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-11-24 17:09:32 +09:00
|
|
|
export default async function(ctx: Koa.Context) {
|
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-12-20 01:39:59 +09:00
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
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-12-31 17:23:47 +09:00
|
|
|
const isThumbnail = file.thumbnailAccessKey === key;
|
|
|
|
const isWebpublic = file.webpublicAccessKey === key;
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (!file.storedInternal) {
|
2019-12-31 17:23:47 +09: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 16:40:58 +09:00
|
|
|
const { mime, ext } = await detectType(path);
|
2019-12-31 17:23:47 +09:00
|
|
|
|
|
|
|
const convertFile = async () => {
|
|
|
|
if (isThumbnail) {
|
2022-04-28 11:14:03 +09:00
|
|
|
if (['image/jpeg', 'image/webp', 'image/png', 'image/svg+xml'].includes(mime)) {
|
|
|
|
return await convertToWebp(path, 498, 280);
|
2020-01-12 16:40:58 +09:00
|
|
|
} else if (mime.startsWith('video/')) {
|
2019-12-31 17:23:47 +09:00
|
|
|
return await GenerateVideoThumbnail(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 03:05:52 +09:00
|
|
|
if (isWebpublic) {
|
|
|
|
if (['image/svg+xml'].includes(mime)) {
|
|
|
|
return await convertToPng(path, 2048, 2048);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 17:23:47 +09:00
|
|
|
return {
|
|
|
|
data: fs.readFileSync(path),
|
|
|
|
ext,
|
2020-01-12 16:40:58 +09:00
|
|
|
type: mime,
|
2019-12-31 17:23:47 +09:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const image = await convertFile();
|
|
|
|
ctx.body = image.data;
|
2022-01-01 22:25:30 +09:00
|
|
|
ctx.set('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
|
2019-12-31 17:23:47 +09:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
|
|
|
} catch (e) {
|
2021-10-16 17:16:24 +09:00
|
|
|
serverLogger.error(`${e}`);
|
2019-12-31 17:23:47 +09:00
|
|
|
|
2021-10-16 17:16:24 +09:00
|
|
|
if (e instanceof StatusError && e.isClientError) {
|
2021-09-03 21:00:44 +09:00
|
|
|
ctx.status = e.statusCode;
|
2019-12-31 17:23:47 +09: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 20:19:14 +09:00
|
|
|
ctx.status = 204;
|
2019-12-20 01:39:59 +09:00
|
|
|
ctx.set('Cache-Control', 'max-age=86400');
|
2018-04-17 20:04:19 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-04 07:20:41 +09:00
|
|
|
if (isThumbnail || isWebpublic) {
|
2020-01-19 03:38:22 +09:00
|
|
|
const { mime, ext } = await detectType(InternalStorage.resolvePath(key));
|
2020-01-04 07:20:41 +09:00
|
|
|
const filename = rename(file.name, {
|
|
|
|
suffix: isThumbnail ? '-thumb' : '-web',
|
2021-12-09 23:58:30 +09:00
|
|
|
extname: ext ? `.${ext}` : undefined,
|
2020-01-04 07:20:41 +09:00
|
|
|
}).toString();
|
|
|
|
|
2019-12-20 01:39:59 +09:00
|
|
|
ctx.body = InternalStorage.read(key);
|
2022-01-01 22:25:30 +09:00
|
|
|
ctx.set('Content-Type', FILE_TYPE_BROWSERSAFE.includes(mime) ? mime : 'application/octet-stream');
|
2019-12-20 01:39:59 +09:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
2020-01-04 07:20:41 +09:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', filename));
|
2018-05-03 20:03:14 +09:00
|
|
|
} else {
|
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.body = readable;
|
2022-01-01 22:25:30 +09:00
|
|
|
ctx.set('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.type) ? file.type : 'application/octet-stream');
|
2019-12-20 01:39:59 +09:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
2020-01-04 07:20:41 +09:00
|
|
|
ctx.set('Content-Disposition', contentDisposition('inline', file.name));
|
2018-05-03 20:03:14 +09:00
|
|
|
}
|
2018-04-13 06:06:18 +09:00
|
|
|
}
|