* Decrypts a file and returns the content * Handles both encrypted and unencrypted (legacy) files * @param {string} filePath - Path to the file * @returns {Promise } - Decrypted file content as a Buffer
(filePath)
| 33 | * @returns {Promise<Buffer>} - Decrypted file content as a Buffer |
| 34 | */ |
| 35 | async function decryptFile(filePath) { |
| 36 | try { |
| 37 | // Read the file |
| 38 | const fileContent = await fs.readFile(filePath, "utf8"); |
| 39 | |
| 40 | try { |
| 41 | // Try to decrypt assuming it's encrypted |
| 42 | const decryptedContent = decrypt(fileContent); |
| 43 | return Buffer.from(decryptedContent, "base64"); |
| 44 | } catch (decryptError) { |
| 45 | // If decryption fails, assume it's an unencrypted legacy file |
| 46 | // and return the content directly as a buffer |
| 47 | return Buffer.from(fileContent); |
| 48 | } |
| 49 | } catch (error) { |
| 50 | console.error(`Error reading/decrypting file ${filePath}:`, error); // oxlint-disable-line no-console |
| 51 | throw error; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Synchronously decrypts a file and returns the content |