Add logging, pdf thumbs
This commit is contained in:
@@ -15,6 +15,12 @@ RUN npm install
|
|||||||
# Bundle app source
|
# Bundle app source
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y \
|
||||||
|
imagemagick \
|
||||||
|
ghostscript \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|||||||
0
IMS/combined.log
Normal file
0
IMS/combined.log
Normal file
@@ -14,37 +14,27 @@ export async function JobsListMedia(req: Request, res: Response) {
|
|||||||
})
|
})
|
||||||
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
|
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
|
||||||
|
|
||||||
const thumbnailGenerationQueue: Promise<void>[] = [];
|
const ret: MediaFile[] = await Promise.all(
|
||||||
|
filesList.map(async (file) => {
|
||||||
const ret: MediaFile[] = filesList.map((file) => {
|
const thumbPath: string = await GenerateThumbnail(
|
||||||
thumbnailGenerationQueue.push(
|
path.join(FolderPaths.Jobs, ro_number, file.name)
|
||||||
GenerateThumbnail(
|
);
|
||||||
path.join(FolderPaths.Jobs, ro_number, file.name),
|
return {
|
||||||
path.join(
|
src: GenerateUrl([
|
||||||
FolderPaths.Jobs,
|
FolderPaths.StaticPath,
|
||||||
|
FolderPaths.JobsFolder,
|
||||||
ro_number,
|
ro_number,
|
||||||
FolderPaths.ThumbsSubDir,
|
file.name,
|
||||||
file.name
|
]),
|
||||||
)
|
thumb: GenerateUrl([
|
||||||
)
|
FolderPaths.StaticPath,
|
||||||
);
|
FolderPaths.JobsFolder,
|
||||||
return {
|
ro_number,
|
||||||
src: GenerateUrl([
|
thumbPath,
|
||||||
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);
|
res.json(ret);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import multer from "multer";
|
import multer from "multer";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import { logger } from "../server";
|
||||||
import GenerateThumbnail from "../util/generateThumbnail";
|
import GenerateThumbnail from "../util/generateThumbnail";
|
||||||
import { FolderPaths } from "../util/serverInit";
|
import { FolderPaths } from "../util/serverInit";
|
||||||
import { JobsListMedia } from "./jobsListMedia";
|
import { JobsListMedia } from "./jobsListMedia";
|
||||||
@@ -13,7 +14,7 @@ export const JobMediaUploadMulter = multer({
|
|||||||
cb(null, DestinationFolder);
|
cb(null, DestinationFolder);
|
||||||
},
|
},
|
||||||
filename: function (req, file, cb) {
|
filename: function (req, file, cb) {
|
||||||
console.log(path.basename(file.originalname));
|
logger.info("Uploading file: ", path.basename(file.originalname));
|
||||||
cb(
|
cb(
|
||||||
null,
|
null,
|
||||||
`${file.originalname}-${Math.floor(Date.now() / 1000)}.${path.extname(
|
`${file.originalname}-${Math.floor(Date.now() / 1000)}.${path.extname(
|
||||||
@@ -34,21 +35,11 @@ export async function jobsUploadMedia(req: Request, res: Response) {
|
|||||||
message: "No file uploaded",
|
message: "No file uploaded",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const thumbnailGenerationQueue: Promise<void>[] = [];
|
const thumbnailGenerationQueue: Promise<string>[] = [];
|
||||||
|
|
||||||
//for each file.path, generate the thumbnail.
|
//for each file.path, generate the thumbnail.
|
||||||
(req.files as Express.Multer.File[]).forEach((file) => {
|
(req.files as Express.Multer.File[]).forEach((file) => {
|
||||||
thumbnailGenerationQueue.push(
|
thumbnailGenerationQueue.push(GenerateThumbnail(file.path));
|
||||||
GenerateThumbnail(
|
|
||||||
file.path,
|
|
||||||
path.join(
|
|
||||||
FolderPaths.Jobs,
|
|
||||||
ro_number,
|
|
||||||
FolderPaths.ThumbsSubDir,
|
|
||||||
file.originalname
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
await Promise.all(thumbnailGenerationQueue);
|
await Promise.all(thumbnailGenerationQueue);
|
||||||
|
|
||||||
|
|||||||
1169
package-lock.json
generated
1169
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -13,18 +13,26 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/multer": "^1.4.7",
|
"@types/multer": "^1.4.7",
|
||||||
"axios": "^0.24.0",
|
"axios": "^0.24.0",
|
||||||
|
"bluebird": "^3.7.2",
|
||||||
"body-parser": "^1.20.0",
|
"body-parser": "^1.20.0",
|
||||||
"cors": "2.8.5",
|
"cors": "2.8.5",
|
||||||
"dotenv": "10.0.0",
|
"dotenv": "10.0.0",
|
||||||
"express": "^4.17.3",
|
"express": "^4.17.3",
|
||||||
|
"file-type": "^16.5.3",
|
||||||
"fs-extra": "^10.1.0",
|
"fs-extra": "^10.1.0",
|
||||||
|
"gm": "^1.23.1",
|
||||||
"image-thumbnail": "^1.0.14",
|
"image-thumbnail": "^1.0.14",
|
||||||
"multer": "^1.4.4"
|
"morgan": "^1.10.0",
|
||||||
|
"multer": "^1.4.4",
|
||||||
|
"winston": "^3.7.2",
|
||||||
|
"winston-daily-rotate-file": "^4.6.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/express": "^4.17.13",
|
"@types/express": "^4.17.13",
|
||||||
"@types/fs-extra": "^9.0.13",
|
"@types/fs-extra": "^9.0.13",
|
||||||
|
"@types/gm": "^1.18.11",
|
||||||
"@types/image-thumbnail": "^1.0.1",
|
"@types/image-thumbnail": "^1.0.1",
|
||||||
|
"@types/morgan": "^1.9.3",
|
||||||
"@types/node": "^16.11.32",
|
"@types/node": "^16.11.32",
|
||||||
"nodemon": "^2.0.15",
|
"nodemon": "^2.0.15",
|
||||||
"ts-node": "^10.7.0",
|
"ts-node": "^10.7.0",
|
||||||
|
|||||||
82
server.ts
82
server.ts
@@ -1,26 +1,101 @@
|
|||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import express, { Express } from "express";
|
import express, { Express } from "express";
|
||||||
|
import winston from "winston";
|
||||||
import { resolve } from "path";
|
import path, { resolve } from "path";
|
||||||
import JobRequestValidator from "./jobs/jobRequestValidator";
|
import JobRequestValidator from "./jobs/jobRequestValidator";
|
||||||
import { JobsListMedia } from "./jobs/jobsListMedia";
|
import { JobsListMedia } from "./jobs/jobsListMedia";
|
||||||
import { JobsMoveMedia } from "./jobs/jobsMoveMedia";
|
import { JobsMoveMedia } from "./jobs/jobsMoveMedia";
|
||||||
import { JobMediaUploadMulter, jobsUploadMedia } from "./jobs/jobsUploadMedia";
|
import { JobMediaUploadMulter, jobsUploadMedia } from "./jobs/jobsUploadMedia";
|
||||||
import InitServer, { FolderPaths } from "./util/serverInit";
|
import InitServer, { FolderPaths } from "./util/serverInit";
|
||||||
|
import DailyRotateFile from "winston-daily-rotate-file";
|
||||||
|
import morgan from "morgan";
|
||||||
|
|
||||||
dotenv.config({
|
dotenv.config({
|
||||||
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
|
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const logger = winston.createLogger({
|
||||||
|
format: winston.format.prettyPrint(),
|
||||||
|
level: "http",
|
||||||
|
levels: { ...winston.config.syslog.levels, http: 8 },
|
||||||
|
exceptionHandlers: [
|
||||||
|
new DailyRotateFile({
|
||||||
|
filename: path.join(FolderPaths.Root, "logs", "exceptions-%DATE%.log"),
|
||||||
|
datePattern: "YYYY-MM-DD-HH",
|
||||||
|
zippedArchive: true,
|
||||||
|
maxSize: "20m",
|
||||||
|
maxFiles: "14d",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
rejectionHandlers: [
|
||||||
|
new DailyRotateFile({
|
||||||
|
filename: path.join(FolderPaths.Root, "logs", "rejections-%DATE%.log"),
|
||||||
|
datePattern: "YYYY-MM-DD-HH",
|
||||||
|
zippedArchive: true,
|
||||||
|
maxSize: "20m",
|
||||||
|
maxFiles: "14d",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
transports: [
|
||||||
|
new DailyRotateFile({
|
||||||
|
filename: path.join(FolderPaths.Root, "logs", "errors-%DATE%.log"),
|
||||||
|
datePattern: "YYYY-MM-DD-HH",
|
||||||
|
zippedArchive: true,
|
||||||
|
maxSize: "20m",
|
||||||
|
maxFiles: "14d",
|
||||||
|
level: "error",
|
||||||
|
}),
|
||||||
|
|
||||||
|
new DailyRotateFile({
|
||||||
|
filename: path.join(FolderPaths.Root, "logs", "debug-%DATE%.log"),
|
||||||
|
datePattern: "YYYY-MM-DD-HH",
|
||||||
|
zippedArchive: true,
|
||||||
|
maxSize: "20m",
|
||||||
|
maxFiles: "14d",
|
||||||
|
level: "debug",
|
||||||
|
}),
|
||||||
|
new DailyRotateFile({
|
||||||
|
filename: path.join(FolderPaths.Root, "logs", "ALL-%DATE%.log"),
|
||||||
|
datePattern: "YYYY-MM-DD-HH",
|
||||||
|
zippedArchive: true,
|
||||||
|
maxSize: "20m",
|
||||||
|
maxFiles: "14d",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== "production") {
|
||||||
|
logger.add(
|
||||||
|
new winston.transports.Console({
|
||||||
|
format: winston.format.combine(
|
||||||
|
winston.format.colorize(),
|
||||||
|
winston.format.simple()
|
||||||
|
),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const app: Express = express();
|
const app: Express = express();
|
||||||
const port = process.env.PORT;
|
const port = process.env.PORT;
|
||||||
app.use(bodyParser.json({ limit: "50mb" }));
|
app.use(bodyParser.json({ limit: "50mb" }));
|
||||||
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
|
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
|
||||||
app.use(FolderPaths.StaticPath, express.static(FolderPaths.Root));
|
app.use(FolderPaths.StaticPath, express.static(FolderPaths.Root));
|
||||||
|
|
||||||
|
const morganMiddleware = morgan(
|
||||||
|
"combined", //":method :url :status :res[content-length] - :response-time ms"
|
||||||
|
{
|
||||||
|
stream: {
|
||||||
|
write: (message) => logger.http(message.trim()),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
app.use(morganMiddleware);
|
||||||
|
|
||||||
app.get("/jobs/list", JobRequestValidator, JobsListMedia);
|
app.get("/jobs/list", JobRequestValidator, JobsListMedia);
|
||||||
//Chaining together the upload. When upload is succesful, everything for the RO is returned.
|
//Chaining together the upload. When upload is succesful, everything for the RO is returned.
|
||||||
|
|
||||||
app.post(
|
app.post(
|
||||||
"/jobs/upload", //JobRequestValidator,
|
"/jobs/upload", //JobRequestValidator,
|
||||||
JobMediaUploadMulter.array("file"),
|
JobMediaUploadMulter.array("file"),
|
||||||
@@ -28,12 +103,11 @@ app.post(
|
|||||||
);
|
);
|
||||||
app.post(
|
app.post(
|
||||||
"/jobs/move", //JobRequestValidator,
|
"/jobs/move", //JobRequestValidator,
|
||||||
|
|
||||||
JobsMoveMedia
|
JobsMoveMedia
|
||||||
);
|
);
|
||||||
|
|
||||||
InitServer();
|
InitServer();
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`ImEX Media Server is running at http://localhost:${port}`);
|
logger.info(`ImEX Media Server is running at http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,29 +2,73 @@ import fs from "fs-extra";
|
|||||||
import { access } from "fs/promises";
|
import { access } from "fs/promises";
|
||||||
import imageThumbnail from "image-thumbnail";
|
import imageThumbnail from "image-thumbnail";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import gm from "gm";
|
||||||
|
import ft from "file-type";
|
||||||
|
import core from "file-type/core";
|
||||||
|
import GenerateUrl from "./MediaUrlGen";
|
||||||
|
import { FolderPaths } from "./serverInit";
|
||||||
|
var Bluebird = require("bluebird");
|
||||||
|
|
||||||
|
Bluebird.promisifyAll(gm.prototype);
|
||||||
|
|
||||||
|
/** @returns {string} Returns the relative path from the file to the thumbnail on the server. This must be converted to a URL. */
|
||||||
export default async function GenerateThumbnail(
|
export default async function GenerateThumbnail(
|
||||||
file: string,
|
file: string
|
||||||
thumbPath: string
|
//thumbPath: string
|
||||||
) {
|
) {
|
||||||
|
const type: core.FileTypeResult | undefined = await ft.fromFile(file);
|
||||||
|
let thumbnailExtension: string = GetThumbnailExtension(type);
|
||||||
|
let thumbPath: string = path.join(
|
||||||
|
path.dirname(file),
|
||||||
|
FolderPaths.ThumbsSubDir,
|
||||||
|
path.parse(path.basename(file)).name + thumbnailExtension
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//Ensure the thumbs directory exists.
|
//Ensure the thumbs directory exists.
|
||||||
await fs.ensureDir(path.dirname(thumbPath));
|
await fs.ensureDir(path.dirname(thumbPath));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await access(thumbPath);
|
await access(thumbPath);
|
||||||
return;
|
return path.relative(path.dirname(file), thumbPath);
|
||||||
return;
|
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
const thumbnail = await imageThumbnail(file, {
|
//Check to see if the file is an image, PDF, or video.
|
||||||
responseType: "buffer",
|
|
||||||
height: 250,
|
|
||||||
width: 250,
|
|
||||||
});
|
|
||||||
|
|
||||||
await fs.writeFile(thumbPath, thumbnail);
|
if (type?.mime === "application/pdf") {
|
||||||
|
const fileOnDisk: Buffer = await fs.readFile(file);
|
||||||
|
await GeneratePdfThumbnail(file, thumbPath);
|
||||||
|
} else {
|
||||||
|
const thumbnail = await imageThumbnail(file, {
|
||||||
|
responseType: "buffer",
|
||||||
|
height: 250,
|
||||||
|
width: 250,
|
||||||
|
});
|
||||||
|
|
||||||
|
await fs.writeFile(thumbPath, thumbnail);
|
||||||
|
}
|
||||||
|
return path.relative(path.dirname(file), thumbPath);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error when genenerating thumbnail:", thumbPath);
|
console.error("Error when genenerating thumbnail:", thumbPath);
|
||||||
|
return path.relative(path.dirname(file), thumbPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function GeneratePdfThumbnail(file: string, thumbPath: string) {
|
||||||
|
const fileOnDisk: Buffer = await fs.readFile(file);
|
||||||
|
return new Promise<string>((resolve, reject) => {
|
||||||
|
const result = gm(fileOnDisk)
|
||||||
|
.setFormat("png")
|
||||||
|
.resize(200) // Resize to fixed 200px width, maintaining aspect ratio
|
||||||
|
.quality(75)
|
||||||
|
.write(thumbPath, (error) => {
|
||||||
|
if (error) reject(error.message);
|
||||||
|
resolve(thumbPath);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function GetThumbnailExtension(file: core.FileTypeResult | undefined) {
|
||||||
|
if (file === undefined) return ".png";
|
||||||
|
return ".png";
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import fs = require("fs-extra");
|
|||||||
import path, { resolve } from "path";
|
import path, { resolve } from "path";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
|
import { logger } from "../server";
|
||||||
|
|
||||||
dotenv.config({
|
dotenv.config({
|
||||||
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
|
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
|
||||||
@@ -21,10 +22,11 @@ export const FolderPaths = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function InitServer() {
|
export default function InitServer() {
|
||||||
console.log(`Ensuring Root media path exists: ${FolderPaths.Root}`);
|
logger.info(`Ensuring Root media path exists: ${FolderPaths.Root}`);
|
||||||
fs.ensureDirSync(FolderPaths.Root);
|
fs.ensureDirSync(FolderPaths.Root);
|
||||||
console.log(`Ensuring Jobs media path exists: ${FolderPaths.Jobs}`);
|
logger.info(`Ensuring Jobs media path exists: ${FolderPaths.Jobs}`);
|
||||||
fs.ensureDirSync(FolderPaths.Jobs);
|
fs.ensureDirSync(FolderPaths.Jobs);
|
||||||
console.log(`Ensuring Vendors media path exists: ${FolderPaths.Vendors}`);
|
logger.info(`Ensuring Vendors media path exists: ${FolderPaths.Vendors}`);
|
||||||
fs.ensureDirSync(FolderPaths.Vendors);
|
fs.ensureDirSync(FolderPaths.Vendors);
|
||||||
|
logger.info("Folder Paths", FolderPaths);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user