FoundKey/src/server/proxy/proxy-media.ts
MeiMei 9703ba5340
ファイルと画像認識処理の改善 (#5690)
* dimensions制限とリファクタ

* comment

* 不要な変更削除

* use fromFile など

* Add probe-image-size.d.ts

* えーCRLFで作るなよ…

* Update src/@types/probe-image-size.d.ts

Co-Authored-By: Acid Chicken (硫酸鶏) <root@acid-chicken.com>

* fix d.ts

* Update src/@types/probe-image-size.d.ts

Co-Authored-By: Acid Chicken (硫酸鶏) <root@acid-chicken.com>

* Update src/@types/probe-image-size.d.ts

Co-Authored-By: Acid Chicken (硫酸鶏) <root@acid-chicken.com>

* fix

Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
2020-01-12 16:40:58 +09:00

51 lines
1.4 KiB
TypeScript

import * as fs from 'fs';
import * as Koa from 'koa';
import { serverLogger } from '..';
import { IImage, convertToPng, convertToJpeg } from '../../services/drive/image-processor';
import { createTemp } from '../../misc/create-temp';
import { downloadUrl } from '../../misc/donwload-url';
import { detectType } from '../../misc/get-file-info';
export async function proxyMedia(ctx: Koa.Context) {
const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url;
// Create temp file
const [path, cleanup] = await createTemp();
try {
await downloadUrl(url, path);
const { mime, ext } = await detectType(path);
if (!mime.startsWith('image/')) throw 403;
let image: IImage;
if ('static' in ctx.query && ['image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng'].includes(mime)) {
image = await convertToPng(path, 498, 280);
} else if ('preview' in ctx.query && ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng'].includes(mime)) {
image = await convertToJpeg(path, 200, 200);
} else {
image = {
data: fs.readFileSync(path),
ext,
type: mime,
};
}
ctx.set('Content-Type', image.type);
ctx.set('Cache-Control', 'max-age=31536000, immutable');
ctx.body = image.data;
} catch (e) {
serverLogger.error(e);
if (typeof e == 'number' && e >= 400 && e < 500) {
ctx.status = e;
} else {
ctx.status = 500;
}
} finally {
cleanup();
}
}