fix(utils): normalize paths to use forward slashes for consistent platform support

This commit is contained in:
xtrullor73
2024-07-04 11:21:10 -07:00
parent 52a2f73c58
commit 068e53857f
2 changed files with 21 additions and 3 deletions

View File

@@ -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;

View File

@@ -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