diff --git a/lib/utils/attachments.js b/lib/utils/attachments.js index c3e1892..672bf16 100644 --- a/lib/utils/attachments.js +++ b/lib/utils/attachments.js @@ -62,6 +62,45 @@ const waitForFile = ( checkFileExistence().catch(reject); }); +const checkMoovAtom = ( + mp4FilePath, + timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT, + interval = DEFAULT_WAIT_FOR_FILE_INTERVAL, +) => { + return new Promise((resolve, reject) => { + let totalTime = 0; + + const checkFile = () => { + try { + fs.readFile(mp4FilePath, (err, data) => { + if (err) { + return reject(new Error(`Error reading file: ${err.message}`)); + } + + if (data.includes('moov')) { + return resolve(true); + } + + if (totalTime >= timeout) { + return reject( + new Error( + `Timeout of ${timeout}ms reached, 'moov' atom not found in file ${mp4FilePath}.`, + ), + ); + } + totalTime += interval; + setTimeout(checkFile, interval); + return null; + }); + } catch (error) { + return reject(new Error(`Unexpected error: ${error.message}`)); + } + return null; + }; + checkFile(); + }); +}; + const getVideoFile = async ( specFileName, videosFolder = '**', @@ -84,6 +123,13 @@ const getVideoFile = async ( return null; } + try { + await checkMoovAtom(videoFilePath, timeout, interval); + } catch (e) { + console.warn(e.message); + return null; + } + return { name: fileName, type: 'video/mp4', @@ -95,4 +141,5 @@ module.exports = { getScreenshotAttachment, getVideoFile, waitForFile, + checkMoovAtom, };