2024-09-18 17:15:52 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2025-08-11 15:01:31 -05:00
|
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
|
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart' hide AssetType;
|
2024-09-18 17:15:52 +02:00
|
|
|
import 'package:immich_mobile/repositories/asset_media.repository.dart';
|
|
|
|
|
import 'package:photo_manager/photo_manager.dart' hide AssetType;
|
|
|
|
|
|
2025-07-25 08:07:22 +05:30
|
|
|
final fileMediaRepositoryProvider = Provider((ref) => const FileMediaRepository());
|
2024-09-18 17:15:52 +02:00
|
|
|
|
2025-06-21 14:24:59 -05:00
|
|
|
class FileMediaRepository {
|
2025-06-25 13:06:24 +05:30
|
|
|
const FileMediaRepository();
|
2025-07-29 00:34:03 +05:30
|
|
|
Future<Asset?> saveImage(Uint8List data, {required String title, String? relativePath}) async {
|
|
|
|
|
final entity = await PhotoManager.editor.saveImage(data, filename: title, title: title, relativePath: relativePath);
|
2024-10-21 00:56:02 +07:00
|
|
|
return AssetMediaRepository.toAsset(entity);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 15:01:31 -05:00
|
|
|
Future<LocalAsset?> saveLocalAsset(Uint8List data, {required String title, String? relativePath}) async {
|
|
|
|
|
final entity = await PhotoManager.editor.saveImage(data, filename: title, title: title, relativePath: relativePath);
|
|
|
|
|
|
|
|
|
|
return LocalAsset(
|
|
|
|
|
id: entity.id,
|
|
|
|
|
name: title,
|
|
|
|
|
type: AssetType.image,
|
|
|
|
|
createdAt: entity.createDateTime,
|
|
|
|
|
updatedAt: entity.modifiedDateTime,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
Future<Asset?> saveImageWithFile(String filePath, {String? title, String? relativePath}) async {
|
|
|
|
|
final entity = await PhotoManager.editor.saveImageWithPath(filePath, title: title, relativePath: relativePath);
|
2024-09-18 17:15:52 +02:00
|
|
|
return AssetMediaRepository.toAsset(entity);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
Future<Asset?> saveLivePhoto({required File image, required File video, required String title}) async {
|
|
|
|
|
final entity = await PhotoManager.editor.darwin.saveLivePhoto(imageFile: image, videoFile: video, title: title);
|
2024-09-18 17:15:52 +02:00
|
|
|
return AssetMediaRepository.toAsset(entity);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
Future<Asset?> saveVideo(File file, {required String title, String? relativePath}) async {
|
|
|
|
|
final entity = await PhotoManager.editor.saveVideo(file, title: title, relativePath: relativePath);
|
2024-09-18 17:15:52 +02:00
|
|
|
return AssetMediaRepository.toAsset(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> clearFileCache() => PhotoManager.clearFileCache();
|
|
|
|
|
|
2025-07-25 08:07:22 +05:30
|
|
|
Future<void> enableBackgroundAccess() => PhotoManager.setIgnorePermissionCheck(true);
|
2024-09-18 17:15:52 +02:00
|
|
|
|
2025-07-25 08:07:22 +05:30
|
|
|
Future<void> requestExtendedPermissions() => PhotoManager.requestPermissionExtend();
|
2024-09-18 17:15:52 +02:00
|
|
|
}
|