Jobs Media List functional

This commit is contained in:
Patrick Fic
2022-04-28 19:30:49 -07:00
parent 295e4deeb2
commit 9962ce0baf
11 changed files with 4116 additions and 4624 deletions

View File

@@ -0,0 +1,15 @@
import { Request, Response, NextFunction } from "express";
export default function ValidateJobBasedRequest(
req: Request,
res: Response,
next: NextFunction
) {
const ro_number: string = (req.body.ro_number || "").trim();
if (ro_number === "") {
res.status(400).json({ error: "No RO Number has been specified." });
return;
} else {
next();
}
}

View File

@@ -1,9 +0,0 @@
import fs from "fs-extra";
//import { FolderPaths } from "../util/serverInit.js";
import express, { Express, Request, Response } from "express";
export async function JobsListMedia(req: Request, res: Response) {
//const { ro_number } = req.body;
res.send();
}

55
jobs/jobsListMedia.ts Normal file
View File

@@ -0,0 +1,55 @@
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();
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<void>[] = [];
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;
}

4535
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -15,11 +15,13 @@
"cors": "2.8.5",
"dotenv": "10.0.0",
"express": "^4.17.3",
"fs-extra": "^10.1.0"
"fs-extra": "^10.1.0",
"image-thumbnail": "^1.0.14"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/fs-extra": "^9.0.13",
"@types/image-thumbnail": "^1.0.1",
"@types/node": "^16.11.32",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0",

View File

@@ -1,8 +1,10 @@
import express, { Express, Request, Response } from "express";
import dotenv from "dotenv";
import { resolve } from "path";
import InitServer from "./util/serverInit";
import { JobsListMedia } from "./jobs/jobs.get";
import path, { resolve } from "path";
import InitServer, { FolderPaths } from "./util/serverInit";
import { JobsListMedia } from "./jobs/jobsListMedia";
import bodyParser from "body-parser";
import JobRequestValidator from "./jobs/jobRequestValidator";
dotenv.config({
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
@@ -10,11 +12,15 @@ dotenv.config({
const app: Express = express();
const port = process.env.PORT;
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(FolderPaths.StaticPath, express.static(FolderPaths.Root));
app.get("/", JobsListMedia);
app.get("/jobs/list", JobRequestValidator, JobsListMedia);
app.post("/jobs/upload", JobRequestValidator, JobsListMedia);
InitServer();
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at https://localhost:${port}`);
console.log(`ImEX Media Server is running at https://localhost:${port}`);
});

3
util/MediaUrlGen.ts Normal file
View File

@@ -0,0 +1,3 @@
export default function GenerateUrl(components: string[]) {
return components.join("/").replace(/([^:]\/)\/+/g, "$1");
}

30
util/generateThumbnail.ts Normal file
View File

@@ -0,0 +1,30 @@
import fs from "fs-extra";
import { access } from "fs/promises";
import imageThumbnail from "image-thumbnail";
import path from "path";
export default async function GenerateThumbnail(
file: string,
thumbPath: string
) {
try {
//Ensure the thumbs directory exists.
await fs.ensureDir(path.dirname(thumbPath));
try {
await access(thumbPath);
return;
return;
} catch {}
const thumbnail = await imageThumbnail(file, {
responseType: "buffer",
height: 250,
width: 250,
});
await fs.writeFile(thumbPath, thumbnail);
} catch (err) {
console.error("Error when genenerating thumbnail:", thumbPath);
}
}

View File

@@ -8,11 +8,16 @@ dotenv.config({
});
const RootDirectory = process.env.MEDIA_PATH!.replace("~", os.homedir);
const JobsFolder = "Jobs";
const VendorsFolder = "Vendors";
export const FolderPaths = {
Root: RootDirectory,
Jobs: path.join(RootDirectory, "Jobs"),
Vendors: path.join(RootDirectory, "Vendors"),
Jobs: path.join(RootDirectory, JobsFolder),
Vendors: path.join(RootDirectory, VendorsFolder),
ThumbsSubDir: "/thumbs",
StaticPath: "/static",
JobsFolder,
VendorsFolder,
};
export default function InitServer() {

1447
yarn-error.log Normal file

File diff suppressed because it is too large Load Diff

2617
yarn.lock

File diff suppressed because it is too large Load Diff