mirror of
https://github.com/xtrll/MusicMetaFinder.git
synced 2026-04-17 15:53:29 +02:00
fix(utils): normalize paths to use forward slashes for consistent platform support
This commit is contained in:
@@ -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;
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user