diff --git a/bills/billsListMedia.ts b/bills/billsListMedia.ts index 8908214..9a8b69d 100644 --- a/bills/billsListMedia.ts +++ b/bills/billsListMedia.ts @@ -1,12 +1,14 @@ import { Request, Response } from "express"; import fs from "fs-extra"; -import path from "path"; +import path, { relative } from "path"; import GenerateThumbnail from "../util/generateThumbnail"; import MediaFile from "../util/interfaces/MediaFile"; import ListableChecker from "../util/listableChecker"; import GenerateUrl from "../util/MediaUrlGen"; import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators"; import { FolderPaths } from "../util/serverInit"; +import core from "file-type/core"; +import ft from "file-type"; /** @description Bills will use the hierarchy of PDFs stored under the Job first, and then the Bills folder. */ export async function BillsListMedia(req: Request, res: Response) { @@ -20,10 +22,19 @@ export async function BillsListMedia(req: Request, res: Response) { if (req.files) { ret = await Promise.all( (req.files as Express.Multer.File[]).map(async (file) => { + const relativeFilePath: string = path.join( + PathToRoBillsFolder(jobid), + file.filename + ); + const relativeThumbPath: string = await GenerateThumbnail( - path.join(PathToRoBillsFolder(jobid), file.filename) + relativeFilePath + ); + const type: core.FileTypeResult | undefined = await ft.fromFile( + relativeFilePath ); return { + type, src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, @@ -61,10 +72,20 @@ export async function BillsListMedia(req: Request, res: Response) { ret = await Promise.all( filesList.map(async (file) => { - const relativeThumbPath: string = await GenerateThumbnail( - path.join(PathToRoBillsFolder(jobid), file.name) + const relativeFilePath: string = path.join( + PathToRoBillsFolder(jobid), + file.name ); + + const relativeThumbPath: string = await GenerateThumbnail( + relativeFilePath + ); + const type: core.FileTypeResult | undefined = await ft.fromFile( + relativeFilePath + ); + return { + type, src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, diff --git a/jobs/jobsListMedia.ts b/jobs/jobsListMedia.ts index e3a5544..9ad1aa5 100644 --- a/jobs/jobsListMedia.ts +++ b/jobs/jobsListMedia.ts @@ -8,6 +8,8 @@ import ListableChecker from "../util/listableChecker"; import GenerateUrl from "../util/MediaUrlGen"; import { PathToRoFolder } from "../util/pathGenerators"; import { FolderPaths, JobRelativeFilePath } from "../util/serverInit"; +import ft from "file-type"; +import core from "file-type/core"; export async function JobsListMedia(req: Request, res: Response) { const jobid: string = (req.body.jobid || "").trim(); @@ -27,7 +29,13 @@ export async function JobsListMedia(req: Request, res: Response) { const relativeThumbPath: string = await GenerateThumbnail( relativeFilePath ); + + const type: core.FileTypeResult | undefined = await ft.fromFile( + relativeFilePath + ); + return { + type, src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, @@ -64,7 +72,12 @@ export async function JobsListMedia(req: Request, res: Response) { const relativeThumbPath: string = await GenerateThumbnail( relativeFilePath ); + const type: core.FileTypeResult | undefined = await ft.fromFile( + relativeFilePath + ); + return { + type, src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, diff --git a/package.json b/package.json index 849c855..046cd71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bodyshop-media-server", - "version": "1.0.1", + "version": "1.0.3", "license": "UNLICENSED", "engines": { "node": "16.15.0" diff --git a/util/generateThumbnail.ts b/util/generateThumbnail.ts index 3483050..e3cc238 100644 --- a/util/generateThumbnail.ts +++ b/util/generateThumbnail.ts @@ -71,8 +71,9 @@ async function GeneratePdfThumbnail(file: string, thumbPath: string) { const fileOnDisk: Buffer = await fs.readFile(file); return new Promise((resolve, reject) => { const result = gm(fileOnDisk) + .selectFrame(0) .setFormat("png") - .resize(200) // Resize to fixed 200px width, maintaining aspect ratio + .resize(200, 200, "!") // Resize to fixed 200px width, maintaining aspect ratio .quality(75) .write(thumbPath, (error) => { if (error) reject(error.message);