Skip to content

Commit 2b4f1c2

Browse files
committed
feat: add check for downloading package before doing operations
1 parent 4c9b49b commit 2b4f1c2

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wasm/src/dispatcher.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
use std::{
2-
cell::RefCell,
3-
error::Error,
4-
fs,
5-
io::{self},
6-
path::Path,
7-
rc::Rc,
8-
};
1+
use std::{cell::RefCell, error::Error, fs, path::Path, rc::Rc};
92

103
use msfs::{commbus::*, sys::sGaugeDrawData, MSFSEvent};
114
use navigation_database::{
@@ -108,7 +101,7 @@ impl<'a> Dispatcher<'a> {
108101
fn list_packages(&self, sort: bool, filter: bool) -> Vec<PackageInfo> {
109102
let navigation_data_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION);
110103

111-
if !Path::exists(navigation_data_path) {
104+
if !util::path_exists(navigation_data_path) {
112105
fs::create_dir(navigation_data_path).unwrap();
113106
}
114107

@@ -152,6 +145,12 @@ impl<'a> Dispatcher<'a> {
152145
}
153146

154147
fn set_package(&self, uuid: String) -> Result<bool, Box<dyn Error>> {
148+
let download_status = self.downloader.download_status.borrow().clone();
149+
150+
if download_status == DownloadStatus::Downloading {
151+
return Err("Cannot do operations while downloading!".into());
152+
}
153+
155154
let base_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION);
156155

157156
let active_path = base_path.join("active");
@@ -191,6 +190,8 @@ impl<'a> Dispatcher<'a> {
191190

192191
// Check for format change and updates the used interface
193192
if package_info.cycle.format != self.database.borrow().get_database_type().as_str() {
193+
println!("Changing to: {}", package_info.cycle.format);
194+
194195
let new_format = InterfaceFormat::from(&package_info.cycle.format);
195196

196197
self.database.replace(match new_format {
@@ -281,7 +282,7 @@ impl<'a> Dispatcher<'a> {
281282

282283
let cycle_path = file.path().join("cycle.json");
283284

284-
if !Path::exists(&cycle_path) {
285+
if !path_exists(&cycle_path) {
285286
println!(
286287
"[NAVIGRAPH]: Can't find cycle.json in {}",
287288
file.path().to_string_lossy()
@@ -312,8 +313,8 @@ impl<'a> Dispatcher<'a> {
312313
fn copy_old_data(&self) -> Result<(), Box<dyn Error>> {
313314
let old_path = Path::new(consts::NAVIGATION_DATA_OLD_WORK_LOCATION);
314315

315-
if !util::path_exists(old_path) {
316-
return Ok(())
316+
if !path_exists(old_path) {
317+
return Ok(());
317318
}
318319

319320
let new_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION);
@@ -328,7 +329,7 @@ impl<'a> Dispatcher<'a> {
328329
.collect();
329330

330331
if uuid_list.contains(&old_uuid) {
331-
return Ok(())
332+
return Ok(());
332333
}
333334

334335
let fix_file = old_path.join("filethatfixeseverything");
@@ -337,20 +338,35 @@ impl<'a> Dispatcher<'a> {
337338
fs::File::create(fix_file)?;
338339
}
339340

340-
util::copy_files_to_folder(&old_path, &new_path.join(old_uuid))?;
341+
util::copy_files_to_folder(old_path, &new_path.join(old_uuid))?;
341342

342343
util::delete_folder_recursively(old_path, None)?;
343344

344345
Ok(())
345346
}
346347

347-
fn delete_package(&self, uuid: String) -> io::Result<()> {
348+
fn delete_package(&self, uuid: String) -> Result<(), Box<dyn Error>> {
349+
let download_status = self.downloader.download_status.borrow().clone();
350+
351+
if download_status == DownloadStatus::Downloading {
352+
return Err("Cannot do operations while downloading!".into());
353+
}
354+
348355
let package_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION).join(uuid);
349356

350-
util::delete_folder_recursively(&package_path, None)
357+
match util::delete_folder_recursively(&package_path, None) {
358+
Err(err) => Err(err.into()),
359+
Ok(_) => Ok(()),
360+
}
351361
}
352362

353363
fn clean_up_packages(&self, count_max: Option<i32>) -> Result<(), Box<dyn Error>> {
364+
let download_status = self.downloader.download_status.borrow().clone();
365+
366+
if download_status == DownloadStatus::Downloading {
367+
return Err("Cannot do operations while downloading!".into());
368+
}
369+
354370
let bundle_path = Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION);
355371

356372
let mut bundle_ids = vec![];

src/wasm/src/download/zip_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<R: io::Read + io::Seek> ZipFileHandler<R> {
8383

8484
let cycle_path = temp_dir.join("cycle.json");
8585

86-
if !Path::exists(&cycle_path) {
86+
if !util::path_exists(&cycle_path) {
8787
return Err("cycle.json not found".into());
8888
};
8989

0 commit comments

Comments
 (0)