Add ability to use full images as thumbnails

This commit is contained in:
Maksim Karasev 2021-09-08 13:46:11 +03:00
parent de63bb6311
commit e83c461c75
5 changed files with 20 additions and 5 deletions

View File

@ -64,6 +64,8 @@ All docker environment variables are optional
`BASE_URL` - base directory if app isn't in root
`FULLS_AS_THUMBS` - whether to use full size images as thumbnails (useful on slow servers with fast clients)
To use Redis instead of built-it memory cache:
`REDIS_HOST` - Redis host

View File

@ -6,4 +6,5 @@
#PROXY='127.0.0.1/8' # (optional) express formatted reverse proxy settings
# USERNAME=test # (optional) used to reset user credentials
# PASSWORD=test # (optional) used to reset user credentials
# BASE_URL=gallery # (optional) base directory if app isn't in root
# BASE_URL=gallery # (optional) base directory if app isn't in root
# FULLS_AS_THUMBS=false # (optional) whether to use full size images as thumbnails (useful on slow servers with fast clients)

View File

@ -4,6 +4,7 @@ import logger from '../utils/logger';
import { imageExists } from '../services/imageService';
import { thumbnailCache } from '../utils/cache';
import { getThumbnail } from '../services/thumbnailService';
import { FULLS_AS_THUMBS } from '../utils/config';
const thumbnailsRouter = Router();
@ -12,10 +13,16 @@ thumbnailsRouter.get('/:image/:type', async (req, res) => {
if (!imageExists(req.params.image)) {
throw new Error('Not found.');
}
const result = (await thumbnailCache.wrap(
`${req.params.image}${req.params.type}`,
() => getThumbnail(req.params.image, req.params.type),
)) as any;
let result:any;
// dont cache thumbs if using fulls
if (FULLS_AS_THUMBS) {
result = await getThumbnail(req.params.image, req.params.type, true);
} else {
result = (await thumbnailCache.wrap(
`${req.params.image}${req.params.type}`,
() => getThumbnail(req.params.image, req.params.type, false),
)) as any;
}
// cache-manager returns different types based on the backend,
// so we manually check how we should treat the result
if (result instanceof Buffer) {

View File

@ -49,6 +49,7 @@ const ffmpegScreenshotPromise = (
export const getThumbnail = async (
filename: string,
thumbnailFormat: string,
fullsAsThumbs: boolean,
width: number = 210,
height: number = 160,
): Promise<Buffer> => {
@ -77,6 +78,9 @@ export const getThumbnail = async (
}
tmpDir.removeCallback();
}
if (fullsAsThumbs) {
return imagebuffer;
}
// Convert the buffer into requested format
switch (thumbnailFormat) {
case 'jpeg':

View File

@ -17,3 +17,4 @@ export const { REDIS_PASSWORD } = process.env;
export const PROXY = process.env.PROXY || true;
export const { USERNAME } = process.env;
export const { PASSWORD } = process.env;
export const FULLS_AS_THUMBS = process.env.FULLS_AS_THUMBS === 'true' || false;