2022-07-13 14:23:48 +02:00
|
|
|
import 'dart:async';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2022-06-25 20:46:51 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/models/exif_info.dart';
|
2023-02-09 19:32:08 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2023-05-25 05:52:43 +02:00
|
|
|
import 'package:immich_mobile/shared/models/user.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/services/sync.service.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
2022-11-30 18:58:07 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
final assetServiceProvider = Provider(
|
|
|
|
(ref) => AssetService(
|
|
|
|
ref.watch(apiServiceProvider),
|
2023-03-04 00:38:30 +02:00
|
|
|
ref.watch(syncServiceProvider),
|
|
|
|
ref.watch(dbProvider),
|
2022-07-13 14:23:48 +02:00
|
|
|
),
|
|
|
|
);
|
2022-06-25 20:46:51 +02:00
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
class AssetService {
|
2022-07-13 14:23:48 +02:00
|
|
|
final ApiService _apiService;
|
2023-03-04 00:38:30 +02:00
|
|
|
final SyncService _syncService;
|
2022-11-30 18:58:07 +02:00
|
|
|
final log = Logger('AssetService');
|
2023-03-04 00:38:30 +02:00
|
|
|
final Isar _db;
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
AssetService(
|
|
|
|
this._apiService,
|
|
|
|
this._syncService,
|
|
|
|
this._db,
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Checks the server for updated assets and updates the local database if
|
|
|
|
/// required. Returns `true` if there were any changes.
|
2023-05-25 05:52:43 +02:00
|
|
|
Future<bool> refreshRemoteAssets([User? user]) async {
|
2023-09-10 14:51:18 +02:00
|
|
|
user ??= Store.get<User>(StoreKey.currentUser);
|
2023-03-04 00:38:30 +02:00
|
|
|
final Stopwatch sw = Stopwatch()..start();
|
2023-03-27 04:35:52 +02:00
|
|
|
final bool changes = await _syncService.syncRemoteAssetsToDb(
|
2023-05-25 05:52:43 +02:00
|
|
|
user,
|
2023-09-10 14:51:18 +02:00
|
|
|
_getRemoteAssetChanges,
|
|
|
|
_getRemoteAssets,
|
2023-03-27 04:35:52 +02:00
|
|
|
);
|
2023-03-04 00:38:30 +02:00
|
|
|
debugPrint("refreshRemoteAssets full took ${sw.elapsedMilliseconds}ms");
|
|
|
|
return changes;
|
|
|
|
}
|
2022-02-07 04:31:32 +02:00
|
|
|
|
2023-09-10 14:51:18 +02:00
|
|
|
/// Returns `(null, null)` if changes are invalid -> requires full sync
|
|
|
|
Future<(List<Asset>? toUpsert, List<String>? toDelete)>
|
|
|
|
_getRemoteAssetChanges(User user, DateTime since) async {
|
|
|
|
final deleted = await _apiService.auditApi
|
|
|
|
.getAuditDeletes(EntityType.ASSET, since, userId: user.id);
|
|
|
|
if (deleted == null || deleted.needsFullSync) return (null, null);
|
|
|
|
final assetDto = await _apiService.assetApi
|
|
|
|
.getAllAssets(userId: user.id, updatedAfter: since);
|
|
|
|
if (assetDto == null) return (null, null);
|
|
|
|
return (assetDto.map(Asset.remote).toList(), deleted.ids);
|
|
|
|
}
|
|
|
|
|
2022-11-26 18:16:02 +02:00
|
|
|
/// Returns `null` if the server state did not change, else list of assets
|
2023-09-10 14:51:18 +02:00
|
|
|
Future<List<Asset>?> _getRemoteAssets(User user) async {
|
2022-11-30 18:58:07 +02:00
|
|
|
try {
|
2023-09-10 14:51:18 +02:00
|
|
|
final List<AssetResponseDto>? assets =
|
|
|
|
await _apiService.assetApi.getAllAssets(userId: user.id);
|
2023-05-21 03:41:34 +02:00
|
|
|
if (assets == null) {
|
2023-03-04 00:38:30 +02:00
|
|
|
return null;
|
2023-05-25 05:52:43 +02:00
|
|
|
} else if (assets.isNotEmpty && assets.first.ownerId != user.id) {
|
|
|
|
log.warning("Make sure that server and app versions match!"
|
|
|
|
" The server returned assets for user ${assets.first.ownerId}"
|
|
|
|
" while requesting assets of user ${user.id}");
|
|
|
|
return null;
|
2023-03-04 00:38:30 +02:00
|
|
|
}
|
2023-05-25 05:52:43 +02:00
|
|
|
return assets.map(Asset.remote).toList();
|
2023-06-26 01:59:35 +02:00
|
|
|
} catch (error, stack) {
|
|
|
|
log.severe(
|
|
|
|
'Error while getting remote assets: ${error.toString()}',
|
|
|
|
error,
|
|
|
|
stack,
|
|
|
|
);
|
2023-03-04 00:38:30 +02:00
|
|
|
return null;
|
2022-02-13 23:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
Future<List<DeleteAssetResponseDto>?> deleteAssets(
|
2023-02-04 22:42:42 +02:00
|
|
|
Iterable<Asset> deleteAssets,
|
2022-07-13 14:23:48 +02:00
|
|
|
) async {
|
2022-02-13 23:10:42 +02:00
|
|
|
try {
|
2022-11-08 19:00:24 +02:00
|
|
|
final List<String> payload = [];
|
2022-02-13 23:10:42 +02:00
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
for (final asset in deleteAssets) {
|
2023-02-04 22:42:42 +02:00
|
|
|
payload.add(asset.remoteId!);
|
2022-02-13 23:10:42 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
return await _apiService.assetApi
|
|
|
|
.deleteAsset(DeleteAssetDto(ids: payload));
|
2023-06-26 01:59:35 +02:00
|
|
|
} catch (error, stack) {
|
|
|
|
log.severe("Error deleteAssets ${error.toString()}", error, stack);
|
2022-02-11 04:40:11 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-02-05 05:25:11 +02:00
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
/// Loads the exif information from the database. If there is none, loads
|
|
|
|
/// the exif info from the server (remote assets only)
|
|
|
|
Future<Asset> loadExif(Asset a) async {
|
|
|
|
a.exifInfo ??= await _db.exifInfos.get(a.id);
|
2023-05-17 19:36:02 +02:00
|
|
|
// fileSize is always filled on the server but not set on client
|
|
|
|
if (a.exifInfo?.fileSize == null) {
|
2023-03-04 00:38:30 +02:00
|
|
|
if (a.isRemote) {
|
|
|
|
final dto = await _apiService.assetApi.getAssetById(a.remoteId!);
|
|
|
|
if (dto != null && dto.exifInfo != null) {
|
2023-05-17 19:36:02 +02:00
|
|
|
final newExif = Asset.remote(dto).exifInfo!.copyWith(id: a.id);
|
|
|
|
if (newExif != a.exifInfo) {
|
|
|
|
if (a.isInDb) {
|
|
|
|
_db.writeTxn(() => a.put(_db));
|
|
|
|
} else {
|
|
|
|
debugPrint("[loadExif] parameter Asset is not from DB!");
|
|
|
|
}
|
2023-03-04 00:38:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO implement local exif info parsing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
Future<List<Asset?>> updateAssets(
|
|
|
|
List<Asset> assets,
|
2023-02-09 19:32:08 +02:00
|
|
|
UpdateAssetDto updateAssetDto,
|
|
|
|
) async {
|
2023-05-17 19:36:02 +02:00
|
|
|
final List<AssetResponseDto?> dtos = await Future.wait(
|
|
|
|
assets.map(
|
|
|
|
(a) => _apiService.assetApi.updateAsset(a.remoteId!, updateAssetDto),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
bool allInDb = true;
|
|
|
|
for (int i = 0; i < assets.length; i++) {
|
|
|
|
final dto = dtos[i], old = assets[i];
|
|
|
|
if (dto != null) {
|
|
|
|
final remote = Asset.remote(dto);
|
|
|
|
if (old.canUpdate(remote)) {
|
|
|
|
assets[i] = old.updatedCopy(remote);
|
|
|
|
}
|
|
|
|
allInDb &= assets[i].isInDb;
|
2023-04-17 07:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-17 19:36:02 +02:00
|
|
|
final toUpdate = allInDb ? assets : assets.where((e) => e.isInDb).toList();
|
|
|
|
await _syncService.upsertAssetsWithExif(toUpdate);
|
|
|
|
return assets;
|
2023-02-05 05:25:11 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
Future<List<Asset?>> changeFavoriteStatus(
|
|
|
|
List<Asset> assets,
|
|
|
|
bool isFavorite,
|
|
|
|
) {
|
|
|
|
return updateAssets(assets, UpdateAssetDto(isFavorite: isFavorite));
|
2023-02-05 05:25:11 +02:00
|
|
|
}
|
2023-04-17 07:02:07 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
Future<List<Asset?>> changeArchiveStatus(List<Asset> assets, bool isArchive) {
|
|
|
|
return updateAssets(assets, UpdateAssetDto(isArchived: isArchive));
|
2023-04-17 07:02:07 +02:00
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|