1
0
forked from mirror/misskey
This commit is contained in:
tamaina 2023-01-25 14:07:58 +00:00
parent 1b09e182a7
commit ce8c34a5b7
2 changed files with 33 additions and 17 deletions

View File

@ -73,7 +73,7 @@ export class ImageProcessingService {
*/ */
@bindThis @bindThis
public async convertToWebp(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise<IImage> { public async convertToWebp(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise<IImage> {
return this.convertSharpToWebp(await sharp(path), width, height, options); return this.convertSharpToWebp(sharp(path), width, height, options);
} }
@bindThis @bindThis
@ -94,6 +94,27 @@ export class ImageProcessingService {
}; };
} }
@bindThis
public convertToWebpStream(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
return this.convertSharpToWebpStream(sharp(path), width, height, options);
}
@bindThis
public convertSharpToWebpStream(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
const data = sharp
.resize(width, height, {
fit: 'inside',
withoutEnlargement: true,
})
.rotate()
.webp(options)
return {
data,
ext: 'webp',
type: 'image/webp',
};
}
/** /**
* Convert to PNG * Convert to PNG
* with resize, remove metadata, resolve orientation, stop animation * with resize, remove metadata, resolve orientation, stop animation

View File

@ -139,7 +139,7 @@ export class FileServerService {
const convertFile = async () => { const convertFile = async () => {
if (file.fileRole === 'thumbnail') { if (file.fileRole === 'thumbnail') {
if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(file.mime)) { if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(file.mime)) {
return this.imageProcessingService.convertToWebp( return this.imageProcessingService.convertToWebpStream(
file.path, file.path,
498, 498,
280 280
@ -151,16 +151,12 @@ export class FileServerService {
if (file.fileRole === 'webpublic') { if (file.fileRole === 'webpublic') {
if (['image/svg+xml'].includes(file.mime)) { if (['image/svg+xml'].includes(file.mime)) {
return { return this.imageProcessingService.convertToWebpStream(
data: this.imageProcessingService.convertToWebp( file.path,
file.path, 2048,
2048, 2048,
2048, { ...webpDefault, lossless: true }
{ ...webpDefault, lossless: true } )
),
ext: 'webp',
type: 'image/webp',
};
} }
} }
@ -241,8 +237,7 @@ export class FileServerService {
height: 128, height: 128,
withoutEnlargement: true, withoutEnlargement: true,
}) })
.webp(webpDefault) .webp(webpDefault);
.toBuffer();
image = { image = {
data, data,
@ -251,9 +246,9 @@ export class FileServerService {
}; };
} }
} else if ('static' in request.query && isConvertibleImage) { } else if ('static' in request.query && isConvertibleImage) {
image = this.imageProcessingService.convertToWebp(file.path, 498, 280); image = this.imageProcessingService.convertToWebpStream(file.path, 498, 280);
} else if ('preview' in request.query && isConvertibleImage) { } else if ('preview' in request.query && isConvertibleImage) {
image = this.imageProcessingService.convertToWebp(file.path, 200, 200); image = this.imageProcessingService.convertToWebpStream(file.path, 200, 200);
} else if ('badge' in request.query) { } else if ('badge' in request.query) {
if (!isConvertibleImage) { if (!isConvertibleImage) {
// 画像でないなら404でお茶を濁す // 画像でないなら404でお茶を濁す
@ -290,7 +285,7 @@ export class FileServerService {
type: 'image/png', type: 'image/png',
}; };
} else if (file.mime === 'image/svg+xml') { } else if (file.mime === 'image/svg+xml') {
image = this.imageProcessingService.convertToWebp(file.path, 2048, 2048); image = this.imageProcessingService.convertToWebpStream(file.path, 2048, 2048);
} else if (!file.mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(file.mime)) { } else if (!file.mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(file.mime)) {
throw new StatusError('Rejected type', 403, 'Rejected type'); throw new StatusError('Rejected type', 403, 'Rejected type');
} }