WebPのアニメーションが失われるのを修正 Fix #6625 (#6649)

This commit is contained in:
MeiMei 2020-08-18 22:48:52 +09:00 committed by GitHub
parent 9855405b89
commit 48e8ee440b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 13 deletions

View File

@ -21,7 +21,7 @@ export async function proxyMedia(ctx: Koa.Context) {
let image: IImage;
if ('static' in ctx.query && ['image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng'].includes(mime)) {
if ('static' in ctx.query && ['image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng', 'image/webp'].includes(mime)) {
image = await convertToPng(path, 498, 280);
} else if ('preview' in ctx.query && ['image/jpeg', 'image/png', 'image/gif', 'image/apng', 'image/vnd.mozilla.apng'].includes(mime)) {
image = await convertToJpeg(path, 200, 200);

View File

@ -7,7 +7,7 @@ import { deleteFile } from './delete-file';
import { fetchMeta } from '../../misc/fetch-meta';
import { GenerateVideoThumbnail } from './generate-video-thumbnail';
import { driveLogger } from './logger';
import { IImage, convertToJpeg, convertToWebp, convertToPng, convertToPngOrJpeg } from './image-processor';
import { IImage, convertSharpToJpeg, convertSharpToWebp, convertSharpToPng, convertSharpToPngOrJpeg } from './image-processor';
import { contentDisposition } from '../../misc/content-disposition';
import { getFileInfo } from '../../misc/get-file-info';
import { DriveFiles, DriveFolders, Users, Instances, UserProfiles } from '../../models';
@ -19,6 +19,7 @@ import { genId } from '../../misc/gen-id';
import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
import * as S3 from 'aws-sdk/clients/s3';
import { getS3 } from './s3';
import * as sharp from 'sharp';
const logger = driveLogger.createSubLogger('register', 'yellow');
@ -143,6 +144,34 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
* @param generateWeb Generate webpublic or not
*/
export async function generateAlts(path: string, type: string, generateWeb: boolean) {
if (type.startsWith('video/')) {
try {
const thumbnail = await GenerateVideoThumbnail(path);
return {
webpublic: null,
thumbnail
};
} catch (e) {
logger.warn(`GenerateVideoThumbnail failed: ${e}`);
return {
webpublic: null,
thumbnail: null
};
}
}
const img = sharp(path);
const metadata = await img.metadata();
const isAnimated = metadata.pages && metadata.pages > 1;
// skip animated
if (isAnimated) {
return {
webpublic: null,
thumbnail: null
};
}
// #region webpublic
let webpublic: IImage | null = null;
@ -151,11 +180,11 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
try {
if (['image/jpeg'].includes(type)) {
webpublic = await convertToJpeg(path, 2048, 2048);
webpublic = await convertSharpToJpeg(img, 2048, 2048);
} else if (['image/webp'].includes(type)) {
webpublic = await convertToWebp(path, 2048, 2048);
webpublic = await convertSharpToWebp(img, 2048, 2048);
} else if (['image/png'].includes(type)) {
webpublic = await convertToPng(path, 2048, 2048);
webpublic = await convertSharpToPng(img, 2048, 2048);
} else {
logger.debug(`web image not created (not an required image)`);
}
@ -172,15 +201,9 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
try {
if (['image/jpeg', 'image/webp'].includes(type)) {
thumbnail = await convertToJpeg(path, 498, 280);
thumbnail = await convertSharpToJpeg(img, 498, 280);
} else if (['image/png'].includes(type)) {
thumbnail = await convertToPngOrJpeg(path, 498, 280);
} else if (type.startsWith('video/')) {
try {
thumbnail = await GenerateVideoThumbnail(path);
} catch (e) {
logger.warn(`GenerateVideoThumbnail failed: ${e}`);
}
thumbnail = await convertSharpToPngOrJpeg(img, 498, 280);
} else {
logger.debug(`thumbnail not created (not an required file)`);
}