mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
Transfer repository from Gitlab
This commit is contained in:
124
mobile/lib/shared/services/backup.service.dart
Normal file
124
mobile/lib/shared/services/backup.service.dart
Normal file
@@ -0,0 +1,124 @@
|
||||
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;
|
||||
import 'package:exif/exif.dart';
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
backupAsset(List<AssetEntity> assetList, CancelToken cancelToken, Function singleAssetDoneCb,
|
||||
Function(int, int) uploadProgress) async {
|
||||
var dio = Dio();
|
||||
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
||||
String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
||||
String savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
||||
File? file;
|
||||
|
||||
for (var entity in assetList) {
|
||||
try {
|
||||
file = await entity.file.timeout(const Duration(seconds: 5));
|
||||
|
||||
if (file != null) {
|
||||
// reading exif
|
||||
// var exifData = await readExifFromFile(file);
|
||||
|
||||
// for (String key in exifData.keys) {
|
||||
// debugPrint("- $key (${exifData[key]?.tagType}): ${exifData[key]}");
|
||||
// }
|
||||
|
||||
// debugPrint("------------------");
|
||||
String originalFileName = await entity.titleAsync;
|
||||
String fileNameWithoutPath = originalFileName.toString().split(".")[0];
|
||||
var fileExtension = p.extension(file.path);
|
||||
LatLng coordinate = await entity.latlngAsync();
|
||||
var mimeType = FileHelper.getMimeType(file.path);
|
||||
var 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,
|
||||
'lat': coordinate.latitude,
|
||||
'lon': coordinate.longitude,
|
||||
'files': [
|
||||
await MultipartFile.fromFile(
|
||||
file.path,
|
||||
filename: fileNameWithoutPath,
|
||||
contentType: MediaType(
|
||||
mimeType["type"],
|
||||
mimeType["subType"],
|
||||
),
|
||||
),
|
||||
]
|
||||
});
|
||||
|
||||
Response res = await dio.post(
|
||||
'$savedEndpoint/asset/upload',
|
||||
data: formData,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: (sent, total) => uploadProgress(sent, total),
|
||||
);
|
||||
|
||||
if (res.statusCode == 201) {
|
||||
singleAssetDoneCb();
|
||||
}
|
||||
}
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
30
mobile/lib/shared/services/device_info.service.dart
Normal file
30
mobile/lib/shared/services/device_info.service.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class DeviceInfoService {
|
||||
Future<Map<String, dynamic>> getDeviceInfo() async {
|
||||
// Get device info
|
||||
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
|
||||
String? deviceId = "";
|
||||
String deviceType = "";
|
||||
|
||||
try {
|
||||
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
|
||||
deviceId = androidInfo.androidId;
|
||||
deviceType = "ANDROID";
|
||||
} catch (e) {
|
||||
debugPrint("Not an android device");
|
||||
}
|
||||
|
||||
try {
|
||||
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
|
||||
deviceId = iosInfo.identifierForVendor;
|
||||
deviceType = "IOS";
|
||||
debugPrint("Device ID: $deviceId");
|
||||
} catch (e) {
|
||||
debugPrint("Not an ios device");
|
||||
}
|
||||
|
||||
return {"deviceId": deviceId, "deviceType": deviceType};
|
||||
}
|
||||
}
|
||||
18
mobile/lib/shared/services/local_storage.service.dart
Normal file
18
mobile/lib/shared/services/local_storage.service.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
|
||||
class LocalStorageService {
|
||||
late Box _box;
|
||||
|
||||
LocalStorageService() {
|
||||
_box = Hive.box(userInfoBox);
|
||||
}
|
||||
|
||||
T get<T>(String key) {
|
||||
return _box.get(key);
|
||||
}
|
||||
|
||||
put<T>(String key, T value) {
|
||||
return _box.put(key, value);
|
||||
}
|
||||
}
|
||||
89
mobile/lib/shared/services/network.service.dart
Normal file
89
mobile/lib/shared/services/network.service.dart
Normal file
@@ -0,0 +1,89 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/utils/dio_http_interceptor.dart';
|
||||
|
||||
class NetworkService {
|
||||
Future<dynamic> getRequest({required String url}) async {
|
||||
try {
|
||||
var dio = Dio();
|
||||
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
||||
|
||||
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
||||
Response res = await dio.get('$savedEndpoint/$url');
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
return res;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
debugPrint("DioError: ${e.response}");
|
||||
} catch (e) {
|
||||
debugPrint("ERROR getRequest: ${e.toString()}");
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> postRequest({required String url, dynamic data}) async {
|
||||
try {
|
||||
var dio = Dio();
|
||||
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
||||
|
||||
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
||||
String validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
||||
Response res = await dio.post(validUrl, data: data);
|
||||
|
||||
return res;
|
||||
} on DioError catch (e) {
|
||||
debugPrint("DioError: ${e.response}");
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint("ERROR BackupService: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> patchRequest({required String url, dynamic data}) async {
|
||||
try {
|
||||
var dio = Dio();
|
||||
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
||||
|
||||
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
||||
|
||||
String validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
||||
Response res = await dio.patch(validUrl, data: data);
|
||||
|
||||
return res;
|
||||
} on DioError catch (e) {
|
||||
debugPrint("DioError: ${e.response}");
|
||||
} catch (e) {
|
||||
debugPrint("ERROR BackupService: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> pingServer() async {
|
||||
try {
|
||||
var dio = Dio();
|
||||
|
||||
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
||||
|
||||
String validUrl = Uri.parse('$savedEndpoint/server-info/ping').toString();
|
||||
|
||||
debugPrint("pint server at url $validUrl");
|
||||
Response res = await dio.get(validUrl);
|
||||
var jsonRespsonse = jsonDecode(res.toString());
|
||||
|
||||
if (jsonRespsonse["res"] == "pong") {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
debugPrint("[PING SERVER] DioError: ${e.response} - $e");
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint("ERROR BackupService: $e");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
mobile/lib/shared/services/server_info.service.dart
Normal file
15
mobile/lib/shared/services/server_info.service.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:immich_mobile/shared/services/network.service.dart';
|
||||
import 'package:immich_mobile/shared/models/server_info.model.dart';
|
||||
|
||||
class ServerInfoService {
|
||||
final NetworkService _networkService = NetworkService();
|
||||
|
||||
Future<ServerInfo> getServerInfo() async {
|
||||
Response response = await _networkService.getRequest(url: 'server-info');
|
||||
|
||||
return ServerInfo.fromJson(response.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user