2022-02-06 08:07:56 +02:00
|
|
|
import 'dart:async';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
2022-06-25 20:46:51 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
2022-07-06 23:12:55 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/models/check_duplicate_asset_response.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/current_upload_asset.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/error_upload_asset.model.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:immich_mobile/shared/services/network.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/device_info.model.dart';
|
|
|
|
import 'package:immich_mobile/utils/files_helper.dart';
|
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
|
|
|
import 'package:http_parser/http_parser.dart';
|
|
|
|
import 'package:path/path.dart' as p;
|
2022-06-18 14:36:58 +02:00
|
|
|
import 'package:cancellation_token_http/http.dart' as http;
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final backupServiceProvider =
|
|
|
|
Provider((ref) => BackupService(ref.watch(networkServiceProvider)));
|
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
class BackupService {
|
2022-06-25 20:46:51 +02:00
|
|
|
final NetworkService _networkService;
|
2022-07-06 23:12:55 +02:00
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
BackupService(this._networkService);
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
Future<List<String>> getDeviceBackupAsset() async {
|
|
|
|
String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
Response response =
|
|
|
|
await _networkService.getRequest(url: "asset/$deviceId");
|
2022-02-03 18:06:44 +02:00
|
|
|
List<dynamic> result = jsonDecode(response.toString());
|
|
|
|
|
|
|
|
return result.cast<String>();
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:12:55 +02:00
|
|
|
Future<bool> checkDuplicateAsset(String deviceAssetId) async {
|
|
|
|
String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Response response =
|
|
|
|
await _networkService.postRequest(url: "asset/check", data: {
|
|
|
|
"deviceId": deviceId,
|
|
|
|
"deviceAssetId": deviceAssetId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
var result = CheckDuplicateAssetResponse.fromJson(response.toString());
|
|
|
|
|
|
|
|
return result.isExist;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
backupAsset(
|
2022-07-06 23:12:55 +02:00
|
|
|
Set<AssetEntity> assetList,
|
|
|
|
http.CancellationToken cancelToken,
|
|
|
|
Function(String, String) singleAssetDoneCb,
|
|
|
|
Function(int, int) uploadProgressCb,
|
|
|
|
Function(CurrentUploadAsset) setCurrentUploadAssetCb,
|
|
|
|
Function(ErrorUploadAsset) errorCb,
|
|
|
|
) async {
|
2022-02-03 18:06:44 +02:00
|
|
|
String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
|
|
|
String savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
|
|
|
File? file;
|
|
|
|
|
|
|
|
for (var entity in assetList) {
|
|
|
|
try {
|
2022-02-06 08:07:56 +02:00
|
|
|
if (entity.type == AssetType.video) {
|
2022-02-10 04:48:06 +02:00
|
|
|
file = await entity.originFile;
|
2022-02-06 08:07:56 +02:00
|
|
|
} else {
|
2022-02-10 04:48:06 +02:00
|
|
|
file = await entity.originFile.timeout(const Duration(seconds: 5));
|
2022-02-06 08:07:56 +02:00
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
if (file != null) {
|
|
|
|
String originalFileName = await entity.titleAsync;
|
2022-06-25 20:46:51 +02:00
|
|
|
String fileNameWithoutPath =
|
|
|
|
originalFileName.toString().split(".")[0];
|
2022-02-03 18:06:44 +02:00
|
|
|
var fileExtension = p.extension(file.path);
|
|
|
|
var mimeType = FileHelper.getMimeType(file.path);
|
2022-06-18 14:36:58 +02:00
|
|
|
var fileStream = file.openRead();
|
|
|
|
var assetRawUploadData = http.MultipartFile(
|
|
|
|
"assetData",
|
|
|
|
fileStream,
|
|
|
|
file.lengthSync(),
|
2022-03-22 08:22:04 +02:00
|
|
|
filename: fileNameWithoutPath,
|
|
|
|
contentType: MediaType(
|
|
|
|
mimeType["type"],
|
|
|
|
mimeType["subType"],
|
|
|
|
),
|
|
|
|
);
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-06-18 14:36:58 +02:00
|
|
|
var box = Hive.box(userInfoBox);
|
2022-03-22 08:22:04 +02:00
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
var req = MultipartRequest(
|
|
|
|
'POST', Uri.parse('$savedEndpoint/asset/upload'),
|
|
|
|
onProgress: ((bytes, totalBytes) =>
|
2022-07-06 23:12:55 +02:00
|
|
|
uploadProgressCb(bytes, totalBytes)));
|
2022-06-18 14:36:58 +02:00
|
|
|
req.headers["Authorization"] = "Bearer ${box.get(accessTokenKey)}";
|
|
|
|
|
|
|
|
req.fields['deviceAssetId'] = entity.id;
|
|
|
|
req.fields['deviceId'] = deviceId;
|
|
|
|
req.fields['assetType'] = _getAssetType(entity.type);
|
|
|
|
req.fields['createdAt'] = entity.createDateTime.toIso8601String();
|
|
|
|
req.fields['modifiedAt'] = entity.modifiedDateTime.toIso8601String();
|
|
|
|
req.fields['isFavorite'] = entity.isFavorite.toString();
|
|
|
|
req.fields['fileExtension'] = fileExtension;
|
|
|
|
req.fields['duration'] = entity.videoDuration.toString();
|
|
|
|
|
|
|
|
req.files.add(assetRawUploadData);
|
2022-03-22 08:22:04 +02:00
|
|
|
|
2022-07-06 23:12:55 +02:00
|
|
|
setCurrentUploadAssetCb(
|
|
|
|
CurrentUploadAsset(
|
|
|
|
id: entity.id,
|
|
|
|
createdAt: entity.createDateTime,
|
|
|
|
fileName: originalFileName,
|
|
|
|
fileType: _getAssetType(entity.type),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
var response = await req.send(cancellationToken: cancelToken);
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-07-06 23:12:55 +02:00
|
|
|
if (response.statusCode == 201) {
|
2022-02-13 23:10:42 +02:00
|
|
|
singleAssetDoneCb(entity.id, deviceId);
|
2022-07-06 23:12:55 +02:00
|
|
|
} else {
|
|
|
|
var data = await response.stream.bytesToString();
|
|
|
|
var error = jsonDecode(data);
|
|
|
|
|
|
|
|
debugPrint(
|
|
|
|
"Error(${error['statusCode']}) uploading ${entity.id} | $originalFileName | Created on ${entity.createDateTime} | ${error['error']}");
|
|
|
|
|
|
|
|
errorCb(ErrorUploadAsset(
|
|
|
|
asset: entity,
|
|
|
|
id: entity.id,
|
|
|
|
createdAt: entity.createDateTime,
|
|
|
|
fileName: originalFileName,
|
|
|
|
fileType: _getAssetType(entity.type),
|
|
|
|
errorMessage: error['error'],
|
|
|
|
));
|
|
|
|
continue;
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 14:36:58 +02:00
|
|
|
} on http.CancelledException {
|
|
|
|
debugPrint("Backup was cancelled by the user");
|
|
|
|
return;
|
2022-02-03 18:06:44 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR backupAsset: ${e.toString()}");
|
|
|
|
continue;
|
|
|
|
} finally {
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
file?.deleteSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:12:55 +02:00
|
|
|
void sendBackupRequest(AssetEntity entity) {}
|
|
|
|
|
2022-02-03 18:06:44 +02: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-25 20:46:51 +02:00
|
|
|
Future<DeviceInfoRemote> setAutoBackup(
|
|
|
|
bool status, String deviceId, String deviceType) async {
|
2022-02-03 18:06:44 +02:00
|
|
|
var res = await _networkService.patchRequest(url: 'device-info', data: {
|
|
|
|
"isAutoBackup": status,
|
|
|
|
"deviceId": deviceId,
|
|
|
|
"deviceType": deviceType,
|
|
|
|
});
|
|
|
|
|
|
|
|
return DeviceInfoRemote.fromJson(res.toString());
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 14:36:58 +02: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);
|
|
|
|
}
|
|
|
|
}
|