mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-23 03:40:14 +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 { getCustomConfig, getAppConfig } = require('~/server/services/Config');
|
||||||
|
|
||||||
const storage = multer.diskStorage({
|
const storage = multer.diskStorage({
|
||||||
destination: async function (req, file, cb) {
|
destination: function (req, file, cb) {
|
||||||
const appConfig = await getAppConfig({ role: req.user?.role });
|
getAppConfig({ role: req.user?.role })
|
||||||
const outputPath = path.join(appConfig.paths.uploads, 'temp', req.user.id);
|
.then((appConfig) => {
|
||||||
if (!fs.existsSync(outputPath)) {
|
const outputPath = path.join(appConfig.paths.uploads, 'temp', req.user.id);
|
||||||
fs.mkdirSync(outputPath, { recursive: true });
|
if (!fs.existsSync(outputPath)) {
|
||||||
}
|
fs.mkdirSync(outputPath, { recursive: true });
|
||||||
cb(null, outputPath);
|
}
|
||||||
|
cb(null, outputPath);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
cb(error);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
filename: function (req, file, cb) {
|
filename: function (req, file, cb) {
|
||||||
req.file_id = crypto.randomUUID();
|
req.file_id = crypto.randomUUID();
|
||||||
|
|
|
||||||
|
|
@ -479,21 +479,17 @@ describe('Multer Configuration', () => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
// Call getDestination which should fail due to permission/path issues
|
||||||
// Call getDestination which should fail due to permission/path issues
|
storage.getDestination(mockReq, mockFile, (err, destination) => {
|
||||||
storage.getDestination(mockReq, mockFile, (err, destination) => {
|
// Now we expect the error to be passed to the callback
|
||||||
// If callback is reached, we didn't get the expected error
|
expect(err).toBeDefined();
|
||||||
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) {
|
|
||||||
// This is the expected behavior - mkdirSync throws synchronously for invalid paths
|
// This is the expected behavior - mkdirSync throws synchronously for invalid paths
|
||||||
// On Linux, this typically returns EACCES (permission denied)
|
// On Linux, this typically returns EACCES (permission denied)
|
||||||
// On macOS/Darwin, this returns ENOENT (no such file or directory)
|
// 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();
|
done();
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle malformed filenames with real sanitization', (done) => {
|
it('should handle malformed filenames with real sanitization', (done) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue