From 30a21e820d18f9718ae839e61ef837addcc61829 Mon Sep 17 00:00:00 2001 From: Stefan Forstenlechner Date: Fri, 29 Apr 2022 21:29:34 +0200 Subject: [PATCH] Extract a method and add some typings --- .../src/controller/images.ts | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/picture-gallery-server/src/controller/images.ts b/picture-gallery-server/src/controller/images.ts index a8793d1..611254a 100644 --- a/picture-gallery-server/src/controller/images.ts +++ b/picture-gallery-server/src/controller/images.ts @@ -14,10 +14,10 @@ const notEmpty = ( return value !== null && value !== undefined; }; -const getRequestedPath = (req: express.Request) => +const getRequestedPath = (req: express.Request): string => req.params[1] === undefined || req.params[1] === "/" ? "" : req.params[1]; -const readThumbnails = (requestedPath: string) => { +const readThumbnails = (requestedPath: string): string[] => { const requestedThumbnailPath = path.posix.join( thumbnailPublicPath, requestedPath @@ -27,7 +27,11 @@ const readThumbnails = (requestedPath: string) => { : []; }; -const getSrc = (thumbnailExists: boolean, requestedPath: string, f: Dirent) => +const getSrc = ( + thumbnailExists: boolean, + requestedPath: string, + f: Dirent +): string => thumbnailExists ? path.posix.join("/staticImages", thumbnailPath, requestedPath, f.name) : path.posix.join("/staticImages", requestedPath, f.name); @@ -37,7 +41,7 @@ const toImage = ( thumbnailExists: boolean, requestedPath: string, f: Dirent -) => { +): Image => { const widthAndHeightSwap = metadata.orientation > 4; // see https://exiftool.org/TagNames/EXIF.html return a({ src: getSrc(thumbnailExists, requestedPath, f), @@ -46,6 +50,34 @@ const toImage = ( }); }; +const getImagesToBeLoaded = ( + dirents: Dirent[], + thumbnails: string[], + requestedPath: string +): Promise[] => + dirents + .filter((f) => f.isFile()) + .map((f) => { + const thumbnailExists: boolean = thumbnails.includes(f.name); + if (!thumbnailExists) { + createThumbnailAsyncForImage(path.posix.join(requestedPath, f.name)); + } + return sharp(path.posix.join(publicPath, requestedPath, f.name)) + .metadata() + .then((metadata) => + toImage(metadata, thumbnailExists, requestedPath, f) + ) + .catch((err) => { + consoleLogger.error( + `Reading metadata from ${path.posix.join( + publicPath, + requestedPath, + f.name + )} produced the following error: ${err.message}` + ); + }); + }); + export const getImages = async ( req: express.Request, res: express.Response @@ -58,31 +90,13 @@ export const getImages = async ( const dirents = fs.readdirSync(path.posix.join(publicPath, requestedPath), { withFileTypes: true, }); - const thumbnails = readThumbnails(requestedPath); - const imagesToBeLoaded = dirents - .filter((f) => f.isFile()) - .map((f) => { - const thumbnailExists: boolean = thumbnails.includes(f.name); - if (!thumbnailExists) { - createThumbnailAsyncForImage(path.posix.join(requestedPath, f.name)); - } - return sharp(path.posix.join(publicPath, requestedPath, f.name)) - .metadata() - .then((metadata) => - toImage(metadata, thumbnailExists, requestedPath, f) - ) - .catch((err) => { - consoleLogger.error( - `Reading metadata from ${path.posix.join( - publicPath, - requestedPath, - f.name - )} produced the following error: ${err.message}` - ); - }); - }); + const imagesToBeLoaded = getImagesToBeLoaded( + dirents, + thumbnails, + requestedPath + ); const images = (await Promise.all(imagesToBeLoaded)).filter(notEmpty); res.json(a({ images })); } catch (e) {