2025-09-18 10:12:37 +05:30
|
|
|
import 'package:flutter/services.dart';
|
2025-06-06 11:23:05 +05:30
|
|
|
import 'package:immich_mobile/constants/constants.dart';
|
2025-09-03 20:28:03 +05:30
|
|
|
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
2025-06-06 11:23:05 +05:30
|
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
2025-06-23 11:27:44 -05:00
|
|
|
import 'package:immich_mobile/infrastructure/repositories/local_album.repository.dart';
|
|
|
|
|
import 'package:immich_mobile/infrastructure/repositories/local_asset.repository.dart';
|
2025-06-06 11:23:05 +05:30
|
|
|
import 'package:immich_mobile/platform/native_sync_api.g.dart';
|
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
2025-09-18 10:12:37 +05:30
|
|
|
const String _kHashCancelledCode = "HASH_CANCELLED";
|
|
|
|
|
|
2025-06-06 11:23:05 +05:30
|
|
|
class HashService {
|
2025-09-18 10:12:37 +05:30
|
|
|
final int _batchSize;
|
2025-06-23 11:27:44 -05:00
|
|
|
final DriftLocalAlbumRepository _localAlbumRepository;
|
|
|
|
|
final DriftLocalAssetRepository _localAssetRepository;
|
2025-06-06 11:23:05 +05:30
|
|
|
final NativeSyncApi _nativeSyncApi;
|
2025-08-28 19:41:54 +05:30
|
|
|
final bool Function()? _cancelChecker;
|
2025-06-06 11:23:05 +05:30
|
|
|
final _log = Logger('HashService');
|
|
|
|
|
|
|
|
|
|
HashService({
|
2025-06-23 11:27:44 -05:00
|
|
|
required DriftLocalAlbumRepository localAlbumRepository,
|
|
|
|
|
required DriftLocalAssetRepository localAssetRepository,
|
2025-06-06 11:23:05 +05:30
|
|
|
required NativeSyncApi nativeSyncApi,
|
2025-08-28 19:41:54 +05:30
|
|
|
bool Function()? cancelChecker,
|
2025-09-18 10:12:37 +05:30
|
|
|
int? batchSize,
|
2025-07-29 00:34:03 +05:30
|
|
|
}) : _localAlbumRepository = localAlbumRepository,
|
|
|
|
|
_localAssetRepository = localAssetRepository,
|
2025-08-28 19:41:54 +05:30
|
|
|
_cancelChecker = cancelChecker,
|
2025-09-18 10:12:37 +05:30
|
|
|
_nativeSyncApi = nativeSyncApi,
|
|
|
|
|
_batchSize = batchSize ?? kBatchHashFileLimit;
|
2025-06-06 11:23:05 +05:30
|
|
|
|
2025-08-28 19:41:54 +05:30
|
|
|
bool get isCancelled => _cancelChecker?.call() ?? false;
|
|
|
|
|
|
2025-06-06 11:23:05 +05:30
|
|
|
Future<void> hashAssets() async {
|
2025-09-03 20:27:30 +05:30
|
|
|
_log.info("Starting hashing of assets");
|
2025-06-06 11:23:05 +05:30
|
|
|
final Stopwatch stopwatch = Stopwatch()..start();
|
2025-09-18 10:12:37 +05:30
|
|
|
try {
|
|
|
|
|
// Sorted by backupSelection followed by isCloud
|
|
|
|
|
final localAlbums = await _localAlbumRepository.getBackupAlbums();
|
|
|
|
|
|
|
|
|
|
for (final album in localAlbums) {
|
|
|
|
|
if (isCancelled) {
|
|
|
|
|
_log.warning("Hashing cancelled. Stopped processing albums.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final assetsToHash = await _localAlbumRepository.getAssetsToHash(album.id);
|
|
|
|
|
if (assetsToHash.isNotEmpty) {
|
|
|
|
|
await _hashAssets(album, assetsToHash);
|
|
|
|
|
}
|
2025-08-28 19:41:54 +05:30
|
|
|
}
|
2025-09-18 10:12:37 +05:30
|
|
|
} on PlatformException catch (e) {
|
|
|
|
|
if (e.code == _kHashCancelledCode) {
|
|
|
|
|
_log.warning("Hashing cancelled by platform");
|
|
|
|
|
return;
|
2025-06-06 11:23:05 +05:30
|
|
|
}
|
2025-09-18 10:12:37 +05:30
|
|
|
} catch (e, s) {
|
|
|
|
|
_log.severe("Error during hashing", e, s);
|
2025-06-06 11:23:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopwatch.stop();
|
|
|
|
|
_log.info("Hashing took - ${stopwatch.elapsedMilliseconds}ms");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Processes a list of [LocalAsset]s, storing their hash and updating the assets in the DB
|
|
|
|
|
/// with hash for those that were successfully hashed. Hashes are looked up in a table
|
|
|
|
|
/// [LocalAssetHashEntity] by local id. Only missing entries are newly hashed and added to the DB.
|
2025-09-03 20:28:03 +05:30
|
|
|
Future<void> _hashAssets(LocalAlbum album, List<LocalAsset> assetsToHash) async {
|
2025-09-18 10:12:37 +05:30
|
|
|
final toHash = <String, LocalAsset>{};
|
2025-06-06 11:23:05 +05:30
|
|
|
|
|
|
|
|
for (final asset in assetsToHash) {
|
2025-08-28 19:41:54 +05:30
|
|
|
if (isCancelled) {
|
|
|
|
|
_log.warning("Hashing cancelled. Stopped processing assets.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 10:12:37 +05:30
|
|
|
toHash[asset.id] = asset;
|
|
|
|
|
if (toHash.length == _batchSize) {
|
2025-09-03 20:28:03 +05:30
|
|
|
await _processBatch(album, toHash);
|
2025-06-06 11:23:05 +05:30
|
|
|
toHash.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 20:28:03 +05:30
|
|
|
await _processBatch(album, toHash);
|
2025-06-06 11:23:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Processes a batch of assets.
|
2025-09-18 10:12:37 +05:30
|
|
|
Future<void> _processBatch(LocalAlbum album, Map<String, LocalAsset> toHash) async {
|
2025-06-06 11:23:05 +05:30
|
|
|
if (toHash.isEmpty) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_log.fine("Hashing ${toHash.length} files");
|
|
|
|
|
|
2025-09-18 10:12:37 +05:30
|
|
|
final hashed = <String, String>{};
|
|
|
|
|
final hashResults = await _nativeSyncApi.hashAssets(
|
|
|
|
|
toHash.keys.toList(),
|
|
|
|
|
allowNetworkAccess: album.backupSelection == BackupSelection.selected,
|
|
|
|
|
);
|
2025-06-09 08:26:44 +05:30
|
|
|
assert(
|
2025-09-18 10:12:37 +05:30
|
|
|
hashResults.length == toHash.length,
|
|
|
|
|
"Hashes length does not match toHash length: ${hashResults.length} != ${toHash.length}",
|
2025-06-09 08:26:44 +05:30
|
|
|
);
|
|
|
|
|
|
2025-09-18 10:12:37 +05:30
|
|
|
for (int i = 0; i < hashResults.length; i++) {
|
2025-08-28 19:41:54 +05:30
|
|
|
if (isCancelled) {
|
|
|
|
|
_log.warning("Hashing cancelled. Stopped processing batch.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 10:12:37 +05:30
|
|
|
final hashResult = hashResults[i];
|
|
|
|
|
if (hashResult.hash != null) {
|
|
|
|
|
hashed[hashResult.assetId] = hashResult.hash!;
|
2025-06-06 11:23:05 +05:30
|
|
|
} else {
|
2025-09-18 10:12:37 +05:30
|
|
|
final asset = toHash[hashResult.assetId];
|
2025-09-03 20:28:03 +05:30
|
|
|
_log.warning(
|
2025-09-18 10:12:37 +05:30
|
|
|
"Failed to hash asset with id: ${hashResult.assetId}, name: ${asset?.name}, createdAt: ${asset?.createdAt}, from album: ${album.name}. Error: ${hashResult.error ?? "unknown"}",
|
2025-09-03 20:28:03 +05:30
|
|
|
);
|
2025-06-06 11:23:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_log.fine("Hashed ${hashed.length}/${toHash.length} assets");
|
|
|
|
|
|
|
|
|
|
await _localAssetRepository.updateHashes(hashed);
|
|
|
|
|
}
|
|
|
|
|
}
|