Skip to content

Commit

Permalink
Added imperfect method to cancel uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
alextran1502 committed Jun 14, 2022
1 parent c0bd77c commit 11d88cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mobile/lib/modules/backup/providers/backup.provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class BackupNotifier extends StateNotifier<BackUpState> {
}

void cancelBackup() {
state.cancelToken.cancel('Cancel Backup');
_backupService.cancelBackup();
state = state.copyWith(backupProgress: BackUpProgressEnum.idle, progressInPercentage: 0.0);
}

Expand Down
34 changes: 28 additions & 6 deletions mobile/lib/modules/backup/services/backup.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:http/http.dart' as http;

class BackupService {
final NetworkService _networkService = NetworkService();
bool shouldCancel = false;

Future<List<String>> getDeviceBackupAsset() async {
String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
Expand All @@ -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;

Expand Down Expand Up @@ -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",
Expand All @@ -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;
Expand All @@ -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()}");
Expand Down Expand Up @@ -135,8 +153,11 @@ class BackupService {

return DeviceInfoRemote.fromJson(res.toString());
}
}

void cancelBackup() {
shouldCancel = true;
}
}

class MultipartRequest extends http.MultipartRequest {
/// Creates a new [MultipartRequest].
Expand Down Expand Up @@ -165,7 +186,8 @@ class MultipartRequest extends http.MultipartRequest {
sink.add(data);
},
);

final stream = byteStream.transform(t);
return http.ByteStream(stream);
}
}
}

0 comments on commit 11d88cd

Please sign in to comment.