wip?
This commit is contained in:
parent
a4a614e180
commit
453ba25af2
5 changed files with 148 additions and 47 deletions
|
|
@ -12,14 +12,17 @@ import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
|
|||
import { StatusError } from '@/misc/status-error.js';
|
||||
import type Logger from '@/logger.js';
|
||||
import { DownloadService } from '@/core/DownloadService.js';
|
||||
import { ImageProcessingService } from '@/core/ImageProcessingService.js';
|
||||
import { 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';
|
||||
import { FileInfoService } from '@/core/FileInfoService.js';
|
||||
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 { PassThrough } from 'node:stream';
|
||||
import sharp from 'sharp';
|
||||
import { Request } from 'got';
|
||||
|
||||
const _filename = fileURLToPath(import.meta.url);
|
||||
const _dirname = dirname(_filename);
|
||||
|
|
@ -106,29 +109,43 @@ export class FileServerService {
|
|||
if (!file.storedInternal) {
|
||||
if (file.isLink && file.uri) { // 期限切れリモートファイル
|
||||
const [path, cleanup] = await createTemp();
|
||||
const got = this.downloadService.gotUrl(file.uri);;
|
||||
|
||||
try {
|
||||
await this.downloadService.downloadUrl(file.uri, path);
|
||||
const fileSaving = this.downloadService.pipeRequestToFile(got, path);
|
||||
const streamCopy = got.pipe(new PassThrough());
|
||||
|
||||
const { mime, ext } = await this.fileInfoService.detectType(path);
|
||||
let { mime, ext } = await this.fileInfoService.detectRequestType(got);
|
||||
if (mime === 'application/octet-stream' || mime === 'application/xml') {
|
||||
await fileSaving;
|
||||
if (await this.fileInfoService.checkSvg(path)) {
|
||||
mime = TYPE_SVG.mime;
|
||||
ext = TYPE_SVG.ext;
|
||||
}
|
||||
}
|
||||
|
||||
const convertFile = async () => {
|
||||
if (isThumbnail) {
|
||||
if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(mime)) {
|
||||
return await this.imageProcessingService.convertToWebp(path, 498, 280);
|
||||
return this.imageProcessingService.convertSharpToWebpStreamObj(streamCopy.pipe(sharp()), 498, 280);
|
||||
} else if (mime.startsWith('video/')) {
|
||||
await fileSaving;
|
||||
return await this.videoProcessingService.generateVideoThumbnail(path);
|
||||
}
|
||||
}
|
||||
|
||||
if (isWebpublic) {
|
||||
if (['image/svg+xml'].includes(mime)) {
|
||||
return await this.imageProcessingService.convertToPng(path, 2048, 2048);
|
||||
return {
|
||||
data: this.imageProcessingService.convertSharpToWebpStream(streamCopy.pipe(sharp()), 2048, 2048, { ...webpDefault, lossless: true }),
|
||||
ext: 'webp',
|
||||
type: 'image/webp',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
data: fs.readFileSync(path),
|
||||
data: streamCopy,
|
||||
ext,
|
||||
type: mime,
|
||||
};
|
||||
|
|
@ -141,6 +158,7 @@ export class FileServerService {
|
|||
} catch (err) {
|
||||
this.logger.error(`${err}`);
|
||||
|
||||
if (!got.closed) got.destroy();
|
||||
if (err instanceof StatusError && err.isClientError) {
|
||||
reply.code(err.statusCode);
|
||||
reply.header('Cache-Control', 'max-age=86400');
|
||||
|
|
|
|||
|
|
@ -9,15 +9,17 @@ 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 { 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 } from '@/core/FileInfoService.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 { PassThrough, pipeline } from 'node:stream';
|
||||
import { Request } from 'got';
|
||||
|
||||
const _filename = fileURLToPath(import.meta.url);
|
||||
const _dirname = dirname(_filename);
|
||||
|
|
@ -73,22 +75,33 @@ export class MediaProxyServerService {
|
|||
|
||||
// Create temp file
|
||||
const [path, cleanup] = await createTemp();
|
||||
const got = this.downloadService.gotUrl(url);
|
||||
|
||||
try {
|
||||
await this.downloadService.downloadUrl(url, path);
|
||||
|
||||
const { mime, ext } = await this.fileInfoService.detectType(path);
|
||||
const fileSaving = this.downloadService.pipeRequestToFile(got, path);
|
||||
const streamCopy = got.pipe(new PassThrough());
|
||||
|
||||
let { mime, ext } = await this.fileInfoService.detectRequestType(got);
|
||||
if (mime === 'application/octet-stream' || mime === 'application/xml') {
|
||||
await fileSaving;
|
||||
if (await this.fileInfoService.checkSvg(path)) {
|
||||
mime = TYPE_SVG.mime;
|
||||
ext = TYPE_SVG.ext;
|
||||
}
|
||||
}
|
||||
const isConvertibleImage = isMimeImage(mime, 'sharp-convertible-image');
|
||||
|
||||
let image: IImage;
|
||||
let image: IImageStreamable;
|
||||
if ('emoji' in request.query && isConvertibleImage) {
|
||||
const data = await sharp(path, { animated: !('static' in request.query) })
|
||||
.resize({
|
||||
height: 128,
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.webp(webpDefault)
|
||||
.toBuffer();
|
||||
const data = pipeline(
|
||||
streamCopy,
|
||||
sharp({ animated: !('static' in request.query) })
|
||||
.resize({
|
||||
height: 128,
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.webp(webpDefault),
|
||||
);
|
||||
|
||||
image = {
|
||||
data,
|
||||
|
|
@ -96,15 +109,17 @@ export class MediaProxyServerService {
|
|||
type: 'image/webp',
|
||||
};
|
||||
} else if ('static' in request.query && isConvertibleImage) {
|
||||
image = await this.imageProcessingService.convertToWebp(path, 498, 280);
|
||||
image = this.imageProcessingService.convertSharpToWebpStreamObj(streamCopy.pipe(sharp()), 498, 280);
|
||||
} else if ('preview' in request.query && isConvertibleImage) {
|
||||
image = await this.imageProcessingService.convertToWebp(path, 200, 200);
|
||||
image = this.imageProcessingService.convertSharpToWebpStreamObj(streamCopy.pipe(sharp()), 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',
|
||||
|
|
@ -135,12 +150,12 @@ export class MediaProxyServerService {
|
|||
type: 'image/png',
|
||||
};
|
||||
} else if (mime === 'image/svg+xml') {
|
||||
image = await this.imageProcessingService.convertToWebp(path, 2048, 2048, webpDefault);
|
||||
image = this.imageProcessingService.convertSharpToWebpStreamObj(streamCopy.pipe(sharp()), 2048, 2048);
|
||||
} else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) {
|
||||
throw new StatusError('Rejected type', 403, 'Rejected type');
|
||||
} else {
|
||||
image = {
|
||||
data: fs.readFileSync(path),
|
||||
data: streamCopy,
|
||||
ext,
|
||||
type: mime,
|
||||
};
|
||||
|
|
@ -152,6 +167,8 @@ export class MediaProxyServerService {
|
|||
} catch (err) {
|
||||
this.logger.error(`${err}`);
|
||||
|
||||
if (!got.closed) got.destroy();
|
||||
|
||||
if ('fallback' in request.query) {
|
||||
return reply.sendFile('/dummy.png', assets);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue