1

I have the function below to convert a .wav file to .mp3. As you can see, before using the ffmpeg module to convert the audio file, I already check if the file exists or not, then upon conversion, I only keep the new file and delete the old one. But occasionally the console throws me the error Error: ENOENT: no such file or directory, unlink, which means that I unlink (delete) a non-existing file. I cannot understand why, because I already have an existence check even before the conversion, so it is supposed to have existed to be unlinked.

module.exports.convertAndMoveElastic = async (calllog) => {
    let { start, sip_uri, direction, source, destination } = calllog;
    const VNtimezoneOffset = 7 + new Date().getTimezoneOffset() / 60;
    const startTime = new Date(start + VNtimezoneOffset * 3600000 - 60000);
    const date = startTime.getDate() < 10 ? `0${startTime.getDate().toString()}` : startTime.getDate().toString();
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    const month = months[startTime.getMonth()];
    const year = startTime.getFullYear().toString();
    sip_uri = sip_uri || (direction === 'outgoing' ? source : destination);
    const [extension, domain_name] = sip_uri.split("@");
    return new Promise(async (resolve, reject) => {
        const links = await getLinkWithElastic(calllog);
        if (!links) { return reject(); }
        let file_id, filepath;
        for (let link of links) {
            const { callid, sipCallid, uuid, record_path } = link._source;
            if (record_path) {
                let recordPathArr = record_path.split('/');
                file_id = recordPathArr[recordPathArr.length - 1].split('.')[0];
                filepath = path.resolve(base_directory, domain_name, 'archive', year, month, date, `${file_id}.wav`);
            }
            if (!file_id || !fs.existsSync(filepath)) {
                file_id = callid;
                filepath = path.resolve(base_directory, domain_name, 'archive', year, month, date, `${file_id}.wav`);
            }
            if (!file_id || !fs.existsSync(filepath)) {
                file_id = uuid;
                filepath = path.resolve(base_directory, domain_name, 'archive', year, month, date, `${file_id}.wav`);
            }
            if (fs.existsSync(filepath)) { break; }
        }
        if (!fs.existsSync(filepath)) { return reject(); }
        ffmpeg(filepath)
            .audioCodec('libmp3lame')
            .on('error', function (error) {
                reject(error);
            })
            .on('end', function () {
                resolve({ recordUrl: `${host}/record/download/${file_id}.mp3` });
                fs.unlinkSync(filepath);
            })
            .toFormat('mp3')
            .saveToFile(path.resolve(dest_directory, file_id + ".mp3"));
    });
};
necroface
  • 3,365
  • 10
  • 46
  • 70

1 Answers1

0

Use util.promisify on your unlink() method

https://stackoverflow.com/a/45463672/3793648

edrichhans
  • 113
  • 1
  • 2
  • 11