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,40 +232,51 @@ 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)}";
final downloadTask = DownloadTask( try {
url: Aux.resdbToHttp(uri), final downloadTask = DownloadTask(
allowPause: true, url: Aux.resdbToHttp(uri),
baseDirectory: BaseDirectory.temporary, allowPause: true,
filename: filename, baseDirectory: BaseDirectory.temporary,
updates: Updates.statusAndProgress, filename: filename,
); updates: Updates.statusAndProgress,
final downloadStatus = await FileDownloader().enqueue(downloadTask); );
if (context.mounted) { final downloadStatus = await FileDownloader().download(downloadTask);
if (downloadStatus) { 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) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Downloaded ${record.formattedName.toString()}"),
),
);
} 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("Downloaded ${record.formattedName.toString()}"), content: Text(
), "Failed to download '${record.formattedName.toString()}':\n$e",
); ),
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Failed to download ${record.formattedName.toString()}"),
), ),
); );
} }
} }
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();
}, },