Sharkey/packages/backend/src/server/FileServerService.ts

184 lines
6 KiB
TypeScript
Raw Normal View History

2022-09-17 20:27:08 +02:00
import * as fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import { Inject, Injectable } from '@nestjs/common';
import Koa from 'koa';
import cors from '@koa/cors';
import Router from '@koa/router';
import send from 'koa-send';
import rename from 'rename';
2022-09-20 22:33:11 +02:00
import type { Config } from '@/config.js';
import type { DriveFilesRepository } from '@/models/index.js';
2022-09-17 20:27:08 +02:00
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';
2022-09-18 16:07:41 +02:00
import type Logger from '@/logger.js';
2022-09-17 20:27:08 +02:00
import { DownloadService } from '@/core/DownloadService.js';
import { ImageProcessingService } from '@/core/ImageProcessingService.js';
import { VideoProcessingService } from '@/core/VideoProcessingService.js';
import { InternalStorageService } from '@/core/InternalStorageService.js';
import { contentDisposition } from '@/misc/content-disposition.js';
import { FileInfoService } from '@/core/FileInfoService.js';
2022-09-18 16:07:41 +02:00
import { LoggerService } from '@/core/LoggerService.js';
2022-09-17 20:27:08 +02:00
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const assets = `${_dirname}/../../server/file/assets/`;
@Injectable()
export class FileServerService {
2022-09-18 20:11:50 +02:00
private logger: Logger;
2022-09-18 16:07:41 +02:00
2022-09-17 20:27:08 +02:00
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private fileInfoService: FileInfoService,
private downloadService: DownloadService,
private imageProcessingService: ImageProcessingService,
private videoProcessingService: VideoProcessingService,
private internalStorageService: InternalStorageService,
2022-09-18 16:07:41 +02:00
private loggerService: LoggerService,
2022-09-17 20:27:08 +02:00
) {
2022-09-18 20:11:50 +02:00
this.logger = this.loggerService.getLogger('server', 'gray', false);
2022-09-17 20:27:08 +02:00
}
2022-09-18 16:07:41 +02:00
public commonReadableHandlerGenerator(ctx: Koa.Context) {
return (e: Error): void => {
2022-09-18 20:11:50 +02:00
this.logger.error(e);
2022-09-18 16:07:41 +02:00
ctx.status = 500;
ctx.set('Cache-Control', 'max-age=300');
};
}
2022-09-17 20:27:08 +02:00
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();
});
// Init router
const router = new Router();
router.get('/app-default.jpg', ctx => {
const file = fs.createReadStream(`${_dirname}/assets/dummy.png`);
ctx.body = file;
ctx.set('Content-Type', 'image/jpeg');
ctx.set('Cache-Control', 'max-age=31536000, immutable');
});
2022-09-18 20:11:50 +02:00
router.get('/:key', ctx => this.sendDriveFile(ctx));
router.get('/:key/(.*)', ctx => this.sendDriveFile(ctx));
2022-09-17 20:27:08 +02:00
// Register router
app.use(router.routes());
return app;
}
2022-09-18 20:11:50 +02:00
private async sendDriveFile(ctx: Koa.Context) {
2022-09-17 20:27:08 +02:00
const key = ctx.params.key;
// Fetch drive file
const file = await this.driveFilesRepository.createQueryBuilder('file')
.where('file.accessKey = :accessKey', { accessKey: key })
.orWhere('file.thumbnailAccessKey = :thumbnailAccessKey', { thumbnailAccessKey: key })
.orWhere('file.webpublicAccessKey = :webpublicAccessKey', { webpublicAccessKey: key })
.getOne();
if (file == null) {
ctx.status = 404;
ctx.set('Cache-Control', 'max-age=86400');
await send(ctx as any, '/dummy.png', { root: assets });
return;
}
const isThumbnail = file.thumbnailAccessKey === key;
const isWebpublic = file.webpublicAccessKey === key;
if (!file.storedInternal) {
if (file.isLink && file.uri) { // 期限切れリモートファイル
const [path, cleanup] = await createTemp();
try {
await this.downloadService.downloadUrl(file.uri, path);
const { mime, ext } = await this.fileInfoService.detectType(path);
const convertFile = async () => {
if (isThumbnail) {
if (['image/jpeg', 'image/webp', 'image/png', 'image/svg+xml'].includes(mime)) {
return await this.imageProcessingService.convertToWebp(path, 498, 280);
} else if (mime.startsWith('video/')) {
return await this.videoProcessingService.generateVideoThumbnail(path);
}
}
if (isWebpublic) {
if (['image/svg+xml'].includes(mime)) {
return await this.imageProcessingService.convertToPng(path, 2048, 2048);
}
}
return {
data: fs.readFileSync(path),
ext,
type: mime,
};
};
const image = await convertFile();
ctx.body = image.data;
ctx.set('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
ctx.set('Cache-Control', 'max-age=31536000, immutable');
} catch (err) {
2022-09-18 20:11:50 +02:00
this.logger.error(`${err}`);
2022-09-17 20:27:08 +02:00
if (err instanceof StatusError && err.isClientError) {
ctx.status = err.statusCode;
ctx.set('Cache-Control', 'max-age=86400');
} else {
ctx.status = 500;
ctx.set('Cache-Control', 'max-age=300');
}
} finally {
cleanup();
}
return;
}
ctx.status = 204;
ctx.set('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();
ctx.body = this.internalStorageService.read(key);
ctx.set('Content-Type', FILE_TYPE_BROWSERSAFE.includes(mime) ? mime : 'application/octet-stream');
ctx.set('Cache-Control', 'max-age=31536000, immutable');
ctx.set('Content-Disposition', contentDisposition('inline', filename));
} else {
const readable = this.internalStorageService.read(file.accessKey!);
2022-09-18 16:36:00 +02:00
readable.on('error', this.commonReadableHandlerGenerator(ctx));
2022-09-17 20:27:08 +02:00
ctx.body = readable;
ctx.set('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.type) ? file.type : 'application/octet-stream');
ctx.set('Cache-Control', 'max-age=31536000, immutable');
ctx.set('Content-Disposition', contentDisposition('inline', file.name));
}
}
}