mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-29 13:27:31 -07:00
parent
481aa17886
commit
17e6782094
2 changed files with 36 additions and 13 deletions
|
@ -21,7 +21,7 @@ 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'].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);
|
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)) {
|
} 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);
|
image = await convertToJpeg(path, 200, 200);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { deleteFile } from './delete-file';
|
||||||
import { fetchMeta } from '../../misc/fetch-meta';
|
import { fetchMeta } from '../../misc/fetch-meta';
|
||||||
import { GenerateVideoThumbnail } from './generate-video-thumbnail';
|
import { GenerateVideoThumbnail } from './generate-video-thumbnail';
|
||||||
import { driveLogger } from './logger';
|
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 { contentDisposition } from '../../misc/content-disposition';
|
||||||
import { getFileInfo } from '../../misc/get-file-info';
|
import { getFileInfo } from '../../misc/get-file-info';
|
||||||
import { DriveFiles, DriveFolders, Users, Instances, UserProfiles } from '../../models';
|
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 { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
|
||||||
import * as S3 from 'aws-sdk/clients/s3';
|
import * as S3 from 'aws-sdk/clients/s3';
|
||||||
import { getS3 } from './s3';
|
import { getS3 } from './s3';
|
||||||
|
import * as sharp from 'sharp';
|
||||||
|
|
||||||
const logger = driveLogger.createSubLogger('register', 'yellow');
|
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
|
* @param generateWeb Generate webpublic or not
|
||||||
*/
|
*/
|
||||||
export async function generateAlts(path: string, type: string, generateWeb: boolean) {
|
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
|
// #region webpublic
|
||||||
let webpublic: IImage | null = null;
|
let webpublic: IImage | null = null;
|
||||||
|
|
||||||
|
@ -151,11 +180,11 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (['image/jpeg'].includes(type)) {
|
if (['image/jpeg'].includes(type)) {
|
||||||
webpublic = await convertToJpeg(path, 2048, 2048);
|
webpublic = await convertSharpToJpeg(img, 2048, 2048);
|
||||||
} else if (['image/webp'].includes(type)) {
|
} else if (['image/webp'].includes(type)) {
|
||||||
webpublic = await convertToWebp(path, 2048, 2048);
|
webpublic = await convertSharpToWebp(img, 2048, 2048);
|
||||||
} else if (['image/png'].includes(type)) {
|
} else if (['image/png'].includes(type)) {
|
||||||
webpublic = await convertToPng(path, 2048, 2048);
|
webpublic = await convertSharpToPng(img, 2048, 2048);
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`web image not created (not an required image)`);
|
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 {
|
try {
|
||||||
if (['image/jpeg', 'image/webp'].includes(type)) {
|
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)) {
|
} else if (['image/png'].includes(type)) {
|
||||||
thumbnail = await convertToPngOrJpeg(path, 498, 280);
|
thumbnail = await convertSharpToPngOrJpeg(img, 498, 280);
|
||||||
} else if (type.startsWith('video/')) {
|
|
||||||
try {
|
|
||||||
thumbnail = await GenerateVideoThumbnail(path);
|
|
||||||
} catch (e) {
|
|
||||||
logger.warn(`GenerateVideoThumbnail failed: ${e}`);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`thumbnail not created (not an required file)`);
|
logger.debug(`thumbnail not created (not an required file)`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue