2023-06-10 20:13:59 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-09-18 17:15:52 +02:00
|
|
|
import 'package:immich_mobile/entities/album.entity.dart';
|
|
|
|
import 'package:immich_mobile/interfaces/album_media.interface.dart';
|
2024-09-24 14:50:21 +02:00
|
|
|
import 'package:immich_mobile/interfaces/asset.interface.dart';
|
2024-09-18 17:15:52 +02:00
|
|
|
import 'package:immich_mobile/repositories/album_media.repository.dart';
|
2024-09-24 14:50:21 +02:00
|
|
|
import 'package:immich_mobile/repositories/asset.repository.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/services/background.service.dart';
|
2024-05-01 04:36:40 +02:00
|
|
|
import 'package:immich_mobile/entities/android_device_asset.entity.dart';
|
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
|
|
|
import 'package:immich_mobile/entities/device_asset.entity.dart';
|
|
|
|
import 'package:immich_mobile/entities/ios_device_asset.entity.dart';
|
2023-11-09 18:19:53 +02:00
|
|
|
import 'package:immich_mobile/extensions/string_extensions.dart';
|
2023-06-10 20:13:59 +02:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
class HashService {
|
2024-09-24 14:50:21 +02:00
|
|
|
HashService(
|
|
|
|
this._assetRepository,
|
|
|
|
this._backgroundService,
|
|
|
|
this._albumMediaRepository,
|
|
|
|
);
|
|
|
|
final IAssetRepository _assetRepository;
|
2023-06-10 20:13:59 +02:00
|
|
|
final BackgroundService _backgroundService;
|
2024-09-18 17:15:52 +02:00
|
|
|
final IAlbumMediaRepository _albumMediaRepository;
|
2023-06-10 20:13:59 +02:00
|
|
|
final _log = Logger('HashService');
|
|
|
|
|
|
|
|
/// Returns all assets that were successfully hashed
|
|
|
|
Future<List<Asset>> getHashedAssets(
|
2024-09-18 17:15:52 +02:00
|
|
|
Album album, {
|
2023-06-10 20:13:59 +02:00
|
|
|
int start = 0,
|
|
|
|
int end = 0x7fffffffffffffff,
|
2024-09-18 17:15:52 +02:00
|
|
|
DateTime? modifiedFrom,
|
|
|
|
DateTime? modifiedUntil,
|
2024-07-02 21:01:54 +02:00
|
|
|
Set<String>? excludedAssets,
|
2023-06-10 20:13:59 +02:00
|
|
|
}) async {
|
2024-09-18 17:15:52 +02:00
|
|
|
final entities = await _albumMediaRepository.getAssets(
|
|
|
|
album.localId!,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
modifiedFrom: modifiedFrom,
|
|
|
|
modifiedUntil: modifiedUntil,
|
|
|
|
);
|
2024-07-02 21:01:54 +02:00
|
|
|
final filtered = excludedAssets == null
|
|
|
|
? entities
|
2024-09-18 17:15:52 +02:00
|
|
|
: entities.where((e) => !excludedAssets.contains(e.localId!)).toList();
|
2024-07-02 21:01:54 +02:00
|
|
|
return _hashAssets(filtered);
|
2023-06-10 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-18 17:15:52 +02:00
|
|
|
/// Processes a list of local [Asset]s, storing their hash and returning only those
|
2023-06-10 20:13:59 +02:00
|
|
|
/// that were successfully hashed. Hashes are looked up in a DB table
|
|
|
|
/// [AndroidDeviceAsset] / [IOSDeviceAsset] by local id. Only missing
|
|
|
|
/// entries are newly hashed and added to the DB table.
|
2024-09-18 17:15:52 +02:00
|
|
|
Future<List<Asset>> _hashAssets(List<Asset> assets) async {
|
2023-06-10 20:13:59 +02:00
|
|
|
const int batchFileCount = 128;
|
|
|
|
const int batchDataSize = 1024 * 1024 * 1024; // 1GB
|
|
|
|
|
2024-09-18 17:15:52 +02:00
|
|
|
final ids = assets
|
|
|
|
.map(Platform.isAndroid ? (a) => a.localId!.toInt() : (a) => a.localId!)
|
2023-06-10 20:13:59 +02:00
|
|
|
.toList();
|
2024-09-24 14:50:21 +02:00
|
|
|
final List<DeviceAsset?> hashes =
|
|
|
|
await _assetRepository.getDeviceAssetsById(ids);
|
2023-06-10 20:13:59 +02:00
|
|
|
final List<DeviceAsset> toAdd = [];
|
|
|
|
final List<String> toHash = [];
|
|
|
|
|
|
|
|
int bytes = 0;
|
|
|
|
|
2024-09-18 17:15:52 +02:00
|
|
|
for (int i = 0; i < assets.length; i++) {
|
2023-06-10 20:13:59 +02:00
|
|
|
if (hashes[i] != null) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-09-21 02:29:07 +02:00
|
|
|
|
|
|
|
File? file;
|
|
|
|
|
|
|
|
try {
|
|
|
|
file = await assets[i].local!.originFile;
|
|
|
|
} catch (error, stackTrace) {
|
|
|
|
_log.warning(
|
|
|
|
"Error getting file to hash for asset ${assets[i].localId}, name: ${assets[i].fileName}, created on: ${assets[i].fileCreatedAt}, skipping",
|
|
|
|
error,
|
|
|
|
stackTrace,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-10 20:13:59 +02:00
|
|
|
if (file == null) {
|
2024-09-18 17:15:52 +02:00
|
|
|
final fileName = assets[i].fileName;
|
2024-04-05 04:28:05 +02:00
|
|
|
|
2023-06-10 20:13:59 +02:00
|
|
|
_log.warning(
|
2024-09-18 17:15:52 +02:00
|
|
|
"Failed to get file for asset ${assets[i].localId}, name: $fileName, created on: ${assets[i].fileCreatedAt}, skipping",
|
2023-06-10 20:13:59 +02:00
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bytes += await file.length();
|
|
|
|
toHash.add(file.path);
|
|
|
|
final deviceAsset = Platform.isAndroid
|
|
|
|
? AndroidDeviceAsset(id: ids[i] as int, hash: const [])
|
|
|
|
: IOSDeviceAsset(id: ids[i] as String, hash: const []);
|
|
|
|
toAdd.add(deviceAsset);
|
|
|
|
hashes[i] = deviceAsset;
|
|
|
|
if (toHash.length == batchFileCount || bytes >= batchDataSize) {
|
|
|
|
await _processBatch(toHash, toAdd);
|
|
|
|
toAdd.clear();
|
|
|
|
toHash.clear();
|
|
|
|
bytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toHash.isNotEmpty) {
|
|
|
|
await _processBatch(toHash, toAdd);
|
|
|
|
}
|
2024-09-18 17:15:52 +02:00
|
|
|
return _getHashedAssets(assets, hashes);
|
2023-06-10 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Processes a batch of files and saves any successfully hashed
|
|
|
|
/// values to the DB table.
|
|
|
|
Future<void> _processBatch(
|
|
|
|
final List<String> toHash,
|
|
|
|
final List<DeviceAsset> toAdd,
|
|
|
|
) async {
|
|
|
|
final hashes = await _hashFiles(toHash);
|
|
|
|
bool anyNull = false;
|
|
|
|
for (int j = 0; j < hashes.length; j++) {
|
|
|
|
if (hashes[j]?.length == 20) {
|
|
|
|
toAdd[j].hash = hashes[j]!;
|
|
|
|
} else {
|
|
|
|
_log.warning("Failed to hash file ${toHash[j]}, skipping");
|
|
|
|
anyNull = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final validHashes = anyNull
|
|
|
|
? toAdd.where((e) => e.hash.length == 20).toList(growable: false)
|
|
|
|
: toAdd;
|
2024-09-24 14:50:21 +02:00
|
|
|
await _assetRepository.upsertDeviceAssets(validHashes);
|
2023-06-10 20:13:59 +02:00
|
|
|
_log.fine("Hashed ${validHashes.length}/${toHash.length} assets");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hashes the given files and returns a list of the same length
|
|
|
|
/// files that could not be hashed have a `null` value
|
|
|
|
Future<List<Uint8List?>> _hashFiles(List<String> paths) async {
|
2024-01-05 18:49:43 +02:00
|
|
|
final List<Uint8List?>? hashes =
|
|
|
|
await _backgroundService.digestFiles(paths);
|
|
|
|
if (hashes == null) {
|
|
|
|
throw Exception("Hashing ${paths.length} files failed");
|
2023-06-10 20:13:59 +02:00
|
|
|
}
|
2024-01-05 18:49:43 +02:00
|
|
|
return hashes;
|
2023-06-10 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-18 17:15:52 +02:00
|
|
|
/// Returns all successfully hashed [Asset]s with their hash value set
|
|
|
|
List<Asset> _getHashedAssets(
|
|
|
|
List<Asset> assets,
|
2023-06-10 20:13:59 +02:00
|
|
|
List<DeviceAsset?> hashes,
|
|
|
|
) {
|
|
|
|
final List<Asset> result = [];
|
|
|
|
for (int i = 0; i < assets.length; i++) {
|
|
|
|
if (hashes[i] != null && hashes[i]!.hash.isNotEmpty) {
|
2024-09-18 17:15:52 +02:00
|
|
|
assets[i].byteHash = hashes[i]!.hash;
|
|
|
|
result.add(assets[i]);
|
2023-06-10 20:13:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final hashServiceProvider = Provider(
|
|
|
|
(ref) => HashService(
|
2024-09-24 14:50:21 +02:00
|
|
|
ref.watch(assetRepositoryProvider),
|
2023-06-10 20:13:59 +02:00
|
|
|
ref.watch(backgroundServiceProvider),
|
2024-09-18 17:15:52 +02:00
|
|
|
ref.watch(albumMediaRepositoryProvider),
|
2023-06-10 20:13:59 +02:00
|
|
|
),
|
|
|
|
);
|