mirror of
https://github.com/djibux/BoilR.git
synced 2026-09-01 05:53:41 +02:00
Download image names based on mime type (#124)
This commit is contained in:
+8
-1
@@ -200,7 +200,14 @@ pub fn get_users_images(user: &SteamUsersInfo) -> Result<Vec<String>, Box<dyn Er
|
||||
let user_folders = std::fs::read_dir(&grid_folder)?;
|
||||
let file_names = user_folders
|
||||
.filter_map(|image| image.ok())
|
||||
.map(|image| image.file_name().to_string_lossy().to_string())
|
||||
.map(|image| {
|
||||
image
|
||||
.path()
|
||||
.file_stem()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
})
|
||||
.collect();
|
||||
Ok(file_names)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ use std::{collections::HashMap, path::Path};
|
||||
use futures::{stream, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::error::Error;
|
||||
use steamgriddb_api::query_parameters::{GridDimentions, Nsfw};
|
||||
use steamgriddb_api::query_parameters::{
|
||||
GridDimentions, MimeType, MimeTypeIcon, MimeTypeLogo, Nsfw,
|
||||
};
|
||||
use tokio::sync::watch::Sender; // 0.3.1
|
||||
|
||||
use steam_shortcuts_util::shortcut::ShortcutOwned;
|
||||
@@ -157,7 +159,7 @@ async fn search_for_images_to_download(
|
||||
// if we are missing any of the images we need to search for them
|
||||
types
|
||||
.iter()
|
||||
.map(|t| t.file_name(s.app_id))
|
||||
.map(|t| t.file_name_no_extension(s.app_id))
|
||||
.any(|image| !known_images.contains(&image))
|
||||
&& !s.app_name.is_empty()
|
||||
});
|
||||
@@ -191,7 +193,7 @@ async fn search_for_images_to_download(
|
||||
.iter()
|
||||
.filter(|s| search_results.contains_key(&s.app_id))
|
||||
.filter(|s| !settings.steamgrid_db.is_image_banned(&image_type, s.app_id))
|
||||
.filter(|s| !known_images.contains(&image_type.file_name(s.app_id)));
|
||||
.filter(|s| !known_images.contains(&image_type.file_name_no_extension(s.app_id)));
|
||||
let image_ids: Vec<usize> = images_needed
|
||||
.clone()
|
||||
.filter_map(|s| search_results.get(&s.app_id))
|
||||
@@ -211,7 +213,12 @@ async fn search_for_images_to_download(
|
||||
.map(|(index, image)| (image, shortcuts[index], image_ids[index]));
|
||||
let download_for_this_type = stream::iter(images)
|
||||
.filter_map(|(image, shortcut, game_id)| {
|
||||
let path = grid_folder.join(image_type.file_name(shortcut.app_id));
|
||||
let extension = image
|
||||
.as_ref()
|
||||
.map(|image| get_image_extension(&image.mime))
|
||||
.unwrap_or("png");
|
||||
let path =
|
||||
grid_folder.join(image_type.file_name(shortcut.app_id, extension));
|
||||
async move {
|
||||
let image_url = match image {
|
||||
Ok(img) => Some(img.url.clone()),
|
||||
@@ -237,6 +244,18 @@ async fn search_for_images_to_download(
|
||||
Ok(to_download)
|
||||
}
|
||||
|
||||
pub fn get_image_extension(mime_type: &steamgriddb_api::images::MimeTypes) -> &'static str {
|
||||
match mime_type {
|
||||
steamgriddb_api::images::MimeTypes::Default(MimeType::Jpeg) => "jpg",
|
||||
steamgriddb_api::images::MimeTypes::Default(MimeType::Png) => "png",
|
||||
steamgriddb_api::images::MimeTypes::Default(MimeType::Webp) => "webp",
|
||||
steamgriddb_api::images::MimeTypes::Logo(MimeTypeLogo::Png) => "png",
|
||||
steamgriddb_api::images::MimeTypes::Logo(MimeTypeLogo::Webp) => "webp",
|
||||
steamgriddb_api::images::MimeTypes::Icon(MimeTypeIcon::Icon) => "ico",
|
||||
steamgriddb_api::images::MimeTypes::Icon(MimeTypeIcon::Png) => "png",
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_images_for_ids(
|
||||
client: &Client,
|
||||
image_ids: &[usize],
|
||||
|
||||
@@ -33,14 +33,19 @@ impl ImageType {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_name(&self, app_id: u32) -> String {
|
||||
pub fn file_name<S: AsRef<str>>(&self, app_id: u32, extension: S) -> String {
|
||||
let file_name = self.file_name_no_extension(app_id);
|
||||
format!("{}.{}", file_name, extension.as_ref())
|
||||
}
|
||||
|
||||
pub fn file_name_no_extension(&self, app_id: u32) -> String {
|
||||
match self {
|
||||
ImageType::Hero => format!("{}_hero.png", app_id),
|
||||
ImageType::Grid => format!("{}p.png", app_id),
|
||||
ImageType::WideGrid => format!("{}.png", app_id),
|
||||
ImageType::Logo => format!("{}_logo.png", app_id),
|
||||
ImageType::BigPicture => format!("{}_bigpicture.png", app_id),
|
||||
ImageType::Icon => format!("{}.ico", app_id),
|
||||
ImageType::Hero => format!("{}_hero", app_id),
|
||||
ImageType::Grid => format!("{}p", app_id),
|
||||
ImageType::WideGrid => format!("{}", app_id),
|
||||
ImageType::Logo => format!("{}_logo", app_id),
|
||||
ImageType::BigPicture => format!("{}_bigpicture", app_id),
|
||||
ImageType::Icon => format!("{}-icon", app_id),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -150,10 +150,13 @@ fn fix_shortcut_icons(
|
||||
&shortcut.exe,
|
||||
&shortcut.app_name,
|
||||
);
|
||||
shortcut.icon = image_folder
|
||||
.join(image_type.file_name(app_id))
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
for ext in ["ico", "png", "jpg", "webp"] {
|
||||
let path = image_folder.join(image_type.file_name(app_id, ext));
|
||||
if path.exists() {
|
||||
shortcut.icon = path.to_string_lossy().to_string();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,13 @@ use std::{
|
||||
use crate::{
|
||||
config::get_thumbnails_folder,
|
||||
steam::{get_shortcuts_paths, SteamUsersInfo},
|
||||
steamgriddb::{get_query_type, CachedSearch, ImageType, ToDownload},
|
||||
steamgriddb::{get_image_extension, get_query_type, CachedSearch, ImageType, ToDownload},
|
||||
};
|
||||
use dashmap::DashMap;
|
||||
use egui::{ImageButton, ScrollArea};
|
||||
use futures::executor::block_on;
|
||||
use steam_shortcuts_util::shortcut::ShortcutOwned;
|
||||
use steamgriddb_api::images::MimeTypes;
|
||||
use tokio::sync::watch::{self, Receiver};
|
||||
|
||||
use super::{ui_images::load_image_from_path, FetcStatus, MyEguiApp};
|
||||
@@ -52,6 +53,7 @@ impl ImageSelectState {
|
||||
pub struct PossibleImage {
|
||||
thumbnail_path: PathBuf,
|
||||
thumbnail_url: String,
|
||||
mime: MimeTypes,
|
||||
full_url: String,
|
||||
id: u32,
|
||||
}
|
||||
@@ -321,12 +323,14 @@ impl MyEguiApp {
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.steam_user_data_folder;
|
||||
for ext in POSSIBLE_EXTENSIONS {
|
||||
let file_name = image_type.file_name(
|
||||
self.image_selected_state
|
||||
.selected_shortcut
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.app_id,
|
||||
ext,
|
||||
);
|
||||
let path = Path::new(data_folder)
|
||||
.join("config")
|
||||
@@ -337,6 +341,7 @@ impl MyEguiApp {
|
||||
}
|
||||
let key = path.to_string_lossy().to_string();
|
||||
self.image_selected_state.image_handles.remove(&key);
|
||||
}
|
||||
self.image_selected_state.image_type_selected = None;
|
||||
}
|
||||
|
||||
@@ -420,6 +425,7 @@ impl MyEguiApp {
|
||||
let path = thumbnails_folder.join(format!("{}.png", possible_image.id));
|
||||
result.push(PossibleImage {
|
||||
thumbnail_path: path,
|
||||
mime: possible_image.mime.clone(),
|
||||
thumbnail_url: possible_image.thumb.clone(),
|
||||
full_url: possible_image.url.clone(),
|
||||
id: possible_image.id,
|
||||
@@ -445,10 +451,12 @@ impl MyEguiApp {
|
||||
.selected_shortcut
|
||||
.as_ref()
|
||||
.unwrap();
|
||||
|
||||
let ext = get_image_extension(&image.mime);
|
||||
let to = Path::new(&user.steam_user_data_folder)
|
||||
.join("config")
|
||||
.join("grid")
|
||||
.join(selected_image_type.file_name(selected_image.app_id));
|
||||
.join(selected_image_type.file_name(selected_image.app_id, ext));
|
||||
|
||||
if to.exists() {
|
||||
let old_key = to.to_string_lossy().to_string();
|
||||
@@ -625,12 +633,28 @@ fn clamp_to_width(size: &mut egui::Vec2, max_width: f32) {
|
||||
trait HasImageKey {
|
||||
fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String);
|
||||
}
|
||||
const POSSIBLE_EXTENSIONS: [&'static str; 4] = ["png", "jpg", "ico", "webp"];
|
||||
|
||||
impl HasImageKey for ShortcutOwned {
|
||||
fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String) {
|
||||
let file_name = image_type.file_name(self.app_id);
|
||||
let path = user_path.join("config").join("grid").join(&file_name);
|
||||
let key = path.to_string_lossy().to_string();
|
||||
let mut keys = POSSIBLE_EXTENSIONS
|
||||
.iter()
|
||||
.map(|ext| key_from_extension(self, image_type, user_path, ext));
|
||||
let first = keys.next().unwrap();
|
||||
let other = keys.filter(|(exsists, _, _)| *exsists).next();
|
||||
let (_, path, key) = other.unwrap_or(first);
|
||||
(path, key)
|
||||
}
|
||||
}
|
||||
|
||||
fn key_from_extension(
|
||||
shortcut: &ShortcutOwned,
|
||||
image_type: &ImageType,
|
||||
user_path: &Path,
|
||||
ext: &str,
|
||||
) -> (bool, PathBuf, String) {
|
||||
let file_name = image_type.file_name(shortcut.app_id, ext);
|
||||
let path = user_path.join("config").join("grid").join(&file_name);
|
||||
let key = path.to_string_lossy().to_string();
|
||||
(path.exists(), path, key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user