Skip to content

Commit

Permalink
fix(web downloads)
Browse files Browse the repository at this point in the history
- complete future when the download is finished
- add debug logs
  • Loading branch information
thiagocarvalhodev committed Nov 16, 2023
1 parent 487156d commit 0a72a1d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
7 changes: 7 additions & 0 deletions lib/src/utils/print_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:flutter/foundation.dart';

void ardriveIODebugPrint(String message) {
if (kDebugMode) {
print('[ardrive_io] $message');
}
}
68 changes: 49 additions & 19 deletions lib/src/web/web_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:html';

import 'package:ardrive_io/ardrive_io.dart';
import 'package:ardrive_io/src/utils/completer.dart';
import 'package:ardrive_io/src/utils/print_utils.dart';
import 'package:ardrive_io/src/web/stream_saver.dart';
import 'package:file_picker/file_picker.dart';
import 'package:file_selector/file_selector.dart' as file_selector;
Expand Down Expand Up @@ -70,10 +71,10 @@ class WebIO implements ArDriveIO {
Stream<SaveStatus> saveFileStream(
IOFile file, Completer<bool> finalize) async* {
if (FileSystemAccess.supported) {
debugPrint('Saving using FileSystemAccess API');
ardriveIODebugPrint('Saving using FileSystemAccess API');
yield* _saveFileSystemAccessApi(file, finalize);
} else {
debugPrint('Saving using StreamSaver.js');
ardriveIODebugPrint('Saving using StreamSaver.js');
yield* _saveFileStreamSaver(file, finalize);
}
}
Expand Down Expand Up @@ -108,37 +109,61 @@ class WebIO implements ArDriveIO {
final writer = writable.getWriter();

await for (final chunk in file.openReadStream()) {
if (await completerMaybe(finalize) == false) break;
if (await completerMaybe(finalize) == false) {
ardriveIODebugPrint('Cancelling saveFileStream...');
break;
}

await writer.ready;
await writer.write(chunk);

ardriveIODebugPrint('Wrote ${chunk.length} bytes to file');

bytesSaved += chunk.length;

ardriveIODebugPrint('Bytes saved: $bytesSaved');

yield SaveStatus(
bytesSaved: bytesSaved,
totalBytes: totalBytes,
);
}

ardriveIODebugPrint('Finished writing to file');

ardriveIODebugPrint('Closing file...');
writer.releaseLock();
await writable.close();
finalize.complete(true);

ardriveIODebugPrint('Finalizing saveFileStream...');

final finalizeResult = await finalize.future;
if (!finalizeResult) {
debugPrint('Cancelling saveFileStream...');
ardriveIODebugPrint('Cancelling saveFileStream...');
await handle.remove();
}

ardriveIODebugPrint('SaveFileStream finalized');

yield SaveStatus(
bytesSaved: bytesSaved,
totalBytes: totalBytes,
saveResult: finalizeResult,
);

ardriveIODebugPrint('SaveFileStream yielded');
} on AbortError {
// User dismissed dialog or picked a file deemed too sensitive or dangerous.
ardriveIODebugPrint(
'User dismissed dialog or picked a file deemed too sensitive or dangerous.');
throw ActionCanceledException();
} on NotAllowedError {
// User did not granted permission to readwrite in this file.
throw FileReadWritePermissionDeniedException();
} on Exception {
ardriveIODebugPrint(
'[ardrive_io]: failed to save file using FileSystemAccess API');
yield SaveStatus(
bytesSaved: bytesSaved,
totalBytes: totalBytes,
Expand All @@ -150,7 +175,9 @@ class WebIO implements ArDriveIO {
Stream<SaveStatus> _saveFileStreamSaver(
IOFile file, Completer<bool> finalize) async* {
var bytesSaved = 0;

final totalBytes = await file.length;

yield SaveStatus(
bytesSaved: bytesSaved,
totalBytes: totalBytes,
Expand All @@ -160,6 +187,7 @@ class WebIO implements ArDriveIO {
final writable = createWriteStream(file.name, {
'size': await file.length,
});

final writer = writable.getWriter();

await for (final chunk in file.openReadStream()) {
Expand All @@ -173,22 +201,24 @@ class WebIO implements ArDriveIO {
totalBytes: totalBytes,
);
}

await writer.readyFuture;
debugPrint('writer ready');

// FIXME: this never ends on Firefox/Safari.
// final finalizeResult = await finalize.future;
// debugPrint('Finalize result: $finalizeResult');
// if (!finalizeResult) {
// debugPrint('Cancelling saveFileStream...');
// writer.abort();
// await writable.abortFuture('Finalize result is false');
// } else {

await writer.closeFuture();
writer.close();
writer.releaseLock();
// }

ardriveIODebugPrint('writer ready');

finalize.complete(true);

final finalizeResult = await finalize.future;

if (!finalizeResult) {
ardriveIODebugPrint('Cancelling saveFileStream...');
writer.abort();
await writable.abortFuture('Finalize result is false');
} else {
await writer.closeFuture();
writer.close();
writer.releaseLock();
}

yield SaveStatus(
bytesSaved: bytesSaved,
Expand Down

0 comments on commit 0a72a1d

Please sign in to comment.