Skip to content

Commit

Permalink
Patch v1/document/upload filename charset encoding (#2535)
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycarambat authored Oct 25, 2024
1 parent 72ba9f7 commit 4080063
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/endpoints/api/document/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Telemetry } = require("../../../models/telemetry");
const { validApiKey } = require("../../../utils/middleware/validApiKey");
const { handleFileUpload } = require("../../../utils/files/multer");
const { handleAPIFileUpload } = require("../../../utils/files/multer");
const {
viewLocalFiles,
findDocumentInDocuments,
Expand All @@ -23,7 +23,7 @@ function apiDocumentEndpoints(app) {

app.post(
"/v1/document/upload",
[validApiKey, handleFileUpload],
[validApiKey, handleAPIFileUpload],
async (request, response) => {
/*
#swagger.tags = ['Documents']
Expand Down
66 changes: 61 additions & 5 deletions server/utils/files/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const path = require("path");
const fs = require("fs");
const { v4 } = require("uuid");

// Handle File uploads for auto-uploading.
/**
* Handle File uploads for auto-uploading.
* Mostly used for internal GUI/API uploads.
*/
const fileUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
const uploadOutput =
Expand All @@ -20,6 +23,23 @@ const fileUploadStorage = multer.diskStorage({
},
});

/**
* Handle API file upload as documents - this does not manipulate the filename
* at all for encoding/charset reasons.
*/
const fileAPIUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
const uploadOutput =
process.env.NODE_ENV === "development"
? path.resolve(__dirname, `../../../collector/hotdir`)
: path.resolve(process.env.STORAGE_DIR, `../../collector/hotdir`);
cb(null, uploadOutput);
},
filename: function (_, file, cb) {
cb(null, file.originalname);
},
});

// Asset storage for logos
const assetUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
Expand All @@ -38,7 +58,9 @@ const assetUploadStorage = multer.diskStorage({
},
});

// Asset sub-storage manager for pfp icons.
/**
* Handle PFP file upload as logos
*/
const pfpUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
const uploadOutput =
Expand All @@ -55,7 +77,12 @@ const pfpUploadStorage = multer.diskStorage({
},
});

// Handle Generic file upload as documents
/**
* Handle Generic file upload as documents from the GUI
* @param {Request} request
* @param {Response} response
* @param {NextFunction} next
*/
function handleFileUpload(request, response, next) {
const upload = multer({ storage: fileUploadStorage }).single("file");
upload(request, response, function (err) {
Expand All @@ -73,7 +100,33 @@ function handleFileUpload(request, response, next) {
});
}

// Handle logo asset uploads
/**
* Handle API file upload as documents - this does not manipulate the filename
* at all for encoding/charset reasons.
* @param {Request} request
* @param {Response} response
* @param {NextFunction} next
*/
function handleAPIFileUpload(request, response, next) {
const upload = multer({ storage: fileAPIUploadStorage }).single("file");
upload(request, response, function (err) {
if (err) {
response
.status(500)
.json({
success: false,
error: `Invalid file upload. ${err.message}`,
})
.end();
return;
}
next();
});
}

/**
* Handle logo asset uploads
*/
function handleAssetUpload(request, response, next) {
const upload = multer({ storage: assetUploadStorage }).single("logo");
upload(request, response, function (err) {
Expand All @@ -91,7 +144,9 @@ function handleAssetUpload(request, response, next) {
});
}

// Handle PFP file upload as logos
/**
* Handle PFP file upload as logos
*/
function handlePfpUpload(request, response, next) {
const upload = multer({ storage: pfpUploadStorage }).single("file");
upload(request, response, function (err) {
Expand All @@ -111,6 +166,7 @@ function handlePfpUpload(request, response, next) {

module.exports = {
handleFileUpload,
handleAPIFileUpload,
handleAssetUpload,
handlePfpUpload,
};

0 comments on commit 4080063

Please sign in to comment.