简体   繁体   English

如果在通过 nodejs 扫描时 mp3 文件损坏,如何处理错误

[英]How to handle errors if an mp3 file is corrupted while scanning through nodejs

I created a function to fetch mp3 files that are inside user's storage drives that takes an array of directories that needed to be searched.我创建了一个函数来获取用户存储驱动器中的 mp3 文件,这些文件包含需要搜索的一系列目录。 After receiving full list of music files in an array, I used that as an argument in another function that fetches it's metadata (using music metadata).在接收到数组中音乐文件的完整列表后,我将其用作另一个获取元数据的函数中的参数(使用音乐元数据)。 So far my code is working perfectly when there are no files it returns empty array otherwise and array of objects containing their metadata.到目前为止,当没有文件时,我的代码运行良好,否则返回空数组以及包含其元数据的对象数组。 Here is My code :这是我的代码:

 const find = require('find') ; const mm = require('music-metadata') ; const directories = ["C:\\\\Users\\\\{UserNamehere}\\\\Music\\\\"] // you can add more directories async function parseMetadata(files){ let metadata ; data = files.map(async (file) => { metadata = await mm.parseFile(file,{duration:true}) ; m = metadata.common ; return m ; }) ; const musicarray = await Promise.all(data) ; return musicarray ; } function fetchNewSongs(dirs){ let res,musicfiles = [] ; dirs.forEach((path)=>{ res = find.fileSync(/\\.mp3/i,path) ; musicfiles.push(...res) ; }); if (musicfiles.length !== 0){ return parseMetadata(musicfiles) ; } else{ return Promise.resolve([]) ; } } fetchNewSongs(directories).then( value => { console.log(value) })

Problem arises when any music file is corrupted or it's metadata cannot be fetched by music-metadata causing the flow of parsing the metadata list to stop.当任何音乐文件损坏或音乐元数据无法获取其music-metadata导致解析元数据列表的流程停止时,就会出现问题。 I tried to rename a .txt to .mp3 to reconstruct situation of a corrupted file.我试图将.txt重命名为.mp3以重建损坏文件的情况。 What I want is whenever parsing the metadata of a particular music file if an error occurs it just return empty array and then continue searching for other files.我想要的是每当解析特定音乐文件的元数据时,如果发生错误,它只会返回空数组,然后继续搜索其他文件。 After the process is complete then removing those elements of array having empty objects.该过程完成后,删除具有空对象的数组元素。

I think you are missing a try/catch in your map function:我认为您的地图功能中缺少 try/catch :

Mocked version:模拟版:

const mm = {
  parseFile(file) {
    return Promise.reject("Bad format");
  },
};

async function parseMetadata(files) {
  let metadata = files.map(async (file) => {
    try {
      metadata = await mm.parseFile(file, { duration: true });
    } catch (error) {
      return [];
    }

    m = metadata.common;
    return m;
  });

  return Promise.all(metadata);
}

async function fetchNewSongs(dirs = ["foo", "bar", "baz"]) {
  return parseMetadata(dirs);
}

fetchNewSongs().then(console.log, console.error);

// output : [ [], [], [] ]

As an addition you might go for a for loop and avoid having to filter your array afterward作为补充,您可能会使用for循环并避免之后必须过滤您的数组

const mm = {
  parseFile(file) {
    return Promise.reject("Bad format");
  },
};

async function parseMetadata(files) {
  const metadataCollection = [];
  for (const file of files) {
    try {
      const metadata = await mm.parseFile(file, { duration: true });
      metadataCollection.push(metadata);
    } catch (error) {
      console.warn(error);
    }
  }
  return metadataCollection;
}

async function fetchNewSongs(dirs = ["foo", "bar", "baz"]) {
  return parseMetadata(dirs);
}

fetchNewSongs().then(console.log, console.error);

// outputs:
// Bad format
// Bad format
// Bad format
// []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM