webp
This commit is contained in:
parent
0f8f7de165
commit
8a32cedf6e
4 changed files with 10 additions and 10 deletions
|
@ -11,7 +11,7 @@ import { DriveFiles } from '@/models/index';
|
||||||
import { InternalStorage } from '@/services/drive/internal-storage';
|
import { InternalStorage } from '@/services/drive/internal-storage';
|
||||||
import { downloadUrl } from '@/misc/download-url';
|
import { downloadUrl } from '@/misc/download-url';
|
||||||
import { detectType } from '@/misc/get-file-info';
|
import { detectType } from '@/misc/get-file-info';
|
||||||
import { convertToWebp, convertToPng } from '@/services/drive/image-processor';
|
import { convertToWebp } from '@/services/drive/image-processor';
|
||||||
import { GenerateVideoThumbnail } from '@/services/drive/generate-video-thumbnail';
|
import { GenerateVideoThumbnail } from '@/services/drive/generate-video-thumbnail';
|
||||||
import { StatusError } from '@/misc/fetch';
|
import { StatusError } from '@/misc/fetch';
|
||||||
import { FILE_TYPE_BROWSERSAFE } from '@/const';
|
import { FILE_TYPE_BROWSERSAFE } from '@/const';
|
||||||
|
@ -74,7 +74,7 @@ export default async function(ctx: Koa.Context) {
|
||||||
|
|
||||||
if (isWebpublic) {
|
if (isWebpublic) {
|
||||||
if (['image/svg+xml'].includes(mime)) {
|
if (['image/svg+xml'].includes(mime)) {
|
||||||
return await convertToPng(path, 2048, 2048);
|
return await convertToWebp(path, 2048, 2048, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as Koa from 'koa';
|
import * as Koa from 'koa';
|
||||||
import { serverLogger } from '../index';
|
import { serverLogger } from '../index';
|
||||||
import { IImage, convertToPng, convertToJpeg } from '@/services/drive/image-processor';
|
import { IImage, convertToWebp } from '@/services/drive/image-processor';
|
||||||
import { createTemp } from '@/misc/create-temp';
|
import { createTemp } from '@/misc/create-temp';
|
||||||
import { downloadUrl } from '@/misc/download-url';
|
import { downloadUrl } from '@/misc/download-url';
|
||||||
import { detectType } from '@/misc/get-file-info';
|
import { detectType } from '@/misc/get-file-info';
|
||||||
|
@ -22,11 +22,11 @@ export async function proxyMedia(ctx: Koa.Context) {
|
||||||
let image: IImage;
|
let image: IImage;
|
||||||
|
|
||||||
if ('static' in ctx.query && ['image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp', 'image/svg+xml'].includes(mime)) {
|
if ('static' in ctx.query && ['image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp', 'image/svg+xml'].includes(mime)) {
|
||||||
image = await convertToPng(path, 498, 280);
|
image = await convertToWebp(path, 498, 280);
|
||||||
} else if ('preview' in ctx.query && ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/svg+xml'].includes(mime)) {
|
} else if ('preview' in ctx.query && ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/svg+xml'].includes(mime)) {
|
||||||
image = await convertToJpeg(path, 200, 200);
|
image = await convertToWebp(path, 200, 200);
|
||||||
} else if (['image/svg+xml'].includes(mime)) {
|
} else if (['image/svg+xml'].includes(mime)) {
|
||||||
image = await convertToPng(path, 2048, 2048);
|
image = await convertToWebp(path, 2048, 2048, 1);
|
||||||
} else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) {
|
} else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) {
|
||||||
throw new StatusError('Rejected type', 403, 'Rejected type');
|
throw new StatusError('Rejected type', 403, 'Rejected type');
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as tmp from 'tmp';
|
import * as tmp from 'tmp';
|
||||||
import { IImage, convertToJpeg } from './image-processor';
|
import { IImage, convertToWebp } from './image-processor';
|
||||||
import * as FFmpeg from 'fluent-ffmpeg';
|
import * as FFmpeg from 'fluent-ffmpeg';
|
||||||
|
|
||||||
export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
|
export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
|
||||||
|
@ -27,7 +27,7 @@ export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
|
||||||
|
|
||||||
const outPath = `${outDir}/output.png`;
|
const outPath = `${outDir}/output.png`;
|
||||||
|
|
||||||
const thumbnail = await convertToJpeg(outPath, 498, 280);
|
const thumbnail = await convertToWebp(outPath, 498, 280);
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
await fs.promises.unlink(outPath);
|
await fs.promises.unlink(outPath);
|
||||||
|
|
|
@ -38,8 +38,8 @@ export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, heig
|
||||||
* Convert to WebP
|
* Convert to WebP
|
||||||
* with resize, remove metadata, resolve orientation, stop animation
|
* with resize, remove metadata, resolve orientation, stop animation
|
||||||
*/
|
*/
|
||||||
export async function convertToWebp(path: string, width: number, height: number): Promise<IImage> {
|
export async function convertToWebp(path: string, width: number, height: number, quality: number = 85): Promise<IImage> {
|
||||||
return convertSharpToWebp(await sharp(path), width, height);
|
return convertSharpToWebp(await sharp(path), width, height, quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number, quality: number = 85): Promise<IImage> {
|
export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number, quality: number = 85): Promise<IImage> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue