2022-02-05 23:07:56 -07:00
|
|
|
import 'dart:async';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
2022-08-18 07:41:59 -07:00
|
|
|
import 'package:collection/collection.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:flutter/material.dart';
|
2022-06-25 11:46:51 -07:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-03-18 07:55:11 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/models/backup_album.model.dart';
|
2022-07-06 14:12:55 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/models/current_upload_asset.model.dart';
|
2023-03-18 07:55:11 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/models/duplicated_asset.model.dart';
|
2022-07-06 14:12:55 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/models/error_upload_asset.model.dart';
|
2023-12-07 08:53:15 -07:00
|
|
|
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
|
|
|
|
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
2023-03-22 18:36:44 -07:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2022-08-02 22:04:34 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2023-03-18 07:55:11 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2022-07-13 05:23:48 -07:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2023-03-18 07:55:11 -07:00
|
|
|
import 'package:isar/isar.dart';
|
2023-06-26 08:27:32 -07:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-13 05:23:48 -07:00
|
|
|
import 'package:openapi/api.dart';
|
2023-06-26 08:27:32 -07:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
2022-06-18 05:36:58 -07:00
|
|
|
import 'package:cancellation_token_http/http.dart' as http;
|
2023-07-11 20:56:30 -07:00
|
|
|
import 'package:path/path.dart' as p;
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
final backupServiceProvider = Provider(
|
|
|
|
(ref) => BackupService(
|
|
|
|
ref.watch(apiServiceProvider),
|
2023-03-18 07:55:11 -07:00
|
|
|
ref.watch(dbProvider),
|
2023-12-07 08:53:15 -07:00
|
|
|
ref.watch(appSettingsServiceProvider),
|
2022-07-13 05:23:48 -07:00
|
|
|
),
|
|
|
|
);
|
2022-06-25 11:46:51 -07:00
|
|
|
|
2022-02-03 09:06:44 -07:00
|
|
|
class BackupService {
|
2023-01-30 15:00:03 -07:00
|
|
|
final httpClient = http.Client();
|
2022-07-13 05:23:48 -07:00
|
|
|
final ApiService _apiService;
|
2023-03-18 07:55:11 -07:00
|
|
|
final Isar _db;
|
2023-06-26 08:27:32 -07:00
|
|
|
final Logger _log = Logger("BackupService");
|
2023-12-07 08:53:15 -07:00
|
|
|
final AppSettingsService _appSetting;
|
2022-08-02 22:04:34 -07:00
|
|
|
|
2023-12-07 08:53:15 -07:00
|
|
|
BackupService(this._apiService, this._db, this._appSetting);
|
2022-07-06 14:12:55 -07:00
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
Future<List<String>?> getDeviceBackupAsset() async {
|
2023-03-22 18:36:44 -07:00
|
|
|
final String deviceId = Store.get(StoreKey.deviceId);
|
2022-07-06 14:12:55 -07:00
|
|
|
|
|
|
|
try {
|
2022-07-13 05:23:48 -07:00
|
|
|
return await _apiService.assetApi.getUserAssetsByDeviceId(deviceId);
|
2023-11-25 08:46:20 -07:00
|
|
|
|
|
|
|
// TODO! Start using this in 1.92.0
|
|
|
|
// return await _apiService.assetApi.getAllUserAssetsByDeviceId(deviceId);
|
2022-07-06 14:12:55 -07:00
|
|
|
} catch (e) {
|
2022-07-13 05:23:48 -07:00
|
|
|
debugPrint('Error [getDeviceBackupAsset] ${e.toString()}');
|
|
|
|
return null;
|
2022-07-06 14:12:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 07:55:11 -07:00
|
|
|
Future<void> _saveDuplicatedAssetIds(List<String> deviceAssetIds) {
|
|
|
|
final duplicates = deviceAssetIds.map((id) => DuplicatedAsset(id)).toList();
|
|
|
|
return _db.writeTxn(() => _db.duplicatedAssets.putAll(duplicates));
|
2022-10-25 07:51:03 -07:00
|
|
|
}
|
|
|
|
|
2023-03-18 07:55:11 -07:00
|
|
|
/// Get duplicated asset id from database
|
|
|
|
Future<Set<String>> getDuplicatedAssetIds() async {
|
|
|
|
final duplicates = await _db.duplicatedAssets.where().findAll();
|
|
|
|
return duplicates.map((e) => e.id).toSet();
|
2022-10-25 07:51:03 -07:00
|
|
|
}
|
|
|
|
|
2023-03-18 07:55:11 -07:00
|
|
|
QueryBuilder<BackupAlbum, BackupAlbum, QAfterFilterCondition>
|
|
|
|
selectedAlbumsQuery() =>
|
|
|
|
_db.backupAlbums.filter().selectionEqualTo(BackupSelection.select);
|
|
|
|
QueryBuilder<BackupAlbum, BackupAlbum, QAfterFilterCondition>
|
|
|
|
excludedAlbumsQuery() =>
|
|
|
|
_db.backupAlbums.filter().selectionEqualTo(BackupSelection.exclude);
|
|
|
|
|
2022-08-21 09:29:24 -07:00
|
|
|
/// Returns all assets newer than the last successful backup per album
|
|
|
|
Future<List<AssetEntity>> buildUploadCandidates(
|
2023-03-18 07:55:11 -07:00
|
|
|
List<BackupAlbum> selectedBackupAlbums,
|
|
|
|
List<BackupAlbum> excludedBackupAlbums,
|
2022-08-18 07:41:59 -07:00
|
|
|
) async {
|
|
|
|
final filter = FilterOptionGroup(
|
|
|
|
containsPathModified: true,
|
|
|
|
orders: [const OrderOption(type: OrderOptionType.updateDate)],
|
2023-02-04 13:42:42 -07:00
|
|
|
// title is needed to create Assets
|
|
|
|
imageOption: const FilterOption(needTitle: true),
|
|
|
|
videoOption: const FilterOption(needTitle: true),
|
2022-08-18 07:41:59 -07:00
|
|
|
);
|
|
|
|
final now = DateTime.now();
|
|
|
|
final List<AssetPathEntity?> selectedAlbums =
|
2023-03-18 07:55:11 -07:00
|
|
|
await _loadAlbumsWithTimeFilter(selectedBackupAlbums, filter, now);
|
2022-08-18 07:41:59 -07:00
|
|
|
if (selectedAlbums.every((e) => e == null)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
final int allIdx = selectedAlbums.indexWhere((e) => e != null && e.isAll);
|
|
|
|
if (allIdx != -1) {
|
|
|
|
final List<AssetPathEntity?> excludedAlbums =
|
2023-03-18 07:55:11 -07:00
|
|
|
await _loadAlbumsWithTimeFilter(excludedBackupAlbums, filter, now);
|
2022-08-18 07:41:59 -07:00
|
|
|
final List<AssetEntity> toAdd = await _fetchAssetsAndUpdateLastBackup(
|
|
|
|
selectedAlbums.slice(allIdx, allIdx + 1),
|
2023-03-18 07:55:11 -07:00
|
|
|
selectedBackupAlbums.slice(allIdx, allIdx + 1),
|
2022-08-18 07:41:59 -07:00
|
|
|
now,
|
|
|
|
);
|
|
|
|
final List<AssetEntity> toRemove = await _fetchAssetsAndUpdateLastBackup(
|
|
|
|
excludedAlbums,
|
2023-03-18 07:55:11 -07:00
|
|
|
excludedBackupAlbums,
|
2022-08-18 07:41:59 -07:00
|
|
|
now,
|
|
|
|
);
|
|
|
|
return toAdd.toSet().difference(toRemove.toSet()).toList();
|
|
|
|
} else {
|
|
|
|
return await _fetchAssetsAndUpdateLastBackup(
|
|
|
|
selectedAlbums,
|
2023-03-18 07:55:11 -07:00
|
|
|
selectedBackupAlbums,
|
2022-08-18 07:41:59 -07:00
|
|
|
now,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<AssetPathEntity?>> _loadAlbumsWithTimeFilter(
|
2023-03-18 07:55:11 -07:00
|
|
|
List<BackupAlbum> albums,
|
2022-08-18 07:41:59 -07:00
|
|
|
FilterOptionGroup filter,
|
|
|
|
DateTime now,
|
|
|
|
) async {
|
2023-03-18 07:55:11 -07:00
|
|
|
List<AssetPathEntity?> result = [];
|
|
|
|
for (BackupAlbum a in albums) {
|
2022-08-18 07:41:59 -07:00
|
|
|
try {
|
|
|
|
final AssetPathEntity album =
|
|
|
|
await AssetPathEntity.obtainPathFromProperties(
|
2023-03-18 07:55:11 -07:00
|
|
|
id: a.id,
|
2022-08-18 07:41:59 -07:00
|
|
|
optionGroup: filter.copyWith(
|
|
|
|
updateTimeCond: DateTimeCond(
|
|
|
|
// subtract 2 seconds to prevent missing assets due to rounding issues
|
2023-03-18 07:55:11 -07:00
|
|
|
min: a.lastBackup.subtract(const Duration(seconds: 2)),
|
2022-08-18 07:41:59 -07:00
|
|
|
max: now,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
maxDateTimeToNow: false,
|
|
|
|
);
|
2023-03-18 07:55:11 -07:00
|
|
|
result.add(album);
|
2022-08-18 07:41:59 -07:00
|
|
|
} on StateError {
|
|
|
|
// either there are no assets matching the filter criteria OR the album no longer exists
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<AssetEntity>> _fetchAssetsAndUpdateLastBackup(
|
|
|
|
List<AssetPathEntity?> albums,
|
2023-03-18 07:55:11 -07:00
|
|
|
List<BackupAlbum> backupAlbums,
|
2022-08-18 07:41:59 -07:00
|
|
|
DateTime now,
|
|
|
|
) async {
|
|
|
|
List<AssetEntity> result = [];
|
|
|
|
for (int i = 0; i < albums.length; i++) {
|
|
|
|
final AssetPathEntity? a = albums[i];
|
2023-03-18 07:55:11 -07:00
|
|
|
if (a != null &&
|
|
|
|
a.lastModified?.isBefore(backupAlbums[i].lastBackup) != true) {
|
2022-09-18 14:11:24 -07:00
|
|
|
result.addAll(
|
|
|
|
await a.getAssetListRange(start: 0, end: await a.assetCountAsync),
|
|
|
|
);
|
2023-03-18 07:55:11 -07:00
|
|
|
backupAlbums[i].lastBackup = now;
|
2022-08-18 07:41:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-21 09:29:24 -07:00
|
|
|
/// Returns a new list of assets not yet uploaded
|
|
|
|
Future<List<AssetEntity>> removeAlreadyUploadedAssets(
|
2022-08-18 07:41:59 -07:00
|
|
|
List<AssetEntity> candidates,
|
|
|
|
) async {
|
2022-10-25 07:51:03 -07:00
|
|
|
if (candidates.isEmpty) {
|
|
|
|
return candidates;
|
|
|
|
}
|
2023-03-18 07:55:11 -07:00
|
|
|
final Set<String> duplicatedAssetIds = await getDuplicatedAssetIds();
|
2022-10-25 07:51:03 -07:00
|
|
|
candidates = duplicatedAssetIds.isEmpty
|
|
|
|
? candidates
|
|
|
|
: candidates
|
|
|
|
.whereNot((asset) => duplicatedAssetIds.contains(asset.id))
|
|
|
|
.toList();
|
|
|
|
if (candidates.isEmpty) {
|
|
|
|
return candidates;
|
|
|
|
}
|
|
|
|
final Set<String> existing = {};
|
|
|
|
try {
|
2023-03-22 18:36:44 -07:00
|
|
|
final String deviceId = Store.get(StoreKey.deviceId);
|
2022-10-25 07:51:03 -07:00
|
|
|
final CheckExistingAssetsResponseDto? duplicates =
|
|
|
|
await _apiService.assetApi.checkExistingAssets(
|
|
|
|
CheckExistingAssetsDto(
|
|
|
|
deviceAssetIds: candidates.map((e) => e.id).toList(),
|
|
|
|
deviceId: deviceId,
|
2022-08-18 07:41:59 -07:00
|
|
|
),
|
|
|
|
);
|
2022-10-25 07:51:03 -07:00
|
|
|
if (duplicates != null) {
|
|
|
|
existing.addAll(duplicates.existingIds);
|
|
|
|
}
|
|
|
|
} on ApiException {
|
|
|
|
// workaround for older server versions or when checking for too many assets at once
|
2022-08-18 07:41:59 -07:00
|
|
|
final List<String>? allAssetsInDatabase = await getDeviceBackupAsset();
|
2022-10-25 07:51:03 -07:00
|
|
|
if (allAssetsInDatabase != null) {
|
|
|
|
existing.addAll(allAssetsInDatabase);
|
2022-08-18 07:41:59 -07:00
|
|
|
}
|
|
|
|
}
|
2022-10-25 07:51:03 -07:00
|
|
|
return existing.isEmpty
|
|
|
|
? candidates
|
|
|
|
: candidates.whereNot((e) => existing.contains(e.id)).toList();
|
2022-08-18 07:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> backupAsset(
|
|
|
|
Iterable<AssetEntity> assetList,
|
2022-07-06 14:12:55 -07:00
|
|
|
http.CancellationToken cancelToken,
|
2023-12-10 08:56:39 -07:00
|
|
|
PMProgressHandler? pmProgressHandler,
|
2022-10-25 07:51:03 -07:00
|
|
|
Function(String, String, bool) uploadSuccessCb,
|
2022-07-06 14:12:55 -07:00
|
|
|
Function(int, int) uploadProgressCb,
|
|
|
|
Function(CurrentUploadAsset) setCurrentUploadAssetCb,
|
2023-09-12 20:50:16 -07:00
|
|
|
Function(ErrorUploadAsset) errorCb, {
|
|
|
|
bool sortAssets = false,
|
|
|
|
}) async {
|
2023-12-07 08:53:15 -07:00
|
|
|
final bool isIgnoreIcloudAssets =
|
|
|
|
_appSetting.getSetting(AppSettingsEnum.ignoreIcloudAssets);
|
|
|
|
|
2023-06-26 08:27:32 -07:00
|
|
|
if (Platform.isAndroid &&
|
|
|
|
!(await Permission.accessMediaLocation.status).isGranted) {
|
|
|
|
// double check that permission is granted here, to guard against
|
|
|
|
// uploading corrupt assets without EXIF information
|
|
|
|
_log.warning("Media location permission is not granted. "
|
|
|
|
"Cannot access original assets for backup.");
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-22 18:36:44 -07:00
|
|
|
final String deviceId = Store.get(StoreKey.deviceId);
|
|
|
|
final String savedEndpoint = Store.get(StoreKey.serverEndpoint);
|
2022-02-03 09:06:44 -07:00
|
|
|
File? file;
|
2023-12-11 19:20:36 -07:00
|
|
|
File? livePhotoFile;
|
2022-08-18 07:41:59 -07:00
|
|
|
bool anyErrors = false;
|
2022-10-25 07:51:03 -07:00
|
|
|
final List<String> duplicatedAssetIds = [];
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2023-09-11 03:31:15 -07:00
|
|
|
// DON'T KNOW WHY BUT THIS HELPS BACKGROUND BACKUP TO WORK ON IOS
|
2023-09-19 02:39:54 -07:00
|
|
|
if (Platform.isIOS) {
|
|
|
|
await PhotoManager.requestPermissionExtend();
|
|
|
|
}
|
2023-09-11 03:31:15 -07:00
|
|
|
|
2023-09-12 20:50:16 -07:00
|
|
|
List<AssetEntity> assetsToUpload = sortAssets
|
|
|
|
// Upload images before video assets
|
|
|
|
// these are further sorted by using their creation date
|
|
|
|
? assetList.sorted(
|
|
|
|
(a, b) {
|
|
|
|
final cmp = a.typeInt - b.typeInt;
|
|
|
|
if (cmp != 0) return cmp;
|
|
|
|
return a.createDateTime.compareTo(b.createDateTime);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: assetList.toList();
|
|
|
|
|
|
|
|
for (var entity in assetsToUpload) {
|
2022-02-03 09:06:44 -07:00
|
|
|
try {
|
2023-12-11 19:20:36 -07:00
|
|
|
final isAvailableLocally =
|
|
|
|
await entity.isLocallyAvailable(isOrigin: true);
|
2023-12-07 08:53:15 -07:00
|
|
|
|
|
|
|
// Handle getting files from iCloud
|
|
|
|
if (!isAvailableLocally && Platform.isIOS) {
|
|
|
|
// Skip iCloud assets if the user has disabled this feature
|
|
|
|
if (isIgnoreIcloudAssets) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentUploadAssetCb(
|
|
|
|
CurrentUploadAsset(
|
|
|
|
id: entity.id,
|
|
|
|
fileCreatedAt: entity.createDateTime.year == 1970
|
|
|
|
? entity.modifiedDateTime
|
|
|
|
: entity.createDateTime,
|
|
|
|
fileName: await entity.titleAsync,
|
|
|
|
fileType: _getAssetType(entity.type),
|
|
|
|
iCloudAsset: true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
file = await entity.loadFile(progressHandler: pmProgressHandler);
|
2023-12-11 19:20:36 -07:00
|
|
|
livePhotoFile = await entity.loadFile(
|
|
|
|
withSubtype: true,
|
|
|
|
progressHandler: pmProgressHandler,
|
|
|
|
);
|
2022-02-05 23:07:56 -07:00
|
|
|
} else {
|
2023-12-07 08:53:15 -07:00
|
|
|
if (entity.type == AssetType.video) {
|
|
|
|
file = await entity.originFile;
|
|
|
|
} else {
|
|
|
|
file = await entity.originFile.timeout(const Duration(seconds: 5));
|
2023-12-11 19:20:36 -07:00
|
|
|
if (entity.isLivePhoto) {
|
|
|
|
livePhotoFile = await entity.originFileWithSubtype
|
|
|
|
.timeout(const Duration(seconds: 5));
|
|
|
|
}
|
2023-12-07 08:53:15 -07:00
|
|
|
}
|
2022-02-05 23:07:56 -07:00
|
|
|
}
|
2022-02-03 09:06:44 -07:00
|
|
|
|
|
|
|
if (file != null) {
|
2022-08-18 14:27:08 -07:00
|
|
|
String originalFileName = await entity.titleAsync;
|
2022-06-18 05:36:58 -07:00
|
|
|
var fileStream = file.openRead();
|
|
|
|
var assetRawUploadData = http.MultipartFile(
|
|
|
|
"assetData",
|
|
|
|
fileStream,
|
|
|
|
file.lengthSync(),
|
2023-05-27 18:53:29 -07:00
|
|
|
filename: originalFileName,
|
2022-03-21 23:22:04 -07:00
|
|
|
);
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
var req = MultipartRequest(
|
2022-07-13 05:23:48 -07:00
|
|
|
'POST',
|
|
|
|
Uri.parse('$savedEndpoint/asset/upload'),
|
|
|
|
onProgress: ((bytes, totalBytes) =>
|
|
|
|
uploadProgressCb(bytes, totalBytes)),
|
|
|
|
);
|
2023-03-22 18:36:44 -07:00
|
|
|
req.headers["Authorization"] =
|
|
|
|
"Bearer ${Store.get(StoreKey.accessToken)}";
|
2023-03-28 11:41:55 -07:00
|
|
|
req.headers["Transfer-Encoding"] = "chunked";
|
2022-06-18 05:36:58 -07:00
|
|
|
|
|
|
|
req.fields['deviceAssetId'] = entity.id;
|
|
|
|
req.fields['deviceId'] = deviceId;
|
2023-09-11 03:31:15 -07:00
|
|
|
req.fields['fileCreatedAt'] =
|
|
|
|
entity.createDateTime.toUtc().toIso8601String();
|
2023-03-18 07:55:11 -07:00
|
|
|
req.fields['fileModifiedAt'] =
|
2023-08-24 21:08:19 -07:00
|
|
|
entity.modifiedDateTime.toUtc().toIso8601String();
|
2022-06-18 05:36:58 -07:00
|
|
|
req.fields['isFavorite'] = entity.isFavorite.toString();
|
|
|
|
req.fields['duration'] = entity.videoDuration.toString();
|
|
|
|
|
|
|
|
req.files.add(assetRawUploadData);
|
2022-03-21 23:22:04 -07:00
|
|
|
|
2023-12-11 09:38:02 -07:00
|
|
|
if (entity.isLivePhoto) {
|
2023-12-11 19:20:36 -07:00
|
|
|
if (livePhotoFile != null) {
|
|
|
|
final livePhotoTitle = p.setExtension(
|
|
|
|
originalFileName,
|
|
|
|
p.extension(livePhotoFile.path),
|
|
|
|
);
|
|
|
|
final fileStream = livePhotoFile.openRead();
|
|
|
|
final livePhotoRawUploadData = http.MultipartFile(
|
|
|
|
"livePhotoData",
|
|
|
|
fileStream,
|
|
|
|
livePhotoFile.lengthSync(),
|
|
|
|
filename: livePhotoTitle,
|
|
|
|
);
|
2023-12-11 09:38:02 -07:00
|
|
|
req.files.add(livePhotoRawUploadData);
|
2023-12-11 19:20:36 -07:00
|
|
|
} else {
|
|
|
|
_log.warning(
|
|
|
|
"Failed to obtain motion part of the livePhoto - $originalFileName",
|
|
|
|
);
|
2023-12-11 09:38:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:12:55 -07:00
|
|
|
setCurrentUploadAssetCb(
|
|
|
|
CurrentUploadAsset(
|
|
|
|
id: entity.id,
|
2023-02-19 09:44:53 -07:00
|
|
|
fileCreatedAt: entity.createDateTime.year == 1970
|
2023-01-22 21:40:56 -07:00
|
|
|
? entity.modifiedDateTime
|
|
|
|
: entity.createDateTime,
|
2022-07-06 14:12:55 -07:00
|
|
|
fileName: originalFileName,
|
|
|
|
fileType: _getAssetType(entity.type),
|
2023-12-07 08:53:15 -07:00
|
|
|
iCloudAsset: false,
|
2022-07-06 14:12:55 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-01-30 15:00:03 -07:00
|
|
|
var response =
|
|
|
|
await httpClient.send(req, cancellationToken: cancelToken);
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-10-25 07:51:03 -07:00
|
|
|
if (response.statusCode == 200) {
|
|
|
|
// asset is a duplicate (already exists on the server)
|
|
|
|
duplicatedAssetIds.add(entity.id);
|
|
|
|
uploadSuccessCb(entity.id, deviceId, true);
|
|
|
|
} else if (response.statusCode == 201) {
|
|
|
|
// stored a new asset on the server
|
|
|
|
uploadSuccessCb(entity.id, deviceId, false);
|
2022-07-06 14:12:55 -07:00
|
|
|
} else {
|
|
|
|
var data = await response.stream.bytesToString();
|
|
|
|
var error = jsonDecode(data);
|
|
|
|
|
|
|
|
debugPrint(
|
2022-07-13 05:23:48 -07:00
|
|
|
"Error(${error['statusCode']}) uploading ${entity.id} | $originalFileName | Created on ${entity.createDateTime} | ${error['error']}",
|
|
|
|
);
|
|
|
|
|
|
|
|
errorCb(
|
|
|
|
ErrorUploadAsset(
|
|
|
|
asset: entity,
|
|
|
|
id: entity.id,
|
2023-02-19 09:44:53 -07:00
|
|
|
fileCreatedAt: entity.createDateTime,
|
2022-07-13 05:23:48 -07:00
|
|
|
fileName: originalFileName,
|
|
|
|
fileType: _getAssetType(entity.type),
|
|
|
|
errorMessage: error['error'],
|
|
|
|
),
|
|
|
|
);
|
2022-07-06 14:12:55 -07:00
|
|
|
continue;
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 05:36:58 -07:00
|
|
|
} on http.CancelledException {
|
|
|
|
debugPrint("Backup was cancelled by the user");
|
2022-10-25 07:51:03 -07:00
|
|
|
anyErrors = true;
|
|
|
|
break;
|
2022-02-03 09:06:44 -07:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR backupAsset: ${e.toString()}");
|
2022-08-18 07:41:59 -07:00
|
|
|
anyErrors = true;
|
2022-02-03 09:06:44 -07:00
|
|
|
continue;
|
|
|
|
} finally {
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
file?.deleteSync();
|
2023-12-11 19:20:36 -07:00
|
|
|
livePhotoFile?.deleteSync();
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 07:51:03 -07:00
|
|
|
if (duplicatedAssetIds.isNotEmpty) {
|
2023-03-18 07:55:11 -07:00
|
|
|
await _saveDuplicatedAssetIds(duplicatedAssetIds);
|
2022-10-25 07:51:03 -07:00
|
|
|
}
|
2022-08-18 07:41:59 -07:00
|
|
|
return !anyErrors;
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
String _getAssetType(AssetType assetType) {
|
|
|
|
switch (assetType) {
|
|
|
|
case AssetType.audio:
|
|
|
|
return "AUDIO";
|
|
|
|
case AssetType.image:
|
|
|
|
return "IMAGE";
|
|
|
|
case AssetType.video:
|
|
|
|
return "VIDEO";
|
|
|
|
case AssetType.other:
|
|
|
|
return "OTHER";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 05:36:58 -07:00
|
|
|
|
|
|
|
class MultipartRequest extends http.MultipartRequest {
|
|
|
|
/// Creates a new [MultipartRequest].
|
|
|
|
MultipartRequest(
|
|
|
|
String method,
|
|
|
|
Uri url, {
|
|
|
|
required this.onProgress,
|
|
|
|
}) : super(method, url);
|
|
|
|
|
|
|
|
final void Function(int bytes, int totalBytes) onProgress;
|
|
|
|
|
|
|
|
/// Freezes all mutable fields and returns a
|
|
|
|
/// single-subscription [http.ByteStream]
|
|
|
|
/// that will emit the request body.
|
|
|
|
@override
|
|
|
|
http.ByteStream finalize() {
|
|
|
|
final byteStream = super.finalize();
|
|
|
|
|
|
|
|
final total = contentLength;
|
|
|
|
var bytes = 0;
|
|
|
|
|
|
|
|
final t = StreamTransformer.fromHandlers(
|
|
|
|
handleData: (List<int> data, EventSink<List<int>> sink) {
|
|
|
|
bytes += data.length;
|
|
|
|
onProgress.call(bytes, total);
|
|
|
|
sink.add(data);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
final stream = byteStream.transform(t);
|
|
|
|
return http.ByteStream(stream);
|
|
|
|
}
|
|
|
|
}
|