diff --git a/src/utils/generateUniqueFileName.js b/src/utils/generateUniqueFilename.js similarity index 81% rename from src/utils/generateUniqueFileName.js rename to src/utils/generateUniqueFilename.js index c0161c1..86a1808 100644 --- a/src/utils/generateUniqueFileName.js +++ b/src/utils/generateUniqueFilename.js @@ -1,6 +1,15 @@ import fs from 'fs/promises'; import path from 'path'; +/** + * Utility to normalize paths to use forward slashes + * @param {string} p + * @return {string} + */ +function normalizeToForwardSlash(p) { + return p.split(path.sep).join('/'); +} + /** * Generates a unique filename based on the original filename, ensuring it doesn't overwrite existing files. * @param {string} directory - The directory to save the downloaded image. @@ -23,7 +32,7 @@ const generateUniqueFilename = async (directory, filename) => { counter += 1; } - return filePath; + return normalizeToForwardSlash(filePath); }; export default generateUniqueFilename; diff --git a/src/utils/renameAudioFileTitle.js b/src/utils/renameAudioFileTitle.js index 243cecf..ce7c140 100644 --- a/src/utils/renameAudioFileTitle.js +++ b/src/utils/renameAudioFileTitle.js @@ -2,6 +2,15 @@ import path from 'path'; import fs from 'fs/promises'; import generateUniqueFilename from './generateUniqueFilename.js'; +/** + * Utility to normalize paths to use forward slashes + * @param {string} p + * @return {string} + */ +function normalizeToForwardSlash(p) { + return p.split(path.sep).join('/'); +} + /** * Renames an audio file by its metadata properties: artist and title. * @@ -22,11 +31,11 @@ export default async function renameFile(artist, title, filePath) { const proposedFileName = `${artist} - ${title}${ext}`; // Generate a unique filename to avoid overwriting - const newFilePath = await generateUniqueFilename(dir, proposedFileName); + const newFilePath = await generateUniqueFilename(normalizeToForwardSlash(dir), normalizeToForwardSlash(proposedFileName)); try { await fs.rename(filePath, newFilePath); - return newFilePath; // Return the new path after successful rename + return normalizeToForwardSlash(newFilePath); // Return the new path after successful rename } catch (error) { console.error(`Error renaming file: ${error.message}`); throw error; // Rethrow the error for the caller to handle