Send back only new files on upload.

This commit is contained in:
Patrick Fic
2022-05-04 17:37:32 -07:00
parent b7be304520
commit 05a8c90f03
9 changed files with 6442 additions and 78 deletions

View File

@@ -10,33 +10,63 @@ 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(PathToRoFolder(ro_number));
const filesList: fs.Dirent[] = (
await fs.readdir(PathToRoFolder(ro_number), {
withFileTypes: true,
})
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
let ret: MediaFile[];
if (req.files) {
//We just uploaded files, we're going to send only those back.
ret = await Promise.all(
(req.files as Express.Multer.File[]).map(async (file) => {
const relativeThumbPath: string = await GenerateThumbnail(
path.join(FolderPaths.Jobs, ro_number, file.filename)
);
return {
src: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
ro_number,
file.filename,
]),
thumbnail: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
ro_number,
relativeThumbPath,
]),
thumbnailHeight: 250,
thumbnailWidth: 250,
};
})
);
} else {
const filesList: fs.Dirent[] = (
await fs.readdir(PathToRoFolder(ro_number), {
withFileTypes: true,
})
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
const ret: MediaFile[] = await Promise.all(
filesList.map(async (file) => {
const relativeThumbPath: 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,
relativeThumbPath,
]),
};
})
);
ret = await Promise.all(
filesList.map(async (file) => {
const relativeThumbPath: string = await GenerateThumbnail(
path.join(FolderPaths.Jobs, ro_number, file.name)
);
return {
src: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
ro_number,
file.name,
]),
thumbnail: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
ro_number,
relativeThumbPath,
]),
thumbnailHeight: 250,
thumbnailWidth: 250,
};
})
);
}
res.json(ret);
}