Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update attachments.js : Add timeout before resolve files #203

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/utils/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '**',
Expand All @@ -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',
Expand All @@ -95,4 +141,5 @@ module.exports = {
getScreenshotAttachment,
getVideoFile,
waitForFile,
checkMoovAtom,
};