46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
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 ret: MediaFile[] = await Promise.all(
|
|
filesList.map(async (file) => {
|
|
const thumbPath: string = await GenerateThumbnail(
|
|
path.join(FolderPaths.Jobs, ro_number, file.name)
|
|
);
|
|
return {
|
|
src: GenerateUrl([
|
|
FolderPaths.StaticPath,
|
|
FolderPaths.JobsFolder,
|
|
ro_number,
|
|
file.name,
|
|
]),
|
|
thumb: GenerateUrl([
|
|
FolderPaths.StaticPath,
|
|
FolderPaths.JobsFolder,
|
|
ro_number,
|
|
thumbPath,
|
|
]),
|
|
};
|
|
})
|
|
);
|
|
|
|
res.json(ret);
|
|
}
|
|
|
|
interface MediaFile {
|
|
src: string;
|
|
thumb: string;
|
|
}
|