mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
fix(mobile): cancel share download when dialog is dismissed (#25466)
* fix(mobile): cancel share download when dialog is dismissed * refactor: centralize temporary file cleanup logic * refactor: replace `CancellationToken` with `Completer<void>` for asset sharing cancellation --------- Co-authored-by: cmdpromptcritical <cmdpromptcritical@github.com>
This commit is contained in:
committed by
GitHub
parent
9d8efe2685
commit
ad9f3cfa05
@@ -23,7 +23,6 @@ final assetMediaRepositoryProvider = Provider((ref) => AssetMediaRepository(ref.
|
||||
|
||||
class AssetMediaRepository {
|
||||
final AssetApiRepository _assetApiRepository;
|
||||
|
||||
static final Logger _log = Logger("AssetMediaRepository");
|
||||
|
||||
const AssetMediaRepository(this._assetApiRepository);
|
||||
@@ -58,6 +57,7 @@ class AssetMediaRepository {
|
||||
|
||||
static asset_entity.Asset? toAsset(AssetEntity? local) {
|
||||
if (local == null) return null;
|
||||
|
||||
final asset_entity.Asset asset = asset_entity.Asset(
|
||||
checksum: "",
|
||||
localId: local.id,
|
||||
@@ -72,19 +72,21 @@ class AssetMediaRepository {
|
||||
height: local.height,
|
||||
isFavorite: local.isFavorite,
|
||||
);
|
||||
|
||||
if (asset.fileCreatedAt.year == 1970) {
|
||||
asset.fileCreatedAt = asset.fileModifiedAt;
|
||||
}
|
||||
|
||||
if (local.latitude != null) {
|
||||
asset.exifInfo = ExifInfo(latitude: local.latitude, longitude: local.longitude);
|
||||
}
|
||||
|
||||
asset.local = local;
|
||||
return asset;
|
||||
}
|
||||
|
||||
Future<String?> getOriginalFilename(String id) async {
|
||||
final entity = await AssetEntity.fromId(id);
|
||||
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -101,12 +103,31 @@ class AssetMediaRepository {
|
||||
}
|
||||
}
|
||||
|
||||
/// Deletes temporary files in parallel
|
||||
Future<void> _cleanupTempFiles(List<File> tempFiles) async {
|
||||
await Future.wait(
|
||||
tempFiles.map((file) async {
|
||||
try {
|
||||
await file.delete();
|
||||
} catch (e) {
|
||||
_log.warning("Failed to delete temporary file: ${file.path}", e);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: make this more efficient
|
||||
Future<int> shareAssets(List<BaseAsset> assets, BuildContext context) async {
|
||||
Future<int> shareAssets(List<BaseAsset> assets, BuildContext context, {Completer<void>? cancelCompleter}) async {
|
||||
final downloadedXFiles = <XFile>[];
|
||||
final tempFiles = <File>[];
|
||||
|
||||
for (var asset in assets) {
|
||||
if (cancelCompleter != null && cancelCompleter.isCompleted) {
|
||||
// if cancelled, delete any temp files created so far
|
||||
await _cleanupTempFiles(tempFiles);
|
||||
return 0;
|
||||
}
|
||||
|
||||
final localId = (asset is LocalAsset)
|
||||
? asset.id
|
||||
: asset is RemoteAsset
|
||||
@@ -146,6 +167,11 @@ class AssetMediaRepository {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cancelCompleter != null && cancelCompleter.isCompleted) {
|
||||
await _cleanupTempFiles(tempFiles);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// we dont want to await the share result since the
|
||||
// "preparing" dialog will not disappear until
|
||||
final size = context.sizeData;
|
||||
@@ -154,13 +180,7 @@ class AssetMediaRepository {
|
||||
downloadedXFiles,
|
||||
sharePositionOrigin: Rect.fromPoints(Offset.zero, Offset(size.width / 3, size.height)),
|
||||
).then((result) async {
|
||||
for (var file in tempFiles) {
|
||||
try {
|
||||
await file.delete();
|
||||
} catch (e) {
|
||||
_log.warning("Failed to delete temporary file: ${file.path}", e);
|
||||
}
|
||||
}
|
||||
await _cleanupTempFiles(tempFiles);
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user