High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
fix(mobile): Fix minor issues with downloading assets (#13609)
dvbthienee0130a58b36
diff --git a/mobile/lib/interfaces/file_media.interface.dart b/mobile/lib/interfaces/file_media.interface.dartindex c898183d79..ea01819dc3 100644--- a/mobile/lib/interfaces/file_media.interface.dart+++ b/mobile/lib/interfaces/file_media.interface.dart@@ -10,6 +10,12 @@ abstract interface class IFileMediaRepository {String? relativePath,});+ Future<Asset?> saveImageWithFile(+ String filePath, {+ String? title,+ String? relativePath,+ });+Future<Asset?> saveVideo(File file, {required String title,diff --git a/mobile/lib/main.dart b/mobile/lib/main.dartindex 40eda30204..48bc936a82 100644--- a/mobile/lib/main.dart+++ b/mobile/lib/main.dart@@ -83,7 +83,7 @@ Future<void> initApp() async {};PlatformDispatcher.instance.onError = (error, stack) {- debugPrint("FlutterError - Catch all: $error");+ debugPrint("FlutterError - Catch all: $error \n $stack");log.severe('PlatformDispatcher - Catch all', error, stack);return true;};diff --git a/mobile/lib/providers/asset_viewer/download.provider.dart b/mobile/lib/providers/asset_viewer/download.provider.dartindex d4aa2823b5..68b120c38a 100644--- a/mobile/lib/providers/asset_viewer/download.provider.dart+++ b/mobile/lib/providers/asset_viewer/download.provider.dart@@ -6,6 +6,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';import 'package:immich_mobile/extensions/build_context_extensions.dart';import 'package:immich_mobile/models/download/download_state.model.dart';import 'package:immich_mobile/models/download/livephotos_medatada.model.dart';+import 'package:immich_mobile/services/album.service.dart';import 'package:immich_mobile/services/download.service.dart';import 'package:immich_mobile/entities/asset.entity.dart';import 'package:immich_mobile/services/share.service.dart';@@ -15,10 +16,12 @@ import 'package:immich_mobile/widgets/common/share_dialog.dart';class DownloadStateNotifier extends StateNotifier<DownloadState> {final DownloadService _downloadService;final ShareService _shareService;+ final AlbumService _albumService;DownloadStateNotifier(this._downloadService,this._shareService,+ this._albumService,) : super(DownloadState(downloadStatus: TaskStatus.complete,@@ -76,7 +79,7 @@ class DownloadStateNotifier extends StateNotifier<DownloadState> {switch (update.status) {case TaskStatus.complete:- _downloadService.saveImage(update.task);+ _downloadService.saveImageWithPath(update.task);_onDownloadComplete(update.task.taskId);break;@@ -133,6 +136,7 @@ class DownloadStateNotifier extends StateNotifier<DownloadState> {showProgress: false,);}+ _albumService.refreshDeviceAlbums();});}@@ -187,5 +191,6 @@ final downloadStateProvider =((ref) => DownloadStateNotifier(ref.watch(downloadServiceProvider),ref.watch(shareServiceProvider),+ ref.watch(albumServiceProvider),)),);diff --git a/mobile/lib/repositories/file_media.repository.dart b/mobile/lib/repositories/file_media.repository.dartindex 5612b378c3..15f7a51e15 100644--- a/mobile/lib/repositories/file_media.repository.dart+++ b/mobile/lib/repositories/file_media.repository.dart@@ -25,6 +25,20 @@ class FileMediaRepository implements IFileMediaRepository {return AssetMediaRepository.toAsset(entity);}+ @override+ Future<Asset?> saveImageWithFile(+ String filePath, {+ String? title,+ String? relativePath,+ }) async {+ final entity = await PhotoManager.editor.saveImageWithPath(+ filePath,+ title: title,+ relativePath: relativePath,+ );+ return AssetMediaRepository.toAsset(entity);+ }+@overrideFuture<Asset?> saveLivePhoto({required File image,diff --git a/mobile/lib/services/download.service.dart b/mobile/lib/services/download.service.dartindex 996cbe61f1..7cf6f309e9 100644--- a/mobile/lib/services/download.service.dart+++ b/mobile/lib/services/download.service.dart@@ -1,7 +1,7 @@import 'dart:io';import 'package:background_downloader/background_downloader.dart';-import 'package:flutter/material.dart';+import 'package:flutter/services.dart';import 'package:hooks_riverpod/hooks_riverpod.dart';import 'package:immich_mobile/entities/store.entity.dart';import 'package:immich_mobile/entities/asset.entity.dart';@@ -12,6 +12,7 @@ import 'package:immich_mobile/repositories/download.repository.dart';import 'package:immich_mobile/repositories/file_media.repository.dart';import 'package:immich_mobile/services/api.service.dart';import 'package:immich_mobile/utils/download.dart';+import 'package:logging/logging.dart';final downloadServiceProvider = Provider((ref) => DownloadService(@@ -23,6 +24,7 @@ final downloadServiceProvider = Provider(class DownloadService {final IDownloadRepository _downloadRepository;final IFileMediaRepository _fileMediaRepository;+ final Logger _log = Logger("DownloadService");void Function(TaskStatusUpdate)? onImageDownloadStatus;void Function(TaskStatusUpdate)? onVideoDownloadStatus;void Function(TaskStatusUpdate)? onLivePhotoDownloadStatus;@@ -55,19 +57,25 @@ class DownloadService {onLivePhotoDownloadStatus?.call(update);}- Future<bool> saveImage(Task task) async {+ Future<bool> saveImageWithPath(Task task) async {final filePath = await task.filePath();final title = task.filename;final relativePath = Platform.isAndroid ? 'DCIM/Immich' : null;- final data = await File(filePath).readAsBytes();-- final Asset? resultAsset = await _fileMediaRepository.saveImage(- data,- title: title,- relativePath: relativePath,- );-- return resultAsset != null;+ try {+ final Asset? resultAsset = await _fileMediaRepository.saveImageWithFile(+ filePath,+ title: title,+ relativePath: relativePath,+ );+ return resultAsset != null;+ } catch (error, stack) {+ _log.severe("Error saving image", error, stack);+ return false;+ } finally {+ if (await File(filePath).exists()) {+ await File(filePath).delete();+ }+ }}Future<bool> saveVideo(Task task) async {@@ -75,58 +83,74 @@ class DownloadService {final title = task.filename;final relativePath = Platform.isAndroid ? 'DCIM/Immich' : null;final file = File(filePath);-- final Asset? resultAsset = await _fileMediaRepository.saveVideo(- file,- title: title,- relativePath: relativePath,- );-- return resultAsset != null;+ try {+ final Asset? resultAsset = await _fileMediaRepository.saveVideo(+ file,+ title: title,+ relativePath: relativePath,+ );+ return resultAsset != null;+ } catch (error, stack) {+ _log.severe("Error saving video", error, stack);+ return false;+ } finally {+ if (await file.exists()) {+ await file.delete();+ }+ }}Future<bool> saveLivePhotos(Task task,String livePhotosId,) async {- try {- final records = await _downloadRepository.getLiveVideoTasks();- if (records.length < 2) {- return false;- }-- final imageRecord = records.firstWhere(- (record) {- final metadata = LivePhotosMetadata.fromJson(record.task.metaData);- return metadata.id == livePhotosId &&- metadata.part == LivePhotosPart.image;- },- );-- final videoRecord = records.firstWhere((record) {- final metadata = LivePhotosMetadata.fromJson(record.task.metaData);- return metadata.id == livePhotosId &&- metadata.part == LivePhotosPart.video;- });+ final records = await _downloadRepository.getLiveVideoTasks();+ if (records.length < 2) {+ return false;+ }- final imageFilePath = await imageRecord.task.filePath();- final videoFilePath = await videoRecord.task.filePath();+ final imageRecord =+ _findTaskRecord(records, livePhotosId, LivePhotosPart.image);+ final videoRecord =+ _findTaskRecord(records, livePhotosId, LivePhotosPart.video);+ final imageFilePath = await imageRecord.task.filePath();+ final videoFilePath = await videoRecord.task.filePath();- final resultAsset = await _fileMediaRepository.saveLivePhoto(+ try {+ final result = await _fileMediaRepository.saveLivePhoto(image: File(imageFilePath),video: File(videoFilePath),title: task.filename,);+ return result != null;+ } on PlatformException catch (error, stack) {+ // Handle saving MotionPhotos on iOS+ if (error.code == 'PHPhotosErrorDomain (-1)') {+ final result = await _fileMediaRepository+ .saveImageWithFile(imageFilePath, title: task.filename);+ return result != null;+ }+ _log.severe("Error saving live photo", error, stack);+ return false;+ } catch (error, stack) {+ _log.severe("Error saving live photo", error, stack);+ return false;+ } finally {+ final imageFile = File(imageFilePath);+ if (await imageFile.exists()) {+ await imageFile.delete();+ }++ final videoFile = File(videoFilePath);+ if (await videoFile.exists()) {+ await videoFile.delete();+ }+await _downloadRepository.deleteRecordsWithIds([imageRecord.task.taskId,videoRecord.task.taskId,]);-- return resultAsset != null;- } catch (error) {- debugPrint("Error saving live photo: $error");- return false;}}@@ -151,7 +175,9 @@ class DownloadService {await _downloadRepository.download(_buildDownloadTask(asset.livePhotoVideoId!,- asset.fileName.toUpperCase().replaceAll(".HEIC", '.MOV'),+ asset.fileName+ .toUpperCase()+ .replaceAll(RegExp(r"\.(JPG|HEIC)$"), '.MOV'),group: downloadGroupLivePhoto,metadata: LivePhotosMetadata(part: LivePhotosPart.video,@@ -191,3 +217,14 @@ class DownloadService {);}}++TaskRecord _findTaskRecord(+ List<TaskRecord> records,+ String livePhotosId,+ LivePhotosPart part,+) {+ return records.firstWhere((record) {+ final metadata = LivePhotosMetadata.fromJson(record.task.metaData);+ return metadata.id == livePhotosId && metadata.part == part;+ });+}diff --git a/mobile/lib/widgets/forms/login/login_form.dart b/mobile/lib/widgets/forms/login/login_form.dartindex 46e8671858..51383fe195 100644--- a/mobile/lib/widgets/forms/login/login_form.dart+++ b/mobile/lib/widgets/forms/login/login_form.dart@@ -176,7 +176,7 @@ class LoginForm extends HookConsumerWidget {populateTestLoginInfo1() {usernameController.text = '[email protected]';passwordController.text = 'password';- serverEndpointController.text = 'http://192.168.1.118:2283/api';+ serverEndpointController.text = 'http://10.1.15.216:2283/api';}login() async {diff --git a/mobile/pubspec.lock b/mobile/pubspec.lockindex 1f29bf7830..fe40017ba8 100644--- a/mobile/pubspec.lock+++ b/mobile/pubspec.lock@@ -1211,18 +1211,18 @@ packages:dependency: "direct main"description:name: photo_manager- sha256: "32a1ce1095aeaaa792a29f28c1f74613aa75109f21c2d4ab85be3ad9964230a4"+ sha256: "70159eee32203e8162d49d588232f0299ed3f383c63eef1e899cb6b83dee6b26"url: "https://pub.dev"source: hosted- version: "3.5.0"+ version: "3.5.1"photo_manager_image_provider:dependency: "direct main"description:name: photo_manager_image_provider- sha256: "38ef1023dc11de3a8669f16e7c981673b3c5cfee715d17120f4b87daa2cdd0af"+ sha256: b6015b67b32f345f57cf32c126f871bced2501236c405aafaefa885f7c821e4furl: "https://pub.dev"source: hosted- version: "2.1.1"+ version: "2.2.0"platform:dependency: transitivedescription:diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yamlindex 703736e3d9..5177e64bcf 100644--- a/mobile/pubspec.yaml+++ b/mobile/pubspec.yaml@@ -13,8 +13,8 @@ dependencies:sdk: flutterpath_provider_ios:- photo_manager: ^3.5.0- photo_manager_image_provider: ^2.1.1+ photo_manager: ^3.5.1+ photo_manager_image_provider: ^2.2.0flutter_hooks: ^0.20.4hooks_riverpod: ^2.4.9riverpod_annotation: ^2.3.3