Delete old images when downloading new (#228)

This commit is contained in:
Philip Kristoffersen
2022-09-06 22:31:53 +02:00
committed by GitHub
parent 00848ae5e4
commit 485933767d
+36 -16
View File
@@ -1,6 +1,6 @@
use std::{ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc, thread::Thread,
}; };
use crate::{ use crate::{
@@ -9,6 +9,7 @@ use crate::{
steam::{get_shortcuts_paths, SteamUsersInfo}, steam::{get_shortcuts_paths, SteamUsersInfo},
steamgriddb::{get_image_extension, get_query_type, CachedSearch, ImageType, ToDownload}, steamgriddb::{get_image_extension, get_query_type, CachedSearch, ImageType, ToDownload},
}; };
use config::File;
use dashmap::DashMap; use dashmap::DashMap;
use egui::{ImageButton, ScrollArea}; use egui::{ImageButton, ScrollArea};
use futures::executor::block_on; use futures::executor::block_on;
@@ -521,7 +522,8 @@ impl MyEguiApp {
if let Ok(possible_images) = search_res { if let Ok(possible_images) = search_res {
let mut result = vec![]; let mut result = vec![];
for possible_image in &possible_images { for possible_image in &possible_images {
let path = thumbnails_folder.join(format!("{}.png", possible_image.id)); let ext = get_image_extension(&possible_image.mime);
let path = thumbnails_folder.join(format!("{}.{}",possible_image.id,ext));
result.push(PossibleImage { result.push(PossibleImage {
thumbnail_path: path, thumbnail_path: path,
mime: possible_image.mime.clone(), mime: possible_image.mime.clone(),
@@ -529,8 +531,8 @@ impl MyEguiApp {
full_url: possible_image.url.clone(), full_url: possible_image.url.clone(),
id: possible_image.id, id: possible_image.id,
}); });
let _ = tx.send(FetcStatus::Fetched(result.clone()));
} }
let _ = tx.send(FetcStatus::Fetched(result));
} }
}); });
} }
@@ -545,33 +547,47 @@ impl MyEguiApp {
.image_type_selected .image_type_selected
.as_ref() .as_ref()
.unwrap(); .unwrap();
let selected_image = self let selected_shortcut = self
.image_selected_state .image_selected_state
.selected_shortcut .selected_shortcut
.as_ref() .as_ref()
.unwrap(); .unwrap();
let ext = get_image_extension(&image.mime); let ext = get_image_extension(&image.mime);
let to = Path::new(&user.steam_user_data_folder) let to_download_to_path = Path::new(&user.steam_user_data_folder)
.join("config") .join("config")
.join("grid") .join("grid")
.join(selected_image_type.file_name(selected_image.app_id(), ext)); .join(selected_image_type.file_name(selected_shortcut.app_id(), ext));
//Delete old possible images
let data_folder = Path::new(&user.steam_user_data_folder);
//Keep deleting images of this type untill we don't find any more
let mut path = self.get_shortcut_image_path(data_folder);
while Path::new(&path).exists(){
let _= std::fs::remove_file(&path);
path = self.get_shortcut_image_path(data_folder);
}
//Put the loaded thumbnail into the image handler map, we can use that for preview
let full_image_key = to_download_to_path.to_string_lossy().to_string();
let _ = self.image_selected_state.image_handles.remove(&full_image_key);
let thumbnail_key = image.thumbnail_path.to_string_lossy().to_string();
let thumbnail = self.image_selected_state.image_handles.remove(&thumbnail_key);
if let Some((key,thumbnail)) = thumbnail {
dbg!(&full_image_key);
dbg!(&key);
if to.exists() {
let old_key = to.to_string_lossy().to_string();
let new_key = image.thumbnail_path.to_string_lossy().to_string();
let _ = self.image_selected_state.image_handles.remove(&old_key);
let swap = self.image_selected_state.image_handles.get(&new_key);
if let Some(swp) = swap {
self.image_selected_state self.image_selected_state
.image_handles .image_handles
.insert(old_key, swp.value().clone()); .insert(full_image_key, thumbnail);
}
} }
let app_name = selected_image.name(); let app_name = selected_shortcut.name();
let to_download = ToDownload { let to_download = ToDownload {
path: to, path: to_download_to_path,
url: image.full_url.clone(), url: image.full_url.clone(),
app_name: app_name.to_string(), app_name: app_name.to_string(),
image_type: *selected_image_type, image_type: *selected_image_type,
@@ -587,6 +603,10 @@ impl MyEguiApp {
} }
} }
fn get_shortcut_image_path(&self, data_folder: &Path) -> String {
self.image_selected_state.selected_shortcut.as_ref().unwrap().key(&self.image_selected_state.image_type_selected.unwrap(), data_folder).1
}
fn clear_loaded_images(&mut self) { fn clear_loaded_images(&mut self) {
if let FetcStatus::Fetched(options) = &*self.image_selected_state.image_options.borrow() { if let FetcStatus::Fetched(options) = &*self.image_selected_state.image_options.borrow() {
for option in options { for option in options {