Skip to content

Commit

Permalink
Support download progress
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Oct 5, 2023
1 parent 5e3f641 commit 570e339
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Tethr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type EventTypes = {
} & {
disconnect: void
liveviewStreamUpdate: MediaStream
progress: {progress: number}
}

type ConfigGetters = {
Expand Down
10 changes: 10 additions & 0 deletions src/TethrPTPUSB/TethrSigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
Expand Down

0 comments on commit 570e339

Please sign in to comment.