From 0a72a1d60452eb42629af89dbbdbf63c31fcee20 Mon Sep 17 00:00:00 2001 From: Thiago Carvalho Date: Thu, 16 Nov 2023 09:01:33 -0300 Subject: [PATCH 1/4] fix(web downloads) - complete future when the download is finished - add debug logs --- lib/src/utils/print_utils.dart | 7 ++++ lib/src/web/web_io.dart | 68 ++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 lib/src/utils/print_utils.dart diff --git a/lib/src/utils/print_utils.dart b/lib/src/utils/print_utils.dart new file mode 100644 index 0000000..a2b3486 --- /dev/null +++ b/lib/src/utils/print_utils.dart @@ -0,0 +1,7 @@ +import 'package:flutter/foundation.dart'; + +void ardriveIODebugPrint(String message) { + if (kDebugMode) { + print('[ardrive_io] $message'); + } +} diff --git a/lib/src/web/web_io.dart b/lib/src/web/web_io.dart index f72c4df..932cff8 100644 --- a/lib/src/web/web_io.dart +++ b/lib/src/web/web_io.dart @@ -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; @@ -70,10 +71,10 @@ class WebIO implements ArDriveIO { Stream saveFileStream( IOFile file, Completer 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); } } @@ -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, @@ -150,7 +175,9 @@ class WebIO implements ArDriveIO { Stream _saveFileStreamSaver( IOFile file, Completer finalize) async* { var bytesSaved = 0; + final totalBytes = await file.length; + yield SaveStatus( bytesSaved: bytesSaved, totalBytes: totalBytes, @@ -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()) { @@ -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, From c22b22fcdf059974b0e64819e002d1a173a0e623 Mon Sep 17 00:00:00 2001 From: Thiago Carvalho Date: Fri, 17 Nov 2023 11:06:47 -0300 Subject: [PATCH 2/4] Update web_io.dart - rollback the changes on file saver. The issue on firefox still happening. --- lib/src/web/web_io.dart | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/src/web/web_io.dart b/lib/src/web/web_io.dart index 932cff8..ab9599c 100644 --- a/lib/src/web/web_io.dart +++ b/lib/src/web/web_io.dart @@ -184,13 +184,17 @@ class WebIO implements ArDriveIO { ); try { + ardriveIODebugPrint('Creating writable stream...'); + final writable = createWriteStream(file.name, { - 'size': await file.length, + 'size': totalBytes, }); final writer = writable.getWriter(); - await for (final chunk in file.openReadStream()) { + await for (final chunk in file.openReadStream(0, totalBytes)) { + ardriveIODebugPrint('Writing ${chunk.length} bytes to file...'); + if (await completerMaybe(finalize) == false) break; await writer.readyFuture; await writer.writeFuture(chunk); @@ -200,25 +204,25 @@ class WebIO implements ArDriveIO { bytesSaved: bytesSaved, totalBytes: totalBytes, ); - } - - await writer.readyFuture; - ardriveIODebugPrint('writer ready'); + ardriveIODebugPrint('save status: $bytesSaved/$totalBytes'); + } - finalize.complete(true); + ardriveIODebugPrint('Finished writing to file. Closing file...'); - final finalizeResult = await finalize.future; + // 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 { - if (!finalizeResult) { - ardriveIODebugPrint('Cancelling saveFileStream...'); - writer.abort(); - await writable.abortFuture('Finalize result is false'); - } else { - await writer.closeFuture(); - writer.close(); - writer.releaseLock(); - } + await writer.closeFuture(); + writer.close(); + writer.releaseLock(); + // } yield SaveStatus( bytesSaved: bytesSaved, From e9a88279e4fcaf5698c1e37055ca579f3a56f912 Mon Sep 17 00:00:00 2001 From: Thiago Carvalho Date: Fri, 17 Nov 2023 11:34:29 -0300 Subject: [PATCH 3/4] Revert "Update web_io.dart" This reverts commit c22b22fcdf059974b0e64819e002d1a173a0e623. --- lib/src/web/web_io.dart | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/src/web/web_io.dart b/lib/src/web/web_io.dart index ab9599c..932cff8 100644 --- a/lib/src/web/web_io.dart +++ b/lib/src/web/web_io.dart @@ -184,17 +184,13 @@ class WebIO implements ArDriveIO { ); try { - ardriveIODebugPrint('Creating writable stream...'); - final writable = createWriteStream(file.name, { - 'size': totalBytes, + 'size': await file.length, }); final writer = writable.getWriter(); - await for (final chunk in file.openReadStream(0, totalBytes)) { - ardriveIODebugPrint('Writing ${chunk.length} bytes to file...'); - + await for (final chunk in file.openReadStream()) { if (await completerMaybe(finalize) == false) break; await writer.readyFuture; await writer.writeFuture(chunk); @@ -204,25 +200,25 @@ class WebIO implements ArDriveIO { bytesSaved: bytesSaved, totalBytes: totalBytes, ); - - ardriveIODebugPrint('save status: $bytesSaved/$totalBytes'); } - ardriveIODebugPrint('Finished writing to file. Closing file...'); + await writer.readyFuture; - // 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 { + ardriveIODebugPrint('writer ready'); - await writer.closeFuture(); - writer.close(); - writer.releaseLock(); - // } + 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, From dc65c0806fd738299350e5733403b979c4b86a38 Mon Sep 17 00:00:00 2001 From: Thiago Carvalho Date: Thu, 4 Jan 2024 14:02:02 -0300 Subject: [PATCH 4/4] bump version --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 8e20cbe..2f09c47 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: ardrive_io description: A new Flutter package project. -version: 1.4.3 +version: 1.4.4 homepage: publish_to: none