From 570e339412aea2742c431c730fe555ee2ef2433d Mon Sep 17 00:00:00 2001 From: Baku Hashimoto Date: Thu, 5 Oct 2023 21:16:08 +0900 Subject: [PATCH] Support download progress --- src/Tethr.ts | 1 + src/TethrPTPUSB/TethrSigma.ts | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/Tethr.ts b/src/Tethr.ts index b2731ab..c9c8027 100644 --- a/src/Tethr.ts +++ b/src/Tethr.ts @@ -61,6 +61,7 @@ type EventTypes = { } & { disconnect: void liveviewStreamUpdate: MediaStream + progress: {progress: number} } type ConfigGetters = { diff --git a/src/TethrPTPUSB/TethrSigma.ts b/src/TethrPTPUSB/TethrSigma.ts index e7c3dd1..cee5852 100644 --- a/src/TethrPTPUSB/TethrSigma.ts +++ b/src/TethrPTPUSB/TethrSigma.ts @@ -978,6 +978,11 @@ export class TethrSigma extends TethrPTPUSB { if (doDownload) { const pictFileInfos = await this.getPictFileInfo2() + const totalSize = pictFileInfos.reduce((p, i) => p + i.fileSize, 0) + let loaded = 0 + + this.emit('progress', 0) + for await (const info of pictFileInfos) { const pictArray = new Uint8Array(info.fileSize) @@ -991,12 +996,17 @@ export class TethrSigma extends TethrPTPUSB { label: 'SigmaFP GetBigPartialPictFile', opcode: OpCodeSigma.GetBigPartialPictFile, parameters: [info.fileAddress, offset, length], + // Added 64 bytes for safety maxByteLength: CHUNK_SIZE + 64, }) // First 4 bytes is the length of buffer so splice them const chunkArray = new Uint8Array(data.slice(4, 4 + length)) + loaded += length + + this.emit('progress', {progress: loaded / totalSize}) + // Copy to buffer pictArray.set(chunkArray, offset) }