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';
|
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
import 'package:immich_mobile/shared/services/network.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/device_info.model.dart';
|
|
|
|
import 'package:immich_mobile/utils/dio_http_interceptor.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;
|
|
|
|
|
|
|
|
class BackupService {
|
|
|
|
final NetworkService _networkService = NetworkService();
|
|
|
|
|
|
|
|
Future<List<String>> getDeviceBackupAsset() async {
|
|
|
|
String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
|
|
|
|
|
|
|
Response response = await _networkService.getRequest(url: "asset/$deviceId");
|
|
|
|
List<dynamic> result = jsonDecode(response.toString());
|
|
|
|
|
|
|
|
return result.cast<String>();
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
backupAsset(List<AssetEntity> assetList, CancelToken cancelToken, Function(String, String) singleAssetDoneCb,
|
2022-02-03 18:06:44 +02:00
|
|
|
Function(int, int) uploadProgress) async {
|
|
|
|
var dio = Dio();
|
|
|
|
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
2022-03-22 08:22:04 +02:00
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
|
|
|
String savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
|
|
|
File? file;
|
|
|
|
|
2022-03-22 08:22:04 +02:00
|
|
|
MultipartFile assetRawUploadData;
|
|
|
|
MultipartFile thumbnailUploadData;
|
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
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) {
|
2022-03-22 08:22:04 +02:00
|
|
|
FormData formData;
|
2022-02-03 18:06:44 +02:00
|
|
|
String originalFileName = await entity.titleAsync;
|
|
|
|
String fileNameWithoutPath = originalFileName.toString().split(".")[0];
|
|
|
|
var fileExtension = p.extension(file.path);
|
|
|
|
var mimeType = FileHelper.getMimeType(file.path);
|
2022-03-22 08:22:04 +02:00
|
|
|
assetRawUploadData = await MultipartFile.fromFile(
|
|
|
|
file.path,
|
|
|
|
filename: fileNameWithoutPath,
|
|
|
|
contentType: MediaType(
|
|
|
|
mimeType["type"],
|
|
|
|
mimeType["subType"],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
formData = FormData.fromMap({
|
2022-02-03 18:06:44 +02:00
|
|
|
'deviceAssetId': entity.id,
|
|
|
|
'deviceId': deviceId,
|
|
|
|
'assetType': _getAssetType(entity.type),
|
|
|
|
'createdAt': entity.createDateTime.toIso8601String(),
|
|
|
|
'modifiedAt': entity.modifiedDateTime.toIso8601String(),
|
|
|
|
'isFavorite': entity.isFavorite,
|
|
|
|
'fileExtension': fileExtension,
|
2022-02-06 08:07:56 +02:00
|
|
|
'duration': entity.videoDuration,
|
2022-03-22 08:22:04 +02:00
|
|
|
'assetData': [assetRawUploadData]
|
2022-02-03 18:06:44 +02:00
|
|
|
});
|
|
|
|
|
2022-03-22 08:22:04 +02:00
|
|
|
// Build thumbnail multipart data
|
|
|
|
var thumbnailData = await entity.thumbDataWithSize(1280, 720);
|
|
|
|
if (thumbnailData != null) {
|
|
|
|
thumbnailUploadData = MultipartFile.fromBytes(
|
|
|
|
List.from(thumbnailData),
|
|
|
|
filename: fileNameWithoutPath,
|
|
|
|
contentType: MediaType(
|
|
|
|
"image",
|
|
|
|
"jpeg",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Send thumbnail data if it is exist
|
|
|
|
formData = FormData.fromMap({
|
|
|
|
'deviceAssetId': entity.id,
|
|
|
|
'deviceId': deviceId,
|
|
|
|
'assetType': _getAssetType(entity.type),
|
|
|
|
'createdAt': entity.createDateTime.toIso8601String(),
|
|
|
|
'modifiedAt': entity.modifiedDateTime.toIso8601String(),
|
|
|
|
'isFavorite': entity.isFavorite,
|
|
|
|
'fileExtension': fileExtension,
|
|
|
|
'duration': entity.videoDuration,
|
|
|
|
'thumbnailData': [thumbnailUploadData],
|
|
|
|
'assetData': [assetRawUploadData]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
Response res = await dio.post(
|
|
|
|
'$savedEndpoint/asset/upload',
|
|
|
|
data: formData,
|
|
|
|
cancelToken: cancelToken,
|
|
|
|
onSendProgress: (sent, total) => uploadProgress(sent, total),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res.statusCode == 201) {
|
2022-02-13 23:10:42 +02:00
|
|
|
singleAssetDoneCb(entity.id, deviceId);
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError backupAsset: ${e.response}");
|
|
|
|
break;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR backupAsset: ${e.toString()}");
|
|
|
|
continue;
|
|
|
|
} finally {
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
file?.deleteSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<DeviceInfoRemote> setAutoBackup(bool status, String deviceId, String deviceType) async {
|
|
|
|
var res = await _networkService.patchRequest(url: 'device-info', data: {
|
|
|
|
"isAutoBackup": status,
|
|
|
|
"deviceId": deviceId,
|
|
|
|
"deviceType": deviceType,
|
|
|
|
});
|
|
|
|
|
|
|
|
return DeviceInfoRemote.fromJson(res.toString());
|
|
|
|
}
|
|
|
|
}
|