diff --git a/api/server/routes/files/multer.js b/api/server/routes/files/multer.js index c73bb6f010..3769cd14ae 100644 --- a/api/server/routes/files/multer.js +++ b/api/server/routes/files/multer.js @@ -7,13 +7,18 @@ const { fileConfig: defaultFileConfig, mergeFileConfig } = require('librechat-da const { getCustomConfig, getAppConfig } = require('~/server/services/Config'); const storage = multer.diskStorage({ - destination: async function (req, file, cb) { - const appConfig = await getAppConfig({ role: req.user?.role }); - const outputPath = path.join(appConfig.paths.uploads, 'temp', req.user.id); - if (!fs.existsSync(outputPath)) { - fs.mkdirSync(outputPath, { recursive: true }); - } - cb(null, outputPath); + destination: function (req, file, cb) { + getAppConfig({ role: req.user?.role }) + .then((appConfig) => { + const outputPath = path.join(appConfig.paths.uploads, 'temp', req.user.id); + if (!fs.existsSync(outputPath)) { + fs.mkdirSync(outputPath, { recursive: true }); + } + cb(null, outputPath); + }) + .catch((error) => { + cb(error); + }); }, filename: function (req, file, cb) { req.file_id = crypto.randomUUID(); diff --git a/api/server/routes/files/multer.spec.js b/api/server/routes/files/multer.spec.js index 9089e5e931..a5056dc4a2 100644 --- a/api/server/routes/files/multer.spec.js +++ b/api/server/routes/files/multer.spec.js @@ -479,21 +479,17 @@ describe('Multer Configuration', () => { }, }); - try { - // Call getDestination which should fail due to permission/path issues - storage.getDestination(mockReq, mockFile, (err, destination) => { - // If callback is reached, we didn't get the expected error - done(new Error('Expected mkdirSync to throw an error but callback was called')); - }); - // If we get here without throwing, something unexpected happened - done(new Error('Expected mkdirSync to throw an error but no error was thrown')); - } catch (error) { + // Call getDestination which should fail due to permission/path issues + storage.getDestination(mockReq, mockFile, (err, destination) => { + // Now we expect the error to be passed to the callback + expect(err).toBeDefined(); // This is the expected behavior - mkdirSync throws synchronously for invalid paths // On Linux, this typically returns EACCES (permission denied) // On macOS/Darwin, this returns ENOENT (no such file or directory) - expect(['EACCES', 'ENOENT']).toContain(error.code); + expect(['EACCES', 'ENOENT']).toContain(err.code); + expect(destination).toBeUndefined(); done(); - } + }); }); it('should handle malformed filenames with real sanitization', (done) => {