From 8c6bc97cdef92908f5883dfed9cc9a7a79ddd0cd Mon Sep 17 00:00:00 2001 From: tdurieux Date: Wed, 8 Sep 2021 09:41:02 +0200 Subject: [PATCH] fix: fix github download --- src/source/GitHubDownload.ts | 39 ++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/source/GitHubDownload.ts b/src/source/GitHubDownload.ts index 23981c0..d5a6b10 100644 --- a/src/source/GitHubDownload.ts +++ b/src/source/GitHubDownload.ts @@ -39,7 +39,12 @@ export default class GitHubDownload extends GitHubBase implements SourceBase { } async download() { - if (this.repository.status == "download") + const fiveMinuteAgo = new Date(); + fiveMinuteAgo.setMinutes(fiveMinuteAgo.getMinutes() - 5); + if ( + this.repository.status == "download" && + this.repository.model.statusDate > fiveMinuteAgo + ) throw new AnonymousError("repo_in_download", this.repository); let response: OctokitResponse; try { @@ -78,28 +83,18 @@ export default class GitHubDownload extends GitHubBase implements SourceBase { } updateProgress(); - await storage.extractTar( - originalPath, - got - .stream(response.url) - .on("downloadProgress", async (p) => { - inDownload = true; - progress = p; - }) - .on("error", (error) => { - inDownload = false; - clearTimeout(progressTimeout); - }) - .on("end", () => { - inDownload = false; - console.log("download finished"); - clearTimeout(progressTimeout); - }) - .on("close", () => clearTimeout(progressTimeout)) - ); + try { + const downloadStream = got.stream(response.url); + downloadStream.addListener("downloadProgress", (p) => (progress = p)); + await storage.extractTar(originalPath, downloadStream); + } catch (error) { + await this.repository.updateStatus("error", "unable_to_download"); + throw new AnonymousError("unable_to_download"); + } finally { + inDownload = false; + clearTimeout(progressTimeout); + } - inDownload = false; - clearTimeout(progressTimeout); await this.repository.updateStatus("ready"); }