From ec8ac32ebb8ec770df331ca859c38ac1a0a45e14 Mon Sep 17 00:00:00 2001 From: Thiago Carvalho Date: Thu, 12 Oct 2023 09:42:56 -0300 Subject: [PATCH] fix(uploader) if the download failed don't show the upload in progress without progress text --- lib/blocs/upload/limits.dart | 9 ++++---- lib/blocs/upload/models/upload_plan.dart | 22 +++++++++---------- lib/blocs/upload/upload_cubit.dart | 3 --- lib/components/upload_form.dart | 6 +++-- lib/utils/upload_plan_utils.dart | 3 ++- .../lib/src/ardrive_uploader.dart | 7 ++++++ .../lib/src/upload_controller.dart | 22 +++++++++++++++---- 7 files changed, 47 insertions(+), 25 deletions(-) diff --git a/lib/blocs/upload/limits.dart b/lib/blocs/upload/limits.dart index 48336484d8..378eaace86 100644 --- a/lib/blocs/upload/limits.dart +++ b/lib/blocs/upload/limits.dart @@ -1,5 +1,4 @@ import 'package:ardrive_utils/ardrive_utils.dart'; -import 'package:flutter/foundation.dart'; final privateFileSizeLimit = const MiB(100000).size; @@ -7,10 +6,12 @@ final mobilePrivateFileSizeLimit = const GiB(10).size; final publicFileSafeSizeLimit = const GiB(5).size; -final bundleSizeLimit = kIsWeb ? webBundleSizeLimit : mobileBundleSizeLimit; +int bundleSizeLimit(bool isTurbo) => + isTurbo ? turboBundleSizeLimit : d2nBundleSizeLimit; -final webBundleSizeLimit = const MiB(65000).size; -final mobileBundleSizeLimit = const MiB(65000).size; +final d2nBundleSizeLimit = const GiB(65).size; +final turboBundleSizeLimit = const MiB(1).size; +final mobileBundleSizeLimit = const GiB(65).size; const maxBundleDataItemCount = 500; const maxFilesPerBundle = maxBundleDataItemCount ~/ 2; const maxFilesSizePerBundleUsingTurbo = 1; diff --git a/lib/blocs/upload/models/upload_plan.dart b/lib/blocs/upload/models/upload_plan.dart index cc2f08509b..0d164d3257 100644 --- a/lib/blocs/upload/models/upload_plan.dart +++ b/lib/blocs/upload/models/upload_plan.dart @@ -5,7 +5,6 @@ import 'package:ardrive/blocs/upload/upload_handles/upload_handle.dart'; import 'package:ardrive/turbo/services/upload_service.dart'; import 'package:ardrive/utils/bundles/next_fit_bundle_packer.dart'; import 'package:ardrive/utils/logger/logger.dart'; -import 'package:flutter/foundation.dart'; import '../upload_handles/file_data_item_upload_handle.dart'; import '../upload_handles/file_v2_upload_handle.dart'; @@ -23,14 +22,14 @@ class UploadPlan { required this.maxDataItemCount, }); - static Future create({ - required Map fileV2UploadHandles, - required Map fileDataItemUploadHandles, - required Map - folderDataItemUploadHandles, - required TurboUploadService turboUploadService, - required int maxDataItemCount, - }) async { + static Future create( + {required Map fileV2UploadHandles, + required Map fileDataItemUploadHandles, + required Map + folderDataItemUploadHandles, + required TurboUploadService turboUploadService, + required int maxDataItemCount, + required bool useTurbo}) async { final uploadPlan = UploadPlan._create( fileV2UploadHandles: fileV2UploadHandles, maxDataItemCount: maxDataItemCount, @@ -43,6 +42,7 @@ class UploadPlan { folderDataItemUploadHandles: folderDataItemUploadHandles, turboUploadService: turboUploadService, maxDataItemCount: maxDataItemCount, + useTurbo: useTurbo, ); } @@ -50,6 +50,7 @@ class UploadPlan { } Future createBundleHandlesFromDataItemHandles({ + required bool useTurbo, Map fileDataItemUploadHandles = const {}, Map folderDataItemUploadHandles = const {}, @@ -58,8 +59,7 @@ class UploadPlan { }) async { logger.i( 'Creating bundle handles from data item handles with a max number of files of $maxDataItemCount'); - final int maxBundleSize = - (kIsWeb ? bundleSizeLimit : mobileBundleSizeLimit); + final int maxBundleSize = bundleSizeLimit(useTurbo); final folderItems = await NextFitBundlePacker( maxBundleSize: maxBundleSize, diff --git a/lib/blocs/upload/upload_cubit.dart b/lib/blocs/upload/upload_cubit.dart index 3bc9f92b90..789ebc0eeb 100644 --- a/lib/blocs/upload/upload_cubit.dart +++ b/lib/blocs/upload/upload_cubit.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'package:ardrive/authentication/ardrive_auth.dart'; import 'package:ardrive/blocs/blocs.dart'; -import 'package:ardrive/blocs/upload/limits.dart'; import 'package:ardrive/blocs/upload/models/models.dart'; import 'package:ardrive/blocs/upload/upload_file_checker.dart'; import 'package:ardrive/core/upload/cost_calculator.dart'; @@ -90,8 +89,6 @@ class UploadCubit extends Cubit { final Map conflictingFiles = {}; final List conflictingFolders = []; - bool fileSizeWithinBundleLimits(int size) => size < bundleSizeLimit; - UploadCubit({ required this.driveId, required this.parentFolderId, diff --git a/lib/components/upload_form.dart b/lib/components/upload_form.dart index d008f766a2..2675373887 100644 --- a/lib/components/upload_form.dart +++ b/lib/components/upload_form.dart @@ -843,8 +843,10 @@ class _UploadFormState extends State { '${filesize(((task.uploadItem!.size) * task.progress).ceil())}/${filesize(task.uploadItem!.size)}'; } } else { - progressText = - 'Your upload is in progress, but for large files the progress it not available. Please wait...'; + if (task.status == UploadStatus.inProgress) { + progressText = + 'Your upload is in progress, but for large files the progress it not available. Please wait...'; + } } return Column( diff --git a/lib/utils/upload_plan_utils.dart b/lib/utils/upload_plan_utils.dart index 8eccab950a..34d5b76bba 100644 --- a/lib/utils/upload_plan_utils.dart +++ b/lib/utils/upload_plan_utils.dart @@ -85,7 +85,7 @@ class UploadPlanUtils { ? RevisionAction.uploadNewVersion : RevisionAction.create; - if (fileSize < bundleSizeLimit) { + if (fileSize < bundleSizeLimit(useTurbo)) { fileDataItemUploadHandles[fileEntity.id!] = FileDataItemUploadHandle( entity: fileEntity, path: filePath, @@ -129,6 +129,7 @@ class UploadPlanUtils { turboUploadService: turboUploadService, maxDataItemCount: useTurbo ? maxFilesSizePerBundleUsingTurbo : maxFilesPerBundle, + useTurbo: useTurbo, ); } diff --git a/packages/ardrive_uploader/lib/src/ardrive_uploader.dart b/packages/ardrive_uploader/lib/src/ardrive_uploader.dart index b131ad7645..b3b06a7614 100644 --- a/packages/ardrive_uploader/lib/src/ardrive_uploader.dart +++ b/packages/ardrive_uploader/lib/src/ardrive_uploader.dart @@ -89,6 +89,8 @@ class _ArDriveUploader implements ArDriveUploader { metadataGenerator: _metadataGenerator, type: type, ), + numOfWorkers: 1, + maxTasksPerWorker: 1, ); final metadata = await _metadataGenerator.generateMetadata( @@ -124,6 +126,8 @@ class _ArDriveUploader implements ArDriveUploader { metadataGenerator: _metadataGenerator, type: type, ), + numOfWorkers: driveKey != null ? 3 : 5, + maxTasksPerWorker: driveKey != null ? 1 : 5, ); for (var f in files) { @@ -135,6 +139,7 @@ class _ArDriveUploader implements ArDriveUploader { final fileTask = FileUploadTask( file: f.$2, metadata: metadata as ARFSFileUploadMetadata, + content: [metadata], encryptionKey: driveKey, streamedUpload: _streamedUploadFactory.fromUploadType( type, @@ -195,6 +200,8 @@ class _ArDriveUploader implements ArDriveUploader { StreamController(), streamedUpload, dataBundler, + numOfWorkers: driveKey != null ? 3 : 5, + maxTasksPerWorker: driveKey != null ? 1 : 5, ); if (folderMetadatas.isNotEmpty) { diff --git a/packages/ardrive_uploader/lib/src/upload_controller.dart b/packages/ardrive_uploader/lib/src/upload_controller.dart index ef1415d555..9436887a7c 100644 --- a/packages/ardrive_uploader/lib/src/upload_controller.dart +++ b/packages/ardrive_uploader/lib/src/upload_controller.dart @@ -254,12 +254,16 @@ abstract class UploadController { factory UploadController( StreamController progressStream, StreamedUpload streamedUpload, - DataBundler dataBundler, - ) { + DataBundler dataBundler, { + int numOfWorkers = 5, + int maxTasksPerWorker = 5, + }) { return _UploadController( progressStream: progressStream, streamedUpload: streamedUpload, dataBundler: dataBundler, + numOfWorkers: numOfWorkers, + maxTasksPerWorker: maxTasksPerWorker, ); } } @@ -268,12 +272,18 @@ class _UploadController implements UploadController { final StreamController _progressStream; final StreamedUpload _streamedUpload; final DataBundler _dataBundler; + final int _numOfWorkers; + final int _maxTasksPerWorker; _UploadController({ required StreamController progressStream, required StreamedUpload streamedUpload, required DataBundler dataBundler, + int numOfWorkers = 5, + int maxTasksPerWorker = 5, }) : _dataBundler = dataBundler, + _numOfWorkers = numOfWorkers, + _maxTasksPerWorker = maxTasksPerWorker, _progressStream = progressStream, _streamedUpload = streamedUpload { init(); @@ -434,6 +444,10 @@ class _UploadController implements UploadController { for (var task in tasks) { if (task.uploadItem != null) { totalSize += task.uploadItem!.size; + } else { + if (task is FileUploadTask) { + totalSize += task.metadata.size; + } } } @@ -488,8 +502,8 @@ class _UploadController implements UploadController { } else { // creates a worker pool and initializes it with the tasks workerPool = WorkerPool( - numWorkers: 5, - maxTasksPerWorker: 5, + numWorkers: _numOfWorkers, + maxTasksPerWorker: _maxTasksPerWorker, taskQueue: tasks.values .where((element) => element.status == UploadStatus.notStarted) .toList(),