summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/MediaProxyServerService.ts
blob: 5b76f15020befe2fb464e232ccd8e151bd421bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import { Inject, Injectable } from '@nestjs/common';
import sharp from 'sharp';
import fastifyStatic from '@fastify/static';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { isMimeImage } from '@/misc/is-mime-image.js';
import { createTemp } from '@/misc/create-temp.js';
import { DownloadService } from '@/core/DownloadService.js';
import { ImageProcessingService, webpDefault } from '@/core/ImageProcessingService.js';
import type { IImage } from '@/core/ImageProcessingService.js';
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
import { StatusError } from '@/misc/status-error.js';
import type Logger from '@/logger.js';
import { FileInfoService } from '@/core/FileInfoService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import type { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify';

const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);

const assets = `${_dirname}/../../src/server/assets/`;

@Injectable()
export class MediaProxyServerService {
	private logger: Logger;

	constructor(
		@Inject(DI.config)
		private config: Config,

		private fileInfoService: FileInfoService,
		private downloadService: DownloadService,
		private imageProcessingService: ImageProcessingService,
		private loggerService: LoggerService,
	) {
		this.logger = this.loggerService.getLogger('server', 'gray', false);

		//this.createServer = this.createServer.bind(this);
	}

	@bindThis
	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();
		});

		fastify.register(fastifyStatic, {
			root: _dirname,
			serve: false,
		});

		fastify.get<{
			Params: { url: string; };
			Querystring: { url?: string; };
		}>('/:url*', async (request, reply) => await this.handler(request, reply));

		done();
	}

	@bindThis
	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') {
			reply.code(400);
			return;
		}
	
		// Create temp file
		const [path, cleanup] = await createTemp();
	
		try {
			await this.downloadService.downloadUrl(url, path);
	
			const { mime, ext } = await this.fileInfoService.detectType(path);
			const isConvertibleImage = isMimeImage(mime, 'sharp-convertible-image');
			const isAnimationConvertibleImage = isMimeImage(mime, 'sharp-animation-convertible-image');
	
			let image: IImage;
			if ('emoji' in request.query && isConvertibleImage) {
				if (!isAnimationConvertibleImage && !('static' in request.query)) {
					image = {
						data: fs.readFileSync(path),
						ext,
						type: mime,
					};
				} else {
					const data = await sharp(path, { animated: !('static' in request.query) })
					.resize({
						height: 128,
						withoutEnlargement: true,
					})
					.webp(webpDefault)
					.toBuffer();

					image = {
						data,
						ext: 'webp',
						type: 'image/webp',
					};
				}
			} else if ('static' in request.query && isConvertibleImage) {
				image = await this.imageProcessingService.convertToWebp(path, 498, 280);
			} else if ('preview' in request.query && isConvertibleImage) {
				image = await this.imageProcessingService.convertToWebp(path, 200, 200);
			} else if ('badge' in request.query) {
				if (!isConvertibleImage) {
					// 画像でないなら404でお茶を濁す
					throw new StatusError('Unexpected mime', 404);
				}

				const mask = sharp(path)
					.resize(96, 96, {
						fit: 'inside',
						withoutEnlargement: false,
					})
					.greyscale()
					.normalise()
					.linear(1.75, -(128 * 1.75) + 128) // 1.75x contrast
					.flatten({ background: '#000' })
					.toColorspace('b-w');
	
				const stats = await mask.clone().stats();
	
				if (stats.entropy < 0.1) {
					// エントロピーがあまりない場合は404にする
					throw new StatusError('Skip to provide badge', 404);
				}
	
				const data = sharp({
					create: { width: 96, height: 96, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } },
				})
					.pipelineColorspace('b-w')
					.boolean(await mask.png().toBuffer(), 'eor');
	
				image = {
					data: await data.png().toBuffer(),
					ext: 'png',
					type: 'image/png',
				};
			} else if (mime === 'image/svg+xml') {
				image = await this.imageProcessingService.convertToWebp(path, 2048, 2048, webpDefault);
			} else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) {
				throw new StatusError('Rejected type', 403, 'Rejected type');
			} else {
				image = {
					data: fs.readFileSync(path),
					ext,
					type: mime,
				};
			}
	
			reply.header('Content-Type', image.type);
			reply.header('Cache-Control', 'max-age=31536000, immutable');
			return image.data;
		} catch (err) {
			this.logger.error(`${err}`);

			if ('fallback' in request.query) {
				return reply.sendFile('/dummy.png', assets);
			}
	
			if (err instanceof StatusError && (err.statusCode === 302 || err.isClientError)) {
				reply.code(err.statusCode);
			} else {
				reply.code(500);
			}
		} finally {
			cleanup();
		}
	}
}