Skip to content

Commit

Permalink
ファイルダウンロードで可能ならサイズ比較をする #1020
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Apr 11, 2020
1 parent 55c093a commit 8ae4911
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/misc/donwload-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ export async function downloadUrl(url: string, path: string) {
throw response.status;
}

// Content-Lengthがあればとっておく
const contentLength = response.headers.get('content-length');
const expectedLength = contentLength != null ? Number(contentLength) : null;

await pipeline(response.body, fs.createWriteStream(path));

// 可能ならばサイズ比較
const actualLength = (await util.promisify(fs.stat)(path)).size;

if (response.headers.get('accept-encoding') == null && expectedLength != null && expectedLength !== actualLength) {
throw `size error: expected: ${expectedLength}, but got ${actualLength}`;
}

logger.succ(`Download finished: ${chalk.cyan(url)}`);
}

0 comments on commit 8ae4911

Please sign in to comment.