mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-22 19:30:15 +01:00
refactor: Update multer storage destination to use promise-based getAppConfig and improve error handling in tests
This commit is contained in:
parent
5bb731764c
commit
eeab69ff7f
2 changed files with 19 additions and 18 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue