Skip to content

Commit

Permalink
path API document mgmt with STORAGE_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycarambat committed Mar 21, 2024
1 parent 22ef2c2 commit 373c833
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions server/endpoints/api/document/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const { handleUploads } = setupMulter();
const fs = require("fs");
const path = require("path");
const { Document } = require("../../../models/documents");
const documentsPath =
process.env.NODE_ENV === "development"
? path.resolve(__dirname, "../../../storage/documents")
: path.resolve(process.env.STORAGE_DIR, `documents`);

function apiDocumentEndpoints(app) {
if (!app) return;
Expand Down Expand Up @@ -600,11 +604,7 @@ function apiDocumentEndpoints(app) {
*/
try {
const { name } = reqBody(request);
const storagePath = path.join(
__dirname,
"../../../storage/documents",
normalizePath(name)
);
const storagePath = path.join(documentsPath, normalizePath(name));

if (fs.existsSync(storagePath)) {
response.status(500).json({
Expand Down Expand Up @@ -681,16 +681,8 @@ function apiDocumentEndpoints(app) {
({ from }) => !embeddedFiles.includes(from)
);
const movePromises = moveableFiles.map(({ from, to }) => {
const sourcePath = path.join(
__dirname,
"../../../storage/documents",
normalizePath(from)
);
const destinationPath = path.join(
__dirname,
"../../../storage/documents",
normalizePath(to)
);
const sourcePath = path.join(documentsPath, normalizePath(from));
const destinationPath = path.join(documentsPath, normalizePath(to));
return new Promise((resolve, reject) => {
fs.rename(sourcePath, destinationPath, (err) => {
if (err) {
Expand Down

0 comments on commit 373c833

Please sign in to comment.