2022-02-27 02:07:39 +00:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import Koa from 'koa';
|
|
|
|
import { serverLogger } from '../index.js';
|
|
|
|
import { IImage, convertToPng, convertToJpeg } from '@/services/drive/image-processor.js';
|
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
|
|
|
import { downloadUrl } from '@/misc/download-url.js';
|
|
|
|
import { detectType } from '@/misc/get-file-info.js';
|
|
|
|
import { StatusError } from '@/misc/fetch.js';
|
|
|
|
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
|
2019-02-04 18:01:36 +00:00
|
|
|
|
2019-11-24 08:09:32 +00:00
|
|
|
export async function proxyMedia(ctx: Koa.Context) {
|
2019-02-04 18:01:36 +00:00
|
|
|
const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url;
|
|
|
|
|
2022-02-03 12:38:57 +00:00
|
|
|
if (typeof url !== 'string') {
|
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-04 18:01:36 +00:00
|
|
|
// Create temp file
|
2019-03-20 19:50:44 +00:00
|
|
|
const [path, cleanup] = await createTemp();
|
2019-02-04 18:01:36 +00:00
|
|
|
|
|
|
|
try {
|
2019-03-20 19:50:44 +00:00
|
|
|
await downloadUrl(url, path);
|
2019-02-04 18:01:36 +00:00
|
|
|
|
2020-01-12 07:40:58 +00:00
|
|
|
const { mime, ext } = await detectType(path);
|
2019-02-04 18:01:36 +00:00
|
|
|
|
|
|
|
let image: IImage;
|
|
|
|
|
2022-01-19 18:03:28 +00:00
|
|
|
if ('static' in ctx.query && ['image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp', 'image/svg+xml'].includes(mime)) {
|
2019-05-15 12:27:20 +00:00
|
|
|
image = await convertToPng(path, 498, 280);
|
2022-01-19 18:03:28 +00:00
|
|
|
} else if ('preview' in ctx.query && ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/svg+xml'].includes(mime)) {
|
2019-05-15 12:27:20 +00:00
|
|
|
image = await convertToJpeg(path, 200, 200);
|
2022-01-19 18:03:28 +00:00
|
|
|
} else if (['image/svg+xml'].includes(mime)) {
|
|
|
|
image = await convertToPng(path, 2048, 2048);
|
|
|
|
} else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) {
|
|
|
|
throw new StatusError('Rejected type', 403, 'Rejected type');
|
2019-02-04 18:01:36 +00:00
|
|
|
} else {
|
|
|
|
image = {
|
|
|
|
data: fs.readFileSync(path),
|
|
|
|
ext,
|
2020-01-12 07:40:58 +00:00
|
|
|
type: mime,
|
2019-02-04 18:01:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-12 13:21:47 +00:00
|
|
|
ctx.set('Content-Type', image.type);
|
2019-02-04 18:01:36 +00:00
|
|
|
ctx.set('Cache-Control', 'max-age=31536000, immutable');
|
|
|
|
ctx.body = image.data;
|
|
|
|
} catch (e) {
|
2021-10-16 08:16:24 +00:00
|
|
|
serverLogger.error(`${e}`);
|
2019-02-05 15:20:00 +00:00
|
|
|
|
2021-10-16 08:16:24 +00:00
|
|
|
if (e instanceof StatusError && e.isClientError) {
|
2021-09-03 12:00:44 +00:00
|
|
|
ctx.status = e.statusCode;
|
2019-02-05 15:20:00 +00:00
|
|
|
} else {
|
|
|
|
ctx.status = 500;
|
|
|
|
}
|
2019-02-04 18:01:36 +00:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|