game_manager_lib\services/
images.rs1use crate::errors::AppError;
7use std::fs;
8use std::path::PathBuf;
9use tauri::{AppHandle, Manager};
10
11fn get_covers_dir(app: &AppHandle) -> Result<PathBuf, AppError> {
13 let app_data_dir = app.path().app_data_dir().map_err(|e| {
15 AppError::IoError(
16 std::io::Error::new(std::io::ErrorKind::NotFound, e.to_string()).to_string(),
17 )
18 })?;
19
20 let covers_dir = app_data_dir.join("covers");
21
22 if !covers_dir.exists() {
24 fs::create_dir_all(&covers_dir).map_err(|e| AppError::IoError(e.to_string()))?;
25 }
26
27 Ok(covers_dir)
28}
29
30#[tauri::command]
34pub async fn cache_cover_image(
35 app: AppHandle,
36 url: String,
37 game_id: String,
38) -> Result<String, AppError> {
39 let covers_dir = get_covers_dir(&app)?;
41 let file_name = format!("{}.jpg", game_id);
43 let file_path = covers_dir.join(&file_name);
44
45 if file_path.exists() {
47 return Ok(file_path.to_string_lossy().to_string());
48 }
49
50 let response = reqwest::get(&url)
53 .await
54 .map_err(|e| AppError::NetworkError(e.to_string()))?;
55
56 let bytes = response
57 .bytes()
58 .await
59 .map_err(|e| AppError::NetworkError(e.to_string()))?;
60
61 fs::write(&file_path, bytes).map_err(|e| AppError::IoError(e.to_string()))?;
63
64 Ok(file_path.to_string_lossy().to_string())
65}
66
67#[tauri::command]
71pub fn check_local_cover(app: AppHandle, game_id: String) -> Option<String> {
72 let covers_dir = get_covers_dir(&app).ok()?;
73 let path = covers_dir.join(format!("{}.jpg", game_id));
74
75 if path.exists() {
76 Some(path.to_string_lossy().to_string())
77 } else {
78 None
79 }
80}
81
82#[tauri::command]
84pub fn clear_cover_cache(app: AppHandle) -> Result<String, AppError> {
85 let covers_dir = get_covers_dir(&app)?;
86
87 if covers_dir.exists() {
89 fs::remove_dir_all(&covers_dir).map_err(|e| AppError::IoError(e.to_string()))?;
90 fs::create_dir_all(&covers_dir).map_err(|e| AppError::IoError(e.to_string()))?;
91 }
92
93 Ok("Cache de imagens limpo com sucesso.".to_string())
94}