Fix file move not waiting for download finish

This commit is contained in:
Nutcake 2023-11-14 20:23:03 +01:00
parent 763a426b65
commit ae20d5e869

View file

@ -232,10 +232,12 @@ class _InventoryBrowserAppBarState extends State<InventoryBrowserAppBar> {
} }
return; return;
} }
for (var record in selectedRecords) { for (var record in selectedRecords) {
final uri = selectedUris == thumbUris ? record.thumbnailUri : record.assetUri; final uri = selectedUris == thumbUris ? record.thumbnailUri : record.assetUri;
final filename = final filename =
"${record.id.split("-")[1]}-${record.formattedName.toString()}${extension(uri)}"; "${record.id.split("-")[1]}-${record.formattedName.toString()}${extension(uri)}";
try {
final downloadTask = DownloadTask( final downloadTask = DownloadTask(
url: Aux.resdbToHttp(uri), url: Aux.resdbToHttp(uri),
allowPause: true, allowPause: true,
@ -243,29 +245,38 @@ class _InventoryBrowserAppBarState extends State<InventoryBrowserAppBar> {
filename: filename, filename: filename,
updates: Updates.statusAndProgress, updates: Updates.statusAndProgress,
); );
final downloadStatus = await FileDownloader().enqueue(downloadTask); final downloadStatus = await FileDownloader().download(downloadTask);
if (downloadStatus.status == TaskStatus.complete) {
final tempDirectory = await _tempDirectoryFuture;
final file = File(
"${tempDirectory.path}/${record.id.split("-")[1]}-${record.formattedName.toString()}${extension(uri)}");
if (await file.exists()) {
final newFile = File("$directory/$filename");
await file.copy(newFile.absolute.path);
await file.delete();
}
if (context.mounted) { if (context.mounted) {
if (downloadStatus) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text("Downloaded ${record.formattedName.toString()}"), content: Text("Downloaded ${record.formattedName.toString()}"),
), ),
); );
} else { } else {
throw downloadStatus.exception ?? "Unknown Error";
}
}
} catch (e, s) {
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text("Failed to download ${record.formattedName.toString()}"), content: Text(
"Failed to download '${record.formattedName.toString()}':\n$e",
),
), ),
); );
} }
} }
final tempDirectory = await _tempDirectoryFuture;
final file = File(
"${tempDirectory.path}/${record.id.split("-")[1]}-${record.formattedName.toString()}${extension(uri)}");
if (await file.exists()) {
final newFile = File("$directory/$filename");
await file.rename(newFile.path);
}
} }
iClient.clearSelectedRecords(); iClient.clearSelectedRecords();
}, },