mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
trashed_local_asset table mirror of local_asset table structure
trashed_local_asset<->local_asset transfer data on move to trash or restore refactor code
This commit is contained in:
parent
b15056deb9
commit
bec1b30554
19 changed files with 1191 additions and 620 deletions
|
|
@ -1,10 +1,13 @@
|
|||
import 'package:collection/collection.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/trashed_asset.model.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/local_asset.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/trashed_local_asset.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/trashed_local_asset.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_asset.repository.dart';
|
||||
|
||||
class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository {
|
||||
final Drift _db;
|
||||
|
|
@ -29,10 +32,10 @@ class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository {
|
|||
|
||||
Future<Iterable<TrashedAsset>> getToHash(String albumId) {
|
||||
final query = _db.trashedLocalAssetEntity.select()..where((r) => r.albumId.equals(albumId) & r.checksum.isNull());
|
||||
return query.map((row) => row.toDto(albumId)).get();
|
||||
return query.map((row) => row.toDto()).get();
|
||||
}
|
||||
|
||||
Future<List<TrashedAsset>> getToRestore() async {
|
||||
Future<Iterable<TrashedAsset>> getToRestore() async {
|
||||
final trashed = _db.trashedLocalAssetEntity;
|
||||
final remote = _db.remoteAssetEntity;
|
||||
final album = _db.localAlbumEntity;
|
||||
|
|
@ -45,10 +48,7 @@ class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository {
|
|||
innerJoin(remote, remote.checksum.equalsExp(trashed.checksum)),
|
||||
])..where(trashed.albumId.isInQuery(selectedAlbumIds) & remote.deletedAt.isNull())).get();
|
||||
|
||||
return rows.map((result) {
|
||||
final assetData = result.readTable(trashed);
|
||||
return assetData.toDto(assetData.albumId);
|
||||
}).toList();
|
||||
return rows.map((result) => result.readTable(trashed).toDto());
|
||||
}
|
||||
|
||||
/// Applies resulted snapshot of trashed assets:
|
||||
|
|
@ -67,12 +67,12 @@ class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository {
|
|||
(a) => TrashedLocalAssetEntityCompanion.insert(
|
||||
id: a.id,
|
||||
albumId: albumId,
|
||||
volume: a.volume == null ? const Value.absent() : Value(a.volume),
|
||||
checksum: a.checksum == null ? const Value.absent() : Value(a.checksum),
|
||||
name: a.name,
|
||||
type: a.type,
|
||||
createdAt: Value(a.createdAt),
|
||||
updatedAt: Value(a.updatedAt),
|
||||
size: a.size == null ? const Value.absent() : Value(a.size),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
@ -102,11 +102,11 @@ class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository {
|
|||
final companions = trashUpdates.map(
|
||||
(a) => TrashedLocalAssetEntityCompanion.insert(
|
||||
id: a.id,
|
||||
volume: a.volume == null ? const Value.absent() : Value(a.volume),
|
||||
albumId: a.albumId,
|
||||
name: a.name,
|
||||
type: a.type,
|
||||
checksum: a.checksum == null ? const Value.absent() : Value(a.checksum),
|
||||
size: a.size == null ? const Value.absent() : Value(a.size),
|
||||
createdAt: Value(a.createdAt),
|
||||
),
|
||||
);
|
||||
|
|
@ -132,14 +132,80 @@ class DriftTrashedLocalAssetRepository extends DriftDatabaseRepository {
|
|||
.map((row) => row.read<int>(t.id.count()) ?? 0);
|
||||
}
|
||||
|
||||
Future<void> delete(Iterable<String> ids) {
|
||||
if (ids.isEmpty) {
|
||||
return Future.value();
|
||||
Future<void> trashLocalAsset(Map<AlbumId, List<LocalAsset>> assetsByAlbums) async {
|
||||
if (assetsByAlbums.isEmpty) {
|
||||
return;
|
||||
}
|
||||
return _db.batch((batch) {
|
||||
for (final slice in ids.slices(32000)) {
|
||||
batch.deleteWhere(_db.trashedLocalAssetEntity, (e) => e.id.isIn(slice));
|
||||
|
||||
final companions = <TrashedLocalAssetEntityCompanion>[];
|
||||
final idToDelete = <String>{};
|
||||
|
||||
assetsByAlbums.forEach((albumId, assets) {
|
||||
for (final asset in assets) {
|
||||
idToDelete.add(asset.id);
|
||||
companions.add(
|
||||
TrashedLocalAssetEntityCompanion(
|
||||
id: Value(asset.id),
|
||||
name: Value(asset.name),
|
||||
albumId: Value(albumId),
|
||||
checksum: asset.checksum == null ? const Value.absent() : Value(asset.checksum),
|
||||
type: Value(asset.type),
|
||||
width: Value(asset.width),
|
||||
height: Value(asset.height),
|
||||
durationInSeconds: Value(asset.durationInSeconds),
|
||||
isFavorite: Value(asset.isFavorite),
|
||||
orientation: Value(asset.orientation),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
await _db.transaction(() async {
|
||||
for (final slice in companions.slices(200)) {
|
||||
await _db.batch((batch) {
|
||||
batch.insertAllOnConflictUpdate(_db.trashedLocalAssetEntity, slice);
|
||||
});
|
||||
}
|
||||
for (final slice in idToDelete.slices(800)) {
|
||||
await (_db.delete(_db.localAssetEntity)..where((e) => e.id.isIn(slice))).go();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> restoreLocalAssets(Iterable<String> ids) async {
|
||||
if (ids.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
final trashedAssets = await (_db.select(_db.trashedLocalAssetEntity)..where((tbl) => tbl.id.isIn(ids))).get();
|
||||
|
||||
if (trashedAssets.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
final localAssets = trashedAssets.map((e) {
|
||||
return LocalAssetEntityCompanion.insert(
|
||||
id: e.id,
|
||||
name: e.name,
|
||||
type: e.type,
|
||||
createdAt: Value(e.createdAt),
|
||||
updatedAt: Value(e.updatedAt),
|
||||
width: Value(e.width),
|
||||
height: Value(e.height),
|
||||
durationInSeconds: Value(e.durationInSeconds),
|
||||
checksum: Value(e.checksum),
|
||||
isFavorite: Value(e.isFavorite),
|
||||
orientation: Value(e.orientation),
|
||||
);
|
||||
}).toList();
|
||||
|
||||
await _db.transaction(() async {
|
||||
await _db.batch((batch) {
|
||||
batch.insertAllOnConflictUpdate(_db.localAssetEntity, localAssets);
|
||||
for (final slice in ids.slices(32000)) {
|
||||
batch.deleteWhere(_db.trashedLocalAssetEntity, (tbl) => tbl.id.isIn(slice));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue