mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
* fix: handle webp images correctly * refactor: use the userPath from the start of the filecycle to avoid handling the blob, whose loading may fail upon user request * refactor: delete temp files on reload and new chat
58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
const { z } = require('zod');
|
|
const fs = require('fs').promises;
|
|
const express = require('express');
|
|
const upload = require('./multer');
|
|
const { localStrategy } = require('~/server/services/Files');
|
|
|
|
const router = express.Router();
|
|
|
|
router.post('/', upload.single('file'), async (req, res) => {
|
|
const file = req.file;
|
|
const metadata = req.body;
|
|
// TODO: add file size/type validation
|
|
|
|
const uuidSchema = z.string().uuid();
|
|
|
|
try {
|
|
if (!file) {
|
|
throw new Error('No file provided');
|
|
}
|
|
|
|
if (!metadata.file_id) {
|
|
throw new Error('No file_id provided');
|
|
}
|
|
|
|
if (!metadata.width) {
|
|
throw new Error('No width provided');
|
|
}
|
|
|
|
if (!metadata.height) {
|
|
throw new Error('No height provided');
|
|
}
|
|
/* parse to validate api call */
|
|
uuidSchema.parse(metadata.file_id);
|
|
metadata.temp_file_id = metadata.file_id;
|
|
metadata.file_id = req.file_id;
|
|
await localStrategy({ req, res, file, metadata });
|
|
} catch (error) {
|
|
console.error('Error processing file:', error);
|
|
try {
|
|
await fs.unlink(file.path);
|
|
} catch (error) {
|
|
console.error('Error deleting file:', error);
|
|
}
|
|
res.status(500).json({ message: 'Error processing file' });
|
|
}
|
|
|
|
// do this if strategy is not local
|
|
// finally {
|
|
// try {
|
|
// // await fs.unlink(file.path);
|
|
// } catch (error) {
|
|
// console.error('Error deleting file:', error);
|
|
|
|
// }
|
|
// }
|
|
});
|
|
|
|
module.exports = router;
|