IO-2008 Include mime type for listed media.
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
import path from "path";
|
import path, { relative } from "path";
|
||||||
import GenerateThumbnail from "../util/generateThumbnail";
|
import GenerateThumbnail from "../util/generateThumbnail";
|
||||||
import MediaFile from "../util/interfaces/MediaFile";
|
import MediaFile from "../util/interfaces/MediaFile";
|
||||||
import ListableChecker from "../util/listableChecker";
|
import ListableChecker from "../util/listableChecker";
|
||||||
import GenerateUrl from "../util/MediaUrlGen";
|
import GenerateUrl from "../util/MediaUrlGen";
|
||||||
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators";
|
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators";
|
||||||
import { FolderPaths } from "../util/serverInit";
|
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. */
|
/** @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) {
|
export async function BillsListMedia(req: Request, res: Response) {
|
||||||
@@ -20,10 +22,19 @@ export async function BillsListMedia(req: Request, res: Response) {
|
|||||||
if (req.files) {
|
if (req.files) {
|
||||||
ret = await Promise.all(
|
ret = await Promise.all(
|
||||||
(req.files as Express.Multer.File[]).map(async (file) => {
|
(req.files as Express.Multer.File[]).map(async (file) => {
|
||||||
|
const relativeFilePath: string = path.join(
|
||||||
|
PathToRoBillsFolder(jobid),
|
||||||
|
file.filename
|
||||||
|
);
|
||||||
|
|
||||||
const relativeThumbPath: string = await GenerateThumbnail(
|
const relativeThumbPath: string = await GenerateThumbnail(
|
||||||
path.join(PathToRoBillsFolder(jobid), file.filename)
|
relativeFilePath
|
||||||
|
);
|
||||||
|
const type: core.FileTypeResult | undefined = await ft.fromFile(
|
||||||
|
relativeFilePath
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
|
type,
|
||||||
src: GenerateUrl([
|
src: GenerateUrl([
|
||||||
FolderPaths.StaticPath,
|
FolderPaths.StaticPath,
|
||||||
FolderPaths.JobsFolder,
|
FolderPaths.JobsFolder,
|
||||||
@@ -61,10 +72,20 @@ export async function BillsListMedia(req: Request, res: Response) {
|
|||||||
|
|
||||||
ret = await Promise.all(
|
ret = await Promise.all(
|
||||||
filesList.map(async (file) => {
|
filesList.map(async (file) => {
|
||||||
const relativeThumbPath: string = await GenerateThumbnail(
|
const relativeFilePath: string = path.join(
|
||||||
path.join(PathToRoBillsFolder(jobid), file.name)
|
PathToRoBillsFolder(jobid),
|
||||||
|
file.name
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const relativeThumbPath: string = await GenerateThumbnail(
|
||||||
|
relativeFilePath
|
||||||
|
);
|
||||||
|
const type: core.FileTypeResult | undefined = await ft.fromFile(
|
||||||
|
relativeFilePath
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type,
|
||||||
src: GenerateUrl([
|
src: GenerateUrl([
|
||||||
FolderPaths.StaticPath,
|
FolderPaths.StaticPath,
|
||||||
FolderPaths.JobsFolder,
|
FolderPaths.JobsFolder,
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import ListableChecker from "../util/listableChecker";
|
|||||||
import GenerateUrl from "../util/MediaUrlGen";
|
import GenerateUrl from "../util/MediaUrlGen";
|
||||||
import { PathToRoFolder } from "../util/pathGenerators";
|
import { PathToRoFolder } from "../util/pathGenerators";
|
||||||
import { FolderPaths, JobRelativeFilePath } from "../util/serverInit";
|
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) {
|
export async function JobsListMedia(req: Request, res: Response) {
|
||||||
const jobid: string = (req.body.jobid || "").trim();
|
const jobid: string = (req.body.jobid || "").trim();
|
||||||
@@ -27,7 +29,13 @@ export async function JobsListMedia(req: Request, res: Response) {
|
|||||||
const relativeThumbPath: string = await GenerateThumbnail(
|
const relativeThumbPath: string = await GenerateThumbnail(
|
||||||
relativeFilePath
|
relativeFilePath
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const type: core.FileTypeResult | undefined = await ft.fromFile(
|
||||||
|
relativeFilePath
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type,
|
||||||
src: GenerateUrl([
|
src: GenerateUrl([
|
||||||
FolderPaths.StaticPath,
|
FolderPaths.StaticPath,
|
||||||
FolderPaths.JobsFolder,
|
FolderPaths.JobsFolder,
|
||||||
@@ -64,7 +72,12 @@ export async function JobsListMedia(req: Request, res: Response) {
|
|||||||
const relativeThumbPath: string = await GenerateThumbnail(
|
const relativeThumbPath: string = await GenerateThumbnail(
|
||||||
relativeFilePath
|
relativeFilePath
|
||||||
);
|
);
|
||||||
|
const type: core.FileTypeResult | undefined = await ft.fromFile(
|
||||||
|
relativeFilePath
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type,
|
||||||
src: GenerateUrl([
|
src: GenerateUrl([
|
||||||
FolderPaths.StaticPath,
|
FolderPaths.StaticPath,
|
||||||
FolderPaths.JobsFolder,
|
FolderPaths.JobsFolder,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "bodyshop-media-server",
|
"name": "bodyshop-media-server",
|
||||||
"version": "1.0.1",
|
"version": "1.0.3",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "16.15.0"
|
"node": "16.15.0"
|
||||||
|
|||||||
@@ -71,8 +71,9 @@ async function GeneratePdfThumbnail(file: string, thumbPath: string) {
|
|||||||
const fileOnDisk: Buffer = await fs.readFile(file);
|
const fileOnDisk: Buffer = await fs.readFile(file);
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
const result = gm(fileOnDisk)
|
const result = gm(fileOnDisk)
|
||||||
|
.selectFrame(0)
|
||||||
.setFormat("png")
|
.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)
|
.quality(75)
|
||||||
.write(thumbPath, (error) => {
|
.write(thumbPath, (error) => {
|
||||||
if (error) reject(error.message);
|
if (error) reject(error.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user