Skip to content

Commit

Permalink
client: fix file push on safari
Browse files Browse the repository at this point in the history
refs #84
  • Loading branch information
drauggres committed May 11, 2021
1 parent 1949b41 commit 67cc51c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ available from the outside. Select `proxy over adb` from the list of interfaces.
* TinyH264Player may fail to start, try to reload the page.
* MsePlayer reports too many dropped frames in quality statistics: needs
further investigation.
* On Safari file upload does not show progress (it works in one piece).

## Security warning
Be advised and keep in mind:
Expand Down
19 changes: 15 additions & 4 deletions src/app/googDevice/FilePushHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export default class FilePushHandler implements DragEventListener {
this.sendUpdate({ pushId, fileName, logString: `error: "${msg}"`, error: true });
}

private static async getStreamReader(
file: File,
): Promise<{ reader: ReadableStreamDefaultReader<Uint8Array>; result: ReadableStreamReadResult<Uint8Array> }> {
const blob = await new Response(file).blob();
const reader = blob.stream().getReader() as ReadableStreamDefaultReader<Uint8Array>;
const result = await reader.read();
return { reader, result };
}

private async pushFile(file: File): Promise<void> {
const start = Date.now();
const { name: fileName, size: fileSize } = file;
Expand All @@ -65,7 +74,7 @@ export default class FilePushHandler implements DragEventListener {
return;
}
const id = FilePushHandler.REQUEST_NEW_PUSH_ID;
this.sendUpdate({ pushId: id, fileName, logString: 'begin...', error: false });
this.sendUpdate({ pushId: id, fileName, logString: 'begins...', error: false });
const newParams = { id, state: FilePushState.NEW };
this.streamReceiver.sendEvent(CommandControlMessage.createPushFileCommand(newParams));
const pushId: number = await this.waitForResponse(id);
Expand All @@ -74,10 +83,12 @@ export default class FilePushHandler implements DragEventListener {
}

const startParams = { id: pushId, fileName, fileSize, state: FilePushState.START };
const waitPromise = this.waitForResponse(pushId);
this.streamReceiver.sendEvent(CommandControlMessage.createPushFileCommand(startParams));
const stream = file.stream();
const reader = stream.getReader();
const [startResponseCode, result] = await Promise.all([this.waitForResponse(pushId), reader.read()]);
const [startResponseCode, { reader, result }] = await Promise.all([
waitPromise,
FilePushHandler.getStreamReader(file),
]);
if (startResponseCode !== 0) {
this.logError(pushId, fileName, startResponseCode);
return;
Expand Down

0 comments on commit 67cc51c

Please sign in to comment.