mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
🗃️ fix: Only Unlink Temp files on Error for Firebase File Uploads (#9152)
This commit is contained in:
parent
cee0579e0e
commit
3394aa5030
1 changed files with 22 additions and 9 deletions
|
|
@ -2,10 +2,10 @@ const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
const { logger } = require('@librechat/data-schemas');
|
||||||
const { ref, uploadBytes, getDownloadURL, deleteObject } = require('firebase/storage');
|
const { ref, uploadBytes, getDownloadURL, deleteObject } = require('firebase/storage');
|
||||||
const { getBufferMetadata } = require('~/server/utils');
|
const { getBufferMetadata } = require('~/server/utils');
|
||||||
const { getFirebaseStorage } = require('./initialize');
|
const { getFirebaseStorage } = require('./initialize');
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a file from Firebase Storage.
|
* Deletes a file from Firebase Storage.
|
||||||
|
|
@ -145,7 +145,10 @@ function extractFirebaseFilePath(urlString) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
} catch (error) {
|
} catch {
|
||||||
|
logger.debug(
|
||||||
|
'[extractFirebaseFilePath] Failed to extract Firebase file path from URL, returning empty string',
|
||||||
|
);
|
||||||
// If URL parsing fails, return an empty string
|
// If URL parsing fails, return an empty string
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
@ -211,14 +214,24 @@ async function uploadFileToFirebase({ req, file, file_id }) {
|
||||||
const inputBuffer = await fs.promises.readFile(inputFilePath);
|
const inputBuffer = await fs.promises.readFile(inputFilePath);
|
||||||
const bytes = Buffer.byteLength(inputBuffer);
|
const bytes = Buffer.byteLength(inputBuffer);
|
||||||
const userId = req.user.id;
|
const userId = req.user.id;
|
||||||
|
|
||||||
const fileName = `${file_id}__${path.basename(inputFilePath)}`;
|
const fileName = `${file_id}__${path.basename(inputFilePath)}`;
|
||||||
|
try {
|
||||||
const downloadURL = await saveBufferToFirebase({ userId, buffer: inputBuffer, fileName });
|
const downloadURL = await saveBufferToFirebase({ userId, buffer: inputBuffer, fileName });
|
||||||
|
return { filepath: downloadURL, bytes };
|
||||||
await fs.promises.unlink(inputFilePath);
|
} catch (err) {
|
||||||
|
logger.error('[uploadFileToFirebase] Error saving file buffer to Firebase:', err);
|
||||||
return { filepath: downloadURL, bytes };
|
try {
|
||||||
|
if (file && file.path) {
|
||||||
|
await fs.promises.unlink(file.path);
|
||||||
|
}
|
||||||
|
} catch (unlinkError) {
|
||||||
|
logger.error(
|
||||||
|
'[uploadFileToFirebase] Error deleting temporary file, likely already deleted:',
|
||||||
|
unlinkError.message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue