import { Request, Response } from "express"; import fs from "fs-extra"; import path from "path"; import GenerateThumbnail from "../util/generateThumbnail"; import GenerateUrl from "../util/MediaUrlGen"; import { FolderPaths } from "../util/serverInit"; export async function JobsListMedia(req: Request, res: Response) { const ro_number: string = (req.body.ro_number || "").trim(); await fs.ensureDir(path.join(FolderPaths.Jobs, ro_number)); const filesList: fs.Dirent[] = ( await fs.readdir(path.join(FolderPaths.Jobs, ro_number), { withFileTypes: true, }) ).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name)); const thumbnailGenerationQueue: Promise[] = []; const ret: MediaFile[] = filesList.map((file) => { thumbnailGenerationQueue.push( GenerateThumbnail( path.join(FolderPaths.Jobs, ro_number, file.name), path.join( FolderPaths.Jobs, ro_number, FolderPaths.ThumbsSubDir, file.name ) ) ); return { src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, ro_number, file.name, ]), thumb: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, ro_number, FolderPaths.ThumbsSubDir, file.name, ]), }; }); await Promise.all(thumbnailGenerationQueue); res.json(ret); } interface MediaFile { src: string; thumb: string; }