summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/MediaProxyServerService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-12-03 19:42:05 +0900
committerGitHub <noreply@github.com>2022-12-03 19:42:05 +0900
commit3a7182bfb5734599321fc03ea77c48b4dbc326d5 (patch)
treec96c46e0a9662809c40381d833e1ed1ca28de873 /packages/backend/src/server/MediaProxyServerService.ts
parentUpdate CHANGELOG.md (diff)
downloadsharkey-3a7182bfb5734599321fc03ea77c48b4dbc326d5.tar.gz
sharkey-3a7182bfb5734599321fc03ea77c48b4dbc326d5.tar.bz2
sharkey-3a7182bfb5734599321fc03ea77c48b4dbc326d5.zip
Fastify (#9106)
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * fix * Update SignupApiService.ts * wip * wip * Update ClientServerService.ts * wip * wip * wip * Update WellKnownServerService.ts * wip * wip * update des * wip * Update ApiServerService.ts * wip * update deps * Update WellKnownServerService.ts * wip * update deps * Update ApiCallService.ts * Update ApiCallService.ts * Update ApiServerService.ts
Diffstat (limited to 'packages/backend/src/server/MediaProxyServerService.ts')
-rw-r--r--packages/backend/src/server/MediaProxyServerService.ts51
1 files changed, 23 insertions, 28 deletions
diff --git a/packages/backend/src/server/MediaProxyServerService.ts b/packages/backend/src/server/MediaProxyServerService.ts
index 31841d39df..4d7bbdf599 100644
--- a/packages/backend/src/server/MediaProxyServerService.ts
+++ b/packages/backend/src/server/MediaProxyServerService.ts
@@ -1,8 +1,6 @@
import * as fs from 'node:fs';
import { Inject, Injectable } from '@nestjs/common';
-import Koa from 'koa';
-import cors from '@koa/cors';
-import Router from '@koa/router';
+import { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify';
import sharp from 'sharp';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
@@ -31,32 +29,29 @@ export class MediaProxyServerService {
private loggerService: LoggerService,
) {
this.logger = this.loggerService.getLogger('server', 'gray', false);
+
+ this.createServer = this.createServer.bind(this);
}
- public createServer() {
- const app = new Koa();
- app.use(cors());
- app.use(async (ctx, next) => {
- ctx.set('Content-Security-Policy', 'default-src \'none\'; img-src \'self\'; media-src \'self\'; style-src \'unsafe-inline\'');
- await next();
+ public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
+ fastify.addHook('onRequest', (request, reply, done) => {
+ reply.header('Content-Security-Policy', 'default-src \'none\'; img-src \'self\'; media-src \'self\'; style-src \'unsafe-inline\'');
+ done();
});
- // Init router
- const router = new Router();
-
- router.get('/:url*', ctx => this.handler(ctx));
-
- // Register router
- app.use(router.routes());
+ fastify.get<{
+ Params: { url: string; };
+ Querystring: { url?: string; };
+ }>('/:url*', async (request, reply) => await this.handler(request, reply));
- return app;
+ done();
}
- private async handler(ctx: Koa.Context) {
- const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url;
+ private async handler(request: FastifyRequest<{ Params: { url: string; }; Querystring: { url?: string; }; }>, reply: FastifyReply) {
+ const url = 'url' in request.query ? request.query.url : 'https://' + request.params.url;
if (typeof url !== 'string') {
- ctx.status = 400;
+ reply.code(400);
return;
}
@@ -71,11 +66,11 @@ export class MediaProxyServerService {
let image: IImage;
- if ('static' in ctx.query && isConvertibleImage) {
+ if ('static' in request.query && isConvertibleImage) {
image = await this.imageProcessingService.convertToWebp(path, 498, 280);
- } else if ('preview' in ctx.query && isConvertibleImage) {
+ } else if ('preview' in request.query && isConvertibleImage) {
image = await this.imageProcessingService.convertToWebp(path, 200, 200);
- } else if ('badge' in ctx.query) {
+ } else if ('badge' in request.query) {
if (!isConvertibleImage) {
// 画像でないなら404でお茶を濁す
throw new StatusError('Unexpected mime', 404);
@@ -122,16 +117,16 @@ export class MediaProxyServerService {
};
}
- ctx.set('Content-Type', image.type);
- ctx.set('Cache-Control', 'max-age=31536000, immutable');
- ctx.body = image.data;
+ reply.header('Content-Type', image.type);
+ reply.header('Cache-Control', 'max-age=31536000, immutable');
+ return image.data;
} catch (err) {
this.logger.error(`${err}`);
if (err instanceof StatusError && (err.statusCode === 302 || err.isClientError)) {
- ctx.status = err.statusCode;
+ reply.code(err.statusCode);
} else {
- ctx.status = 500;
+ reply.code(500);
}
} finally {
cleanup();