Skip to content

Commit

Permalink
feat: write to temp files while downloading
Browse files Browse the repository at this point in the history
closes #37
  • Loading branch information
lightpohl committed Mar 5, 2022
1 parent 56c1cad commit dae7938
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions bin/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getArchiveFilename, getFilename } from "./naming.js";
import {
getEpisodeAudioUrlAndExt,
getArchiveKey,
getTempPath,
runFfmpeg,
runExec,
writeItemMeta,
Expand Down Expand Up @@ -63,9 +64,10 @@ const download = async ({
},
});

const tempOutputPath = getTempPath(outputPath);
const removeFile = () => {
if (fs.existsSync(outputPath)) {
fs.unlinkSync(outputPath);
if (fs.existsSync(tempOutputPath)) {
fs.unlinkSync(tempOutputPath);
}
};

Expand Down Expand Up @@ -101,14 +103,14 @@ const download = async ({

await pipeline(
got.stream(finalUrl).on("downloadProgress", onDownloadProgress),
fs.createWriteStream(outputPath)
fs.createWriteStream(tempOutputPath)
);
} catch (error) {
removeFile();
throw error;
}

const fileSize = fs.statSync(outputPath).size;
const fileSize = fs.statSync(tempOutputPath).size;

if (fileSize === 0) {
removeFile();
Expand All @@ -121,6 +123,8 @@ const download = async ({
return;
}

fs.renameSync(tempOutputPath, outputPath);

if (expectedSize && !isNaN(expectedSize) && expectedSize !== fileSize) {
logMessage(
"File size differs from expected content length. Suggestion: verify file works as expected",
Expand Down
5 changes: 5 additions & 0 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const parser = new rssParser({
defaultRSS: 2.0,
});

const getTempPath = (path) => {
return `${path}.tmp`;
};

const getArchiveKey = ({ prefix, name }) => {
return `${prefix}-${name}`;
};
Expand Down Expand Up @@ -569,6 +573,7 @@ export {
getFeed,
getImageUrl,
getItemsToDownload,
getTempPath,
getUrlExt,
getUrlEmbed,
logFeedInfo,
Expand Down

0 comments on commit dae7938

Please sign in to comment.