Patch v1/document/upload filename charset encoding (#2535)

This commit is contained in:
Timothy Carambat 2024-10-25 12:03:19 -07:00 committed by GitHub
parent 72ba9f7f28
commit 40800631e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 7 deletions

View File

@ -1,6 +1,6 @@
const { Telemetry } = require("../../../models/telemetry"); const { Telemetry } = require("../../../models/telemetry");
const { validApiKey } = require("../../../utils/middleware/validApiKey"); const { validApiKey } = require("../../../utils/middleware/validApiKey");
const { handleFileUpload } = require("../../../utils/files/multer"); const { handleAPIFileUpload } = require("../../../utils/files/multer");
const { const {
viewLocalFiles, viewLocalFiles,
findDocumentInDocuments, findDocumentInDocuments,
@ -23,7 +23,7 @@ function apiDocumentEndpoints(app) {
app.post( app.post(
"/v1/document/upload", "/v1/document/upload",
[validApiKey, handleFileUpload], [validApiKey, handleAPIFileUpload],
async (request, response) => { async (request, response) => {
/* /*
#swagger.tags = ['Documents'] #swagger.tags = ['Documents']

View File

@ -3,7 +3,10 @@ const path = require("path");
const fs = require("fs"); const fs = require("fs");
const { v4 } = require("uuid"); 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({ const fileUploadStorage = multer.diskStorage({
destination: function (_, __, cb) { destination: function (_, __, cb) {
const uploadOutput = const uploadOutput =
@ -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 // Asset storage for logos
const assetUploadStorage = multer.diskStorage({ const assetUploadStorage = multer.diskStorage({
destination: function (_, __, cb) { destination: function (_, __, cb) {
@ -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({ const pfpUploadStorage = multer.diskStorage({
destination: function (_, __, cb) { destination: function (_, __, cb) {
const uploadOutput = const uploadOutput =
@ -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) { function handleFileUpload(request, response, next) {
const upload = multer({ storage: fileUploadStorage }).single("file"); const upload = multer({ storage: fileUploadStorage }).single("file");
upload(request, response, function (err) { upload(request, response, function (err) {
@ -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) { function handleAssetUpload(request, response, next) {
const upload = multer({ storage: assetUploadStorage }).single("logo"); const upload = multer({ storage: assetUploadStorage }).single("logo");
upload(request, response, function (err) { upload(request, response, function (err) {
@ -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) { function handlePfpUpload(request, response, next) {
const upload = multer({ storage: pfpUploadStorage }).single("file"); const upload = multer({ storage: pfpUploadStorage }).single("file");
upload(request, response, function (err) { upload(request, response, function (err) {
@ -111,6 +166,7 @@ function handlePfpUpload(request, response, next) {
module.exports = { module.exports = {
handleFileUpload, handleFileUpload,
handleAPIFileUpload,
handleAssetUpload, handleAssetUpload,
handlePfpUpload, handlePfpUpload,
}; };