parent
23c9f6a6ca
commit
3d8bbedf1b
4 changed files with 47 additions and 14 deletions
|
@ -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 '..';
|
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 { createTemp } from '../../misc/create-temp';
|
||||||
import { downloadUrl } from '../../misc/donwload-url';
|
import { downloadUrl } from '../../misc/donwload-url';
|
||||||
import { detectMine } from '../../misc/detect-mine';
|
import { detectMine } from '../../misc/detect-mine';
|
||||||
|
@ -20,9 +20,9 @@ export async function proxyMedia(ctx: Koa.BaseContext) {
|
||||||
let image: IImage;
|
let image: IImage;
|
||||||
|
|
||||||
if ('static' in ctx.query && ['image/png', 'image/gif'].includes(type)) {
|
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)) {
|
} 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 {
|
} else {
|
||||||
image = {
|
image = {
|
||||||
data: fs.readFileSync(path),
|
data: fs.readFileSync(path),
|
||||||
|
|
|
@ -12,7 +12,7 @@ import config from '../../config';
|
||||||
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 } from './image-processor';
|
import { IImage, convertToJpeg, convertToWebp, convertToPng, convertToGif, convertToApng } from './image-processor';
|
||||||
import { contentDisposition } from '../../misc/content-disposition';
|
import { contentDisposition } from '../../misc/content-disposition';
|
||||||
import { detectMine } from '../../misc/detect-mine';
|
import { detectMine } from '../../misc/detect-mine';
|
||||||
import { DriveFiles, DriveFolders, Users, Instances, UserProfiles } from '../../models';
|
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`);
|
logger.info(`creating web image`);
|
||||||
|
|
||||||
if (['image/jpeg'].includes(type)) {
|
if (['image/jpeg'].includes(type)) {
|
||||||
webpublic = await ConvertToJpeg(path, 2048, 2048);
|
webpublic = await convertToJpeg(path, 2048, 2048);
|
||||||
} else if (['image/webp'].includes(type)) {
|
} else if (['image/webp'].includes(type)) {
|
||||||
webpublic = await ConvertToWebp(path, 2048, 2048);
|
webpublic = await convertToWebp(path, 2048, 2048);
|
||||||
} else if (['image/png'].includes(type)) {
|
} 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 {
|
} else {
|
||||||
logger.info(`web image not created (not an image)`);
|
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;
|
let thumbnail: IImage | null = null;
|
||||||
|
|
||||||
if (['image/jpeg', 'image/webp'].includes(type)) {
|
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)) {
|
} 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/')) {
|
} else if (type.startsWith('video/')) {
|
||||||
try {
|
try {
|
||||||
thumbnail = await GenerateVideoThumbnail(path);
|
thumbnail = await GenerateVideoThumbnail(path);
|
||||||
|
|
|
@ -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, convertToJpeg } from './image-processor';
|
||||||
const ThumbnailGenerator = require('video-thumbnail-generator').default;
|
const ThumbnailGenerator = require('video-thumbnail-generator').default;
|
||||||
|
|
||||||
export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
|
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 outPath = `${outDir}/output.png`;
|
||||||
|
|
||||||
const thumbnail = await ConvertToJpeg(outPath, 498, 280);
|
const thumbnail = await convertToJpeg(outPath, 498, 280);
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
fs.unlinkSync(outPath);
|
fs.unlinkSync(outPath);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import * as sharp from 'sharp';
|
import * as sharp from 'sharp';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
|
||||||
export type IImage = {
|
export type IImage = {
|
||||||
data: Buffer;
|
data: Buffer;
|
||||||
|
@ -10,7 +11,7 @@ export type IImage = {
|
||||||
* Convert to JPEG
|
* Convert to JPEG
|
||||||
* with resize, remove metadata, resolve orientation, stop animation
|
* 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)
|
const data = await sharp(path)
|
||||||
.resize(width, height, {
|
.resize(width, height, {
|
||||||
fit: 'inside',
|
fit: 'inside',
|
||||||
|
@ -34,7 +35,7 @@ export async function ConvertToJpeg(path: string, width: number, height: number)
|
||||||
* 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): Promise<IImage> {
|
||||||
const data = await sharp(path)
|
const data = await sharp(path)
|
||||||
.resize(width, height, {
|
.resize(width, height, {
|
||||||
fit: 'inside',
|
fit: 'inside',
|
||||||
|
@ -57,7 +58,7 @@ export async function ConvertToWebp(path: string, width: number, height: number)
|
||||||
* Convert to PNG
|
* Convert to PNG
|
||||||
* with resize, remove metadata, resolve orientation, stop animation
|
* 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)
|
const data = await sharp(path)
|
||||||
.resize(width, height, {
|
.resize(width, height, {
|
||||||
fit: 'inside',
|
fit: 'inside',
|
||||||
|
@ -73,3 +74,29 @@ export async function ConvertToPng(path: string, width: number, height: number):
|
||||||
type: 'image/png'
|
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'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue