GIFのサムネイルが生成されないのを修正

#4728
This commit is contained in:
syuilo 2019-05-15 21:27:20 +09:00
parent 23c9f6a6ca
commit 3d8bbedf1b
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
4 changed files with 47 additions and 14 deletions

View File

@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as Koa from 'koa';
import { serverLogger } from '..';
import { IImage, ConvertToPng, ConvertToJpeg } from '../../services/drive/image-processor';
import { IImage, convertToPng, convertToJpeg } from '../../services/drive/image-processor';
import { createTemp } from '../../misc/create-temp';
import { downloadUrl } from '../../misc/donwload-url';
import { detectMine } from '../../misc/detect-mine';
@ -20,9 +20,9 @@ export async function proxyMedia(ctx: Koa.BaseContext) {
let image: IImage;
if ('static' in ctx.query && ['image/png', 'image/gif'].includes(type)) {
image = await ConvertToPng(path, 498, 280);
image = await convertToPng(path, 498, 280);
} else if ('preview' in ctx.query && ['image/jpeg', 'image/png', 'image/gif'].includes(type)) {
image = await ConvertToJpeg(path, 200, 200);
image = await convertToJpeg(path, 200, 200);
} else {
image = {
data: fs.readFileSync(path),

View File

@ -12,7 +12,7 @@ import config from '../../config';
import { fetchMeta } from '../../misc/fetch-meta';
import { GenerateVideoThumbnail } from './generate-video-thumbnail';
import { driveLogger } from './logger';
import { IImage, ConvertToJpeg, ConvertToWebp, ConvertToPng } from './image-processor';
import { IImage, convertToJpeg, convertToWebp, convertToPng, convertToGif, convertToApng } from './image-processor';
import { contentDisposition } from '../../misc/content-disposition';
import { detectMine } from '../../misc/detect-mine';
import { DriveFiles, DriveFolders, Users, Instances, UserProfiles } from '../../models';
@ -149,11 +149,15 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
logger.info(`creating web image`);
if (['image/jpeg'].includes(type)) {
webpublic = await ConvertToJpeg(path, 2048, 2048);
webpublic = await convertToJpeg(path, 2048, 2048);
} else if (['image/webp'].includes(type)) {
webpublic = await ConvertToWebp(path, 2048, 2048);
webpublic = await convertToWebp(path, 2048, 2048);
} else if (['image/png'].includes(type)) {
webpublic = await ConvertToPng(path, 2048, 2048);
webpublic = await convertToPng(path, 2048, 2048);
} else if (['image/apng', 'image/vnd.mozilla.apng'].includes(type)) {
webpublic = await convertToApng(path);
} else if (['image/gif'].includes(type)) {
webpublic = await convertToGif(path);
} else {
logger.info(`web image not created (not an image)`);
}
@ -166,9 +170,11 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
let thumbnail: IImage | null = null;
if (['image/jpeg', 'image/webp'].includes(type)) {
thumbnail = await ConvertToJpeg(path, 498, 280);
thumbnail = await convertToJpeg(path, 498, 280);
} else if (['image/png'].includes(type)) {
thumbnail = await ConvertToPng(path, 498, 280);
thumbnail = await convertToPng(path, 498, 280);
} else if (['image/gif'].includes(type)) {
thumbnail = await convertToGif(path);
} else if (type.startsWith('video/')) {
try {
thumbnail = await GenerateVideoThumbnail(path);

View File

@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as tmp from 'tmp';
import { IImage, ConvertToJpeg } from './image-processor';
import { IImage, convertToJpeg } from './image-processor';
const ThumbnailGenerator = require('video-thumbnail-generator').default;
export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
@ -23,7 +23,7 @@ export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
const outPath = `${outDir}/output.png`;
const thumbnail = await ConvertToJpeg(outPath, 498, 280);
const thumbnail = await convertToJpeg(outPath, 498, 280);
// cleanup
fs.unlinkSync(outPath);

View File

@ -1,4 +1,5 @@
import * as sharp from 'sharp';
import * as fs from 'fs';
export type IImage = {
data: Buffer;
@ -10,7 +11,7 @@ export type IImage = {
* Convert to JPEG
* with resize, remove metadata, resolve orientation, stop animation
*/
export async function ConvertToJpeg(path: string, width: number, height: number): Promise<IImage> {
export async function convertToJpeg(path: string, width: number, height: number): Promise<IImage> {
const data = await sharp(path)
.resize(width, height, {
fit: 'inside',
@ -34,7 +35,7 @@ export async function ConvertToJpeg(path: string, width: number, height: number)
* Convert to WebP
* 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): Promise<IImage> {
const data = await sharp(path)
.resize(width, height, {
fit: 'inside',
@ -57,7 +58,7 @@ export async function ConvertToWebp(path: string, width: number, height: number)
* Convert to PNG
* with resize, remove metadata, resolve orientation, stop animation
*/
export async function ConvertToPng(path: string, width: number, height: number): Promise<IImage> {
export async function convertToPng(path: string, width: number, height: number): Promise<IImage> {
const data = await sharp(path)
.resize(width, height, {
fit: 'inside',
@ -73,3 +74,29 @@ export async function ConvertToPng(path: string, width: number, height: number):
type: 'image/png'
};
}
/**
* Convert to GIF (Actually just NOP)
*/
export async function convertToGif(path: string): Promise<IImage> {
const data = await fs.promises.readFile(path);
return {
data,
ext: 'gif',
type: 'image/gif'
};
}
/**
* Convert to APNG (Actually just NOP)
*/
export async function convertToApng(path: string): Promise<IImage> {
const data = await fs.promises.readFile(path);
return {
data,
ext: 'apng',
type: 'image/apng'
};
}