diff --git a/mobile/lib/modules/backup/providers/backup.provider.dart b/mobile/lib/modules/backup/providers/backup.provider.dart index f71e248d73c9b0..7bfa06d802afe1 100644 --- a/mobile/lib/modules/backup/providers/backup.provider.dart +++ b/mobile/lib/modules/backup/providers/backup.provider.dart @@ -274,7 +274,7 @@ class BackupNotifier extends StateNotifier { } void cancelBackup() { - state.cancelToken.cancel('Cancel Backup'); + _backupService.cancelBackup(); state = state.copyWith(backupProgress: BackUpProgressEnum.idle, progressInPercentage: 0.0); } diff --git a/mobile/lib/modules/backup/services/backup.service.dart b/mobile/lib/modules/backup/services/backup.service.dart index 73932784ad3177..f323f81646dd2b 100644 --- a/mobile/lib/modules/backup/services/backup.service.dart +++ b/mobile/lib/modules/backup/services/backup.service.dart @@ -17,6 +17,7 @@ import 'package:http/http.dart' as http; class BackupService { final NetworkService _networkService = NetworkService(); + bool shouldCancel = false; Future> getDeviceBackupAsset() async { String deviceId = Hive.box(userInfoBox).get(deviceIdKey); @@ -35,6 +36,7 @@ class BackupService { String deviceId = Hive.box(userInfoBox).get(deviceIdKey); String savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); File? file; + shouldCancel = false; http.MultipartFile? thumbnailUploadData; @@ -65,6 +67,7 @@ class BackupService { // Build thumbnail multipart data var thumbnailData = await entity.thumbnailDataWithSize(const ThumbnailSize(1440, 2560)); + if (thumbnailData != null) { thumbnailUploadData = http.MultipartFile.fromBytes( "thumbnailData", @@ -79,7 +82,18 @@ class BackupService { var box = Hive.box(userInfoBox); - var req = MultipartRequest('POST', Uri.parse('$savedEndpoint/asset/upload'), onProgress: ((bytes, totalBytes) => uploadProgress(bytes, totalBytes))); + var req = MultipartRequest( + 'POST', + Uri.parse('$savedEndpoint/asset/upload'), + onProgress: ((bytes, totalBytes) { + if (shouldCancel) { + return; + } + + uploadProgress(bytes, totalBytes); + }), + ); + req.headers["Authorization"] = "Bearer ${box.get(accessTokenKey)}"; req.fields['deviceAssetId'] = entity.id; @@ -91,16 +105,20 @@ class BackupService { req.fields['fileExtension'] = fileExtension; req.fields['duration'] = entity.videoDuration.toString(); - if(thumbnailUploadData != null) { + if (thumbnailUploadData != null) { req.files.add(thumbnailUploadData); } req.files.add(assetRawUploadData); - var res = await req.send(); - + http.StreamedResponse res = await req.send(); + if (res.statusCode == 201) { singleAssetDoneCb(entity.id, deviceId); } + + if (shouldCancel) { + return; + } } } catch (e) { debugPrint("ERROR backupAsset: ${e.toString()}"); @@ -135,8 +153,11 @@ class BackupService { return DeviceInfoRemote.fromJson(res.toString()); } -} + void cancelBackup() { + shouldCancel = true; + } +} class MultipartRequest extends http.MultipartRequest { /// Creates a new [MultipartRequest]. @@ -165,7 +186,8 @@ class MultipartRequest extends http.MultipartRequest { sink.add(data); }, ); + final stream = byteStream.transform(t); return http.ByteStream(stream); } -} \ No newline at end of file +}