From 7f3dd2c93aaec88a7a4d651c38ed44fa7d32a096 Mon Sep 17 00:00:00 2001 From: xtrullor73 Date: Sat, 25 May 2024 00:43:04 -0700 Subject: [PATCH] feat: implement saveImageToFile to save downloaded images in a given directory --- src/utils/saveImageToFile.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/utils/saveImageToFile.js diff --git a/src/utils/saveImageToFile.js b/src/utils/saveImageToFile.js new file mode 100644 index 0000000..3f151f5 --- /dev/null +++ b/src/utils/saveImageToFile.js @@ -0,0 +1,19 @@ +import path from 'path'; // Replace with your actual path +import downloadImageToFile from './downloadImageToFile.js'; + +export default async function saveImageToFile(imageUrl, outputDirectory) { + try { + // Create a safe file name + const imageName = path.basename(imageUrl); // This may need to be adjusted for unique naming + const savePath = path.join(outputDirectory, imageName); + + // Use the updated function to download and get the path to the saved image + const savedImagePath = await downloadImageToFile(imageUrl, savePath); + + console.log('Image saved to', savedImagePath); + return savedImagePath; // Return the saved image path for further use + } catch (error) { + console.error('Error writing image to file:', error); + throw error; + } +}