IO-2008 Include mime type for listed media.

This commit is contained in:
Patrick Fic
2022-08-03 13:27:17 -07:00
parent 3d0791f633
commit 92e7e3ebda
4 changed files with 41 additions and 6 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -1,6 +1,6 @@
{
"name": "bodyshop-media-server",
"version": "1.0.1",
"version": "1.0.3",
"license": "UNLICENSED",
"engines": {
"node": "16.15.0"

View File

@@ -71,8 +71,9 @@ async function GeneratePdfThumbnail(file: string, thumbPath: string) {
const fileOnDisk: Buffer = await fs.readFile(file);
return new Promise<string>((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);