This commit is contained in:
tamaina 2023-01-24 00:16:30 +00:00
parent 88b41f86a9
commit 495cb7d311
4 changed files with 323 additions and 292 deletions

View File

@ -2,7 +2,6 @@ import { Inject, Injectable } from '@nestjs/common';
import sharp from 'sharp';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { ReadableStream } from 'node:stream/web';
import { Readable } from 'node:stream';
export type IImage = {
@ -93,9 +92,8 @@ export class ImageProcessingService {
}
@bindThis
public convertToWebpFromWebReadable(readable: ReadableStream | null, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
if (readable == null) throw new Error('Input is null');
return this.convertSharpToWebpStreamObj(Readable.fromWeb(readable).pipe(sharp()), width, height, options);
public convertToWebpFromReadable(readable: Readable, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
return this.convertSharpToWebpStreamObj(readable.pipe(sharp()), width, height, options);
}
@bindThis

View File

@ -5,14 +5,14 @@ import { Inject, Injectable } from '@nestjs/common';
import fastifyStatic from '@fastify/static';
import rename from 'rename';
import type { Config } from '@/config.js';
import type { DriveFilesRepository } from '@/models/index.js';
import type { DriveFile, DriveFilesRepository } from '@/models/index.js';
import { DI } from '@/di-symbols.js';
import { createTemp } from '@/misc/create-temp.js';
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
import { StatusError } from '@/misc/status-error.js';
import type Logger from '@/logger.js';
import { DownloadService, NonNullBodyResponse } from '@/core/DownloadService.js';
import { ImageProcessingService, webpDefault } from '@/core/ImageProcessingService.js';
import { IImageStreamable, ImageProcessingService, webpDefault } from '@/core/ImageProcessingService.js';
import { VideoProcessingService } from '@/core/VideoProcessingService.js';
import { InternalStorageService } from '@/core/InternalStorageService.js';
import { contentDisposition } from '@/misc/content-disposition.js';
@ -20,7 +20,9 @@ import { FileInfoService, TYPE_SVG } from '@/core/FileInfoService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify';
import { Readable } from 'node:stream';
import { Readable, pipeline } from 'node:stream';
import { isMimeImage } from '@/misc/is-mime-image';
import sharp from 'sharp';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
@ -58,7 +60,7 @@ export class FileServerService {
reply.header('Cache-Control', 'max-age=300');
};
}
@bindThis
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
fastify.addHook('onRequest', (request, reply, done) => {
@ -78,16 +80,300 @@ export class FileServerService {
return reply.send(file);
});
fastify.get<{ Params: { key: string; } }>('/:key', async (request, reply) => await this.sendDriveFile(request, reply));
fastify.get<{ Params: { key: string; } }>('/:key/*', async (request, reply) => await this.sendDriveFile(request, reply));
fastify.get<{ Params: { key: string; } }>('/files/:key', async (request, reply) => {
await this.sendDriveFile(request, reply).catch(err => this.errorHandler(request, reply, err));
});
fastify.get<{ Params: { key: string; } }>('/files/:key/*', async (request, reply) => {
await this.sendDriveFile(request, reply).catch(err => this.errorHandler(request, reply, err));
});
fastify.get<{
Params: { url: string; };
Querystring: { url?: string; };
}>('/proxy/:url*', async (request, reply) => {
await this.proxyHandler(request, reply).catch(err => this.errorHandler(request, reply, err));
});
done();
}
@bindThis
private async errorHandler(request: FastifyRequest<{ Params?: { [x: string]: any }; Querystring?: { [x: string]: any }; }>, reply: FastifyReply, err?: any) {
this.logger.error(`${err}`);
if (request.query && 'fallback' in request.query) {
return reply.sendFile('/dummy.png', assets);
}
if (err instanceof StatusError && (err.statusCode === 302 || err.isClientError)) {
reply.code(err.statusCode);
return;
}
reply.code(500);
return;
}
@bindThis
private async sendDriveFile(request: FastifyRequest<{ Params: { key: string; } }>, reply: FastifyReply) {
const key = request.params.key;
const file = await this.getFileFromKey(key).then();
if (file === '404') {
reply.code(404);
reply.header('Cache-Control', 'max-age=86400');
return reply.sendFile('/dummy.png', assets);
}
if (file === '204') {
reply.code(204);
reply.header('Cache-Control', 'max-age=86400');
return;
}
try {
if (file.state === 'remote') {
const convertFile = async () => {
if (file.fileRole === 'thumbnail') {
if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(file.mime)) {
return this.imageProcessingService.convertToWebpFromReadable(
file.stream,
498,
280
);
} else if (file.mime.startsWith('video/')) {
await file.fileSaving;
return await this.videoProcessingService.generateVideoThumbnail(file.path);
}
}
if (file.fileRole === 'webpublic') {
if (['image/svg+xml'].includes(file.mime)) {
return {
data: this.imageProcessingService.convertToWebpFromReadable(
file.stream,
2048,
2048,
{ ...webpDefault, lossless: true }
),
ext: 'webp',
type: 'image/webp',
};
}
}
return {
data: file.stream,
ext: file.ext,
type: file.mime,
};
};
const image = await convertFile();
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
reply.header('Cache-Control', 'max-age=31536000, immutable');
return image.data;
}
if (file.fileRole !== 'original') {
const filename = rename(file.file.name, {
suffix: file.fileRole === 'thumbnail' ? '-thumb' : '-web',
extname: file.ext ? `.${file.ext}` : undefined,
}).toString();
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.mime) ? file.mime : 'application/octet-stream');
reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition', contentDisposition('inline', filename));
return file.stream;
} else {
file.stream.on('error', this.commonReadableHandlerGenerator(reply));
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.file.type) ? file.file.type : 'application/octet-stream');
reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition', contentDisposition('inline', file.file.name));
return file.stream;
}
} finally {
if ('cleanup' in file) file.cleanup();
}
}
@bindThis
private async proxyHandler(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 file = await this.getStreamAndTypeFromUrl(url);
if (file === '404') {
reply.code(404);
reply.header('Cache-Control', 'max-age=86400');
return reply.sendFile('/dummy.png', assets);
}
if (file === '204') {
reply.code(204);
reply.header('Cache-Control', 'max-age=86400');
return;
}
try {
const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image');
const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image');
let image: IImageStreamable | null = null;
if ('emoji' in request.query && isConvertibleImage) {
if (!isAnimationConvertibleImage && !('static' in request.query)) {
image = {
data: file.stream,
ext: file.ext,
type: file.mime,
};
} else {
const data = pipeline(
file.stream,
sharp({ animated: !('static' in request.query) })
.resize({
height: 128,
withoutEnlargement: true,
})
.webp(webpDefault),
err => {
if (err) {
this.logger.error('Sharp pipeline error (emoji)', err);
throw new StatusError('Internal Error occured (in emoji pipeline, MediaProxy)', 500, 'Internal Error occured');
}
}
);
image = {
data,
ext: 'webp',
type: 'image/webp',
};
}
} else if ('static' in request.query && isConvertibleImage) {
image = this.imageProcessingService.convertToWebpFromReadable(file.stream, 498, 280);
} else if ('preview' in request.query && isConvertibleImage) {
image = this.imageProcessingService.convertToWebpFromReadable(file.stream, 200, 200);
} else if ('badge' in request.query) {
if (!isConvertibleImage) {
// 画像でないなら404でお茶を濁す
throw new StatusError('Unexpected mime', 404);
}
const path = await (async () => {
if (file.state === 'stored_internal') {
return file.path;
} else {
await file.fileSaving;
return file.path;
}
})();
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 (file.mime === 'image/svg+xml') {
image = this.imageProcessingService.convertToWebpFromReadable(file.stream, 2048, 2048);
} else if (!file.mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(file.mime)) {
throw new StatusError('Rejected type', 403, 'Rejected type');
}
if (!image) {
image = {
data: file.stream,
ext: file.ext,
type: file.mime,
};
}
reply.header('Content-Type', image.type);
reply.header('Cache-Control', 'max-age=31536000, immutable');
return image.data;
} finally {
if ('cleanup' in file) file.cleanup();
}
}
@bindThis
private async getStreamAndTypeFromUrl(url: string): Promise<
{ state: 'remote'; fileRole?: 'thumbnail' | 'webpublic' | 'original'; file?: DriveFile; stream: Readable; mime: string; ext: string | null; path: string; cleanup: () => void; fileSaving: Promise<any> }
| { state: 'stored_internal'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: DriveFile; stream: Readable; mime: string; ext: string | null; path: string; }
| '404'
| '204'
> {
if (url.startsWith(`${this.config.url}/files/`)) {
const key = url.replace(`${this.config.url}/files/`, '').split('/').shift();
if (!key) throw new StatusError('Invalid File Key', 400, 'Invalid File Key');
return await this.getFileFromKey(key);
}
return await this.downloadAndDetectTypeFromUrl(url);
}
@bindThis
private async downloadAndDetectTypeFromUrl(url: string): Promise<
{ state: 'remote' ; stream: Readable; mime: string; ext: string | null; path: string; cleanup: () => void; fileSaving: Promise<any> }
> {
const [path, cleanup] = await createTemp();
try {
const response = await this.downloadService.fetchUrl(url);
const fileSaving = this.downloadService.pipeRequestToFile(response, path);
const { mime, ext } = await this.fileInfoService.detectResponseType(response, path, fileSaving);
return {
state: 'remote',
stream: Readable.fromWeb(response.body),
mime, ext,
path, cleanup,
fileSaving,
}
} catch (e) {
cleanup();
throw e;
}
}
@bindThis
private async getFileFromKey(key: string): Promise<
{ state: 'remote'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: DriveFile; stream: Readable; mime: string; ext: string | null; path: string; cleanup: () => void; fileSaving: Promise<any> }
| { state: 'stored_internal'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: DriveFile; stream: Readable; mime: string; ext: string | null; path: string; }
| '404'
| '204'
> {
// Fetch drive file
const file = await this.driveFilesRepository.createQueryBuilder('file')
.where('file.accessKey = :accessKey', { accessKey: key })
@ -95,103 +381,43 @@ export class FileServerService {
.orWhere('file.webpublicAccessKey = :webpublicAccessKey', { webpublicAccessKey: key })
.getOne();
if (file == null) {
reply.code(404);
reply.header('Cache-Control', 'max-age=86400');
return reply.sendFile('/dummy.png', assets);
}
if (file == null) return '404';
const isThumbnail = file.thumbnailAccessKey === key;
const isWebpublic = file.webpublicAccessKey === key;
if (!file.storedInternal) {
if (file.isLink && file.uri) { // 期限切れリモートファイル
const [path, cleanup] = await createTemp();
try {
const response = await this.downloadService.fetchUrl(file.uri);
const fileSaving = this.downloadService.pipeRequestToFile(response, path);
const { mime, ext } = await this.fileInfoService.detectResponseType(response, path, fileSaving);
const convertFile = async () => {
if (isThumbnail) {
if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(mime)) {
return this.imageProcessingService.convertToWebpFromWebReadable(
response.body,
498,
280
);
} else if (mime.startsWith('video/')) {
await fileSaving;
return await this.videoProcessingService.generateVideoThumbnail(path);
}
}
if (isWebpublic) {
if (['image/svg+xml'].includes(mime)) {
return {
data: this.imageProcessingService.convertToWebpFromWebReadable(
response.body,
2048,
2048,
{ ...webpDefault, lossless: true }
),
ext: 'webp',
type: 'image/webp',
};
}
}
return {
data: Readable.fromWeb(response.body),
ext,
type: mime,
};
};
const image = await convertFile();
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
reply.header('Cache-Control', 'max-age=31536000, immutable');
return image.data;
} catch (err) {
this.logger.error(`${err}`);
if (err instanceof StatusError && err.isClientError) {
reply.code(err.statusCode);
reply.header('Cache-Control', 'max-age=86400');
} else {
reply.code(500);
reply.header('Cache-Control', 'max-age=300');
}
} finally {
cleanup();
}
return;
if (!(file.isLink && file.uri)) return '204';
const result = await this.downloadAndDetectTypeFromUrl(file.uri);
return {
...result,
fileRole: isThumbnail ? 'thumbnail' : isWebpublic ? 'webpublic' : 'original',
file,
}
reply.code(204);
reply.header('Cache-Control', 'max-age=86400');
return;
}
if (isThumbnail || isWebpublic) {
const { mime, ext } = await this.fileInfoService.detectType(this.internalStorageService.resolvePath(key));
const filename = rename(file.name, {
suffix: isThumbnail ? '-thumb' : '-web',
extname: ext ? `.${ext}` : undefined,
}).toString();
const path = this.internalStorageService.resolvePath(key);
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(mime) ? mime : 'application/octet-stream');
reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition', contentDisposition('inline', filename));
return this.internalStorageService.read(key);
} else {
const readable = this.internalStorageService.read(file.accessKey!);
readable.on('error', this.commonReadableHandlerGenerator(reply));
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.type) ? file.type : 'application/octet-stream');
reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition', contentDisposition('inline', file.name));
return readable;
if (isThumbnail || isWebpublic) {
const { mime, ext } = await this.fileInfoService.detectType(path);
return {
state: 'stored_internal',
fileRole: isThumbnail ? 'thumbnail' : 'webpublic',
file,
stream: this.internalStorageService.read(key),
mime, ext,
path,
};
}
return {
state: 'stored_internal',
fileRole: 'original',
file,
stream: this.internalStorageService.read(file.accessKey!),
mime: file.type,
ext: null,
path,
}
}
}

View File

@ -1,190 +0,0 @@
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, NonNullBodyResponse } from '@/core/DownloadService.js';
import { IImageStreamable, 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, TYPE_SVG } from '@/core/FileInfoService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import type { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify';
import { Readable, pipeline } from 'node:stream';
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 {
const response = await this.downloadService.fetchUrl(url);
const fileSaving = this.downloadService.pipeRequestToFile(response, path);
const { mime, ext } = await this.fileInfoService.detectResponseType(response, path, fileSaving);
const isConvertibleImage = isMimeImage(mime, 'sharp-convertible-image');
const isAnimationConvertibleImage = isMimeImage(mime, 'sharp-animation-convertible-image');
let image: IImageStreamable | null = null;
if ('emoji' in request.query && isConvertibleImage) {
if (!isAnimationConvertibleImage && !('static' in request.query)) {
image = {
data: Readable.fromWeb(response.body),
ext,
type: mime,
};
} else {
const data = pipeline(
Readable.fromWeb(response.body),
sharp({ animated: !('static' in request.query) })
.resize({
height: 128,
withoutEnlargement: true,
})
.webp(webpDefault),
err => {
if (err) {
this.logger.error('Sharp pipeline error (emoji)', err);
throw new StatusError('Internal Error occured (in emoji pipeline, MediaProxy)', 500, 'Internal Error occured');
}
}
);
image = {
data,
ext: 'webp',
type: 'image/webp',
};
}
} else if ('static' in request.query && isConvertibleImage) {
image = this.imageProcessingService.convertToWebpFromWebReadable(response.body, 498, 280);
} else if ('preview' in request.query && isConvertibleImage) {
image = this.imageProcessingService.convertToWebpFromWebReadable(response.body, 200, 200);
} else if ('badge' in request.query) {
if (!isConvertibleImage) {
// 画像でないなら404でお茶を濁す
throw new StatusError('Unexpected mime', 404);
}
await fileSaving;
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 = this.imageProcessingService.convertToWebpFromWebReadable(response.body, 2048, 2048);
} else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) {
throw new StatusError('Rejected type', 403, 'Rejected type');
}
if (!image) {
image = {
data: Readable.fromWeb(response.body),
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();
}
return; // Not all code paths return a value. 対策
}
}

View File

@ -20,7 +20,6 @@ import { NodeinfoServerService } from './NodeinfoServerService.js';
import { ApiServerService } from './api/ApiServerService.js';
import { StreamingApiServerService } from './api/StreamingApiServerService.js';
import { WellKnownServerService } from './WellKnownServerService.js';
import { MediaProxyServerService } from './MediaProxyServerService.js';
import { FileServerService } from './FileServerService.js';
import { ClientServerService } from './web/ClientServerService.js';
@ -48,7 +47,6 @@ export class ServerService {
private wellKnownServerService: WellKnownServerService,
private nodeinfoServerService: NodeinfoServerService,
private fileServerService: FileServerService,
private mediaProxyServerService: MediaProxyServerService,
private clientServerService: ClientServerService,
private globalEventService: GlobalEventService,
private loggerService: LoggerService,
@ -73,8 +71,7 @@ export class ServerService {
}
fastify.register(this.apiServerService.createServer, { prefix: '/api' });
fastify.register(this.fileServerService.createServer, { prefix: '/files' });
fastify.register(this.mediaProxyServerService.createServer, { prefix: '/proxy' });
fastify.register(this.fileServerService.createServer);
fastify.register(this.activityPubServerService.createServer);
fastify.register(this.nodeinfoServerService.createServer);
fastify.register(this.wellKnownServerService.createServer);