Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.4.4 #20

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that the linter doesn't complain about this print statement.

}
}
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