From 7a44a423316e5305081b4c552713d56e1dd9cb6b Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 7 Sep 2021 12:10:25 +0200 Subject: [PATCH] feat: display download progress --- public/partials/status.htm | 10 ++++++-- src/Repository.ts | 8 ++++--- .../anonymizedRepositories.schema.ts | 3 ++- .../anonymizedRepositories.types.ts | 3 ++- src/source/GitHubDownload.ts | 23 ++++++++++++++++++- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/public/partials/status.htm b/public/partials/status.htm index bbd3bfc..efe57d4 100644 --- a/public/partials/status.htm +++ b/public/partials/status.htm @@ -10,7 +10,7 @@ FAQ for more information.

-
+
- {{repo.status | title}} + + {{repo.status | title}} + +
diff --git a/src/Repository.ts b/src/Repository.ts index 72f79ed..f47188d 100644 --- a/src/Repository.ts +++ b/src/Repository.ts @@ -202,9 +202,10 @@ export default class Repository { * @param status the new status * @param errorMessage a potential error message to display */ - async updateStatus(status: RepositoryStatus, errorMessage?: string) { + async updateStatus(status: RepositoryStatus, statusMessage?: string) { this._model.status = status; - this._model.errorMessage = errorMessage; + this._model.statusDate = new Date(); + this._model.statusMessage = statusMessage; return this._model.save(); } @@ -323,7 +324,8 @@ export default class Repository { options: this._model.options, conference: this._model.conference, anonymizeDate: this._model.anonymizeDate, - status: this._model.status, + status: this.status, + statusMessage: this._model.statusMessage, source: this.source.toJSON(), lastView: this._model.lastView, pageView: this._model.pageView, diff --git a/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts b/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts index 0fb65c9..7025ada 100644 --- a/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts +++ b/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts @@ -10,7 +10,8 @@ const AnonymizedRepositorySchema = new Schema({ type: String, default: "preparing", }, - errorMessage: String, + statusDate: Date, + statusMessage: String, anonymizeDate: Date, lastView: Date, pageView: Number, diff --git a/src/database/anonymizedRepositories/anonymizedRepositories.types.ts b/src/database/anonymizedRepositories/anonymizedRepositories.types.ts index 1d038b7..484d7d1 100644 --- a/src/database/anonymizedRepositories/anonymizedRepositories.types.ts +++ b/src/database/anonymizedRepositories/anonymizedRepositories.types.ts @@ -4,7 +4,8 @@ import { RepositoryStatus, Tree } from "../../types"; export interface IAnonymizedRepository { repoId: string; status?: RepositoryStatus; - errorMessage?: string; + statusMessage?: string; + statusDate: Date; anonymizeDate: Date; source: { type: "GitHubDownload" | "GitHubStream" | "Zip"; diff --git a/src/source/GitHubDownload.ts b/src/source/GitHubDownload.ts index 15f7576..b131977 100644 --- a/src/source/GitHubDownload.ts +++ b/src/source/GitHubDownload.ts @@ -60,7 +60,28 @@ export default class GitHubDownload extends GitHubBase implements SourceBase { await this.repository.updateStatus("download"); const originalPath = this.repository.originalCachePath; await storage.mk(originalPath); - await storage.extractTar(originalPath, got.stream(response.url)); + let progress = null; + let progressInterval = setInterval(async () => { + if (progress) { + await this.repository.updateStatus( + this.repository.status, + progress.transferred + ); + } + }, 1000); + await storage.extractTar( + originalPath, + got + .stream(response.url) + .on("downloadProgress", async (p) => { + console.log(p); + progress = p; + }) + .on("error", () => clearInterval(progressInterval)) + .on("end", () => clearInterval(progressInterval)) + .on("close", () => clearInterval(progressInterval)) + ); + clearInterval(progressInterval); await this.repository.updateStatus("ready"); }