Skip to content

Commit

Permalink
Await to download file
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriRojas committed Mar 22, 2024
1 parent ec8b683 commit ef6aee4
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions VirtualEncoder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,28 @@ const streamToMux = (filePath, context) => {
});
};

const downloadFromMuxToFileSystem = async (url, filePath, context) => {
const downloadFromMuxToFileSystem = async (url, filePath) => {
const writer = fs.createWriteStream(filePath);

context.log("Downloading asset from Mux");

const response = await axios({
return axios({
method: 'GET',
url: url,
responseType: 'stream'
});

context.log("Writing asset to file system");

await response.data.pipe(writer);

context.log("Saved on file system");

return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
}).then(response => {
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
writer.close();
reject(err);
});
writer.on('close', () => {
if (!error) {
resolve(true);
}
});
});
});
}

Expand All @@ -84,6 +86,8 @@ module.exports = async function (context, myQueueItem) {
fs.mkdirSync(outputDir, { recursive: true });
const filePath = path.join(outputDir, 'downloaded_file.mp4')

context.log("Downloading asset from Mux");

await downloadFromMuxToFileSystem(url, filePath, context)
.then(() => {
context.log('File downloaded successfully');
Expand Down

0 comments on commit ef6aee4

Please sign in to comment.