[fractal] API, util.rs: remove cache_path(), use cache_dir_path() directly



commit 61b631795c1ab18acdee1ebedb7cf81a66bf41c8
Author: Alejandro Domínguez <adomu net-c com>
Date:   Thu Aug 29 02:50:32 2019 +0200

    API, util.rs: remove cache_path(), use cache_dir_path() directly

 fractal-gtk/src/appop/room.rs               |  8 ++++----
 fractal-gtk/src/cache/state.rs              |  8 +++++---
 fractal-gtk/src/widgets/avatar.rs           |  6 +++---
 fractal-matrix-api/src/backend/directory.rs |  4 ++--
 fractal-matrix-api/src/backend/media.rs     |  2 +-
 fractal-matrix-api/src/backend/room.rs      |  4 ++--
 fractal-matrix-api/src/util.rs              | 28 +++++++++++++---------------
 7 files changed, 30 insertions(+), 30 deletions(-)
---
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 14ae2d4c..2a408ecd 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -11,7 +11,7 @@ use crate::appop::AppOp;
 
 use crate::backend;
 use crate::backend::BKCommand;
-use fractal_api::util::cache_path;
+use fractal_api::util::cache_dir_path;
 
 use crate::actions;
 use crate::actions::AppState;
@@ -349,7 +349,7 @@ impl AppOp {
 
     pub fn set_room_avatar(&mut self, roomid: String, avatar: Option<Url>) {
         if avatar.is_none() {
-            if let Ok(dest) = cache_path(&roomid) {
+            if let Ok(dest) = cache_dir_path(None, &roomid) {
                 let _ = remove_file(dest);
             }
         }
@@ -360,8 +360,8 @@ impl AppOp {
                         if m != uid {
                             //FIXME: Find a better solution
                             // create a symlink from user avatar to room avatar (works only on unix)
-                            if let Ok(source) = cache_path(m) {
-                                if let Ok(dest) = cache_path(&roomid) {
+                            if let Ok(source) = cache_dir_path(None, m) {
+                                if let Ok(dest) = cache_dir_path(None, &roomid) {
                                     let _ = fs::symlink(source, dest);
                                 }
                             }
diff --git a/fractal-gtk/src/cache/state.rs b/fractal-gtk/src/cache/state.rs
index 831b733f..2456c56c 100644
--- a/fractal-gtk/src/cache/state.rs
+++ b/fractal-gtk/src/cache/state.rs
@@ -13,7 +13,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
 
 use crate::types::Message;
 use crate::types::Room;
-use fractal_api::util::cache_path;
+use fractal_api::util::cache_dir_path;
 
 // Models
 
@@ -101,7 +101,8 @@ impl FCache {
     fn get_store<'a>(&'a self) -> MutexGuard<'a, Option<Cache>> {
         let mut guard = self.cache.lock().unwrap();
         if guard.is_none() {
-            let db: String = cache_path("cache.mdl").expect("Fatal error: Can't start the cache");
+            let db: String =
+                cache_dir_path(None, "cache.mdl").expect("Fatal error: Can't start the cache");
             let mdl_cache = Cache::new(&db).expect("Fatal error: Can't start the cache");
             *guard = Some(mdl_cache);
         }
@@ -112,7 +113,8 @@ impl FCache {
         let mut guard = self.cache.lock().unwrap();
         guard.take();
 
-        let fname = cache_path("cache.mdl").or(Err(err_msg("Can't remove cache file")))?;
+        let fname =
+            cache_dir_path(None, "cache.mdl").or(Err(err_msg("Can't remove cache file")))?;
         remove_dir_all(fname).or_else(|_| Err(err_msg("Can't remove cache file")))
     }
 
diff --git a/fractal-gtk/src/widgets/avatar.rs b/fractal-gtk/src/widgets/avatar.rs
index 25f1259d..4ab4d399 100644
--- a/fractal-gtk/src/widgets/avatar.rs
+++ b/fractal-gtk/src/widgets/avatar.rs
@@ -3,7 +3,7 @@ use std::cell::RefCell;
 use std::rc::Rc;
 
 use cairo;
-use fractal_api::util::cache_path;
+use fractal_api::util::cache_dir_path;
 use gdk::ContextExt;
 use gdk_pixbuf::Pixbuf;
 use gtk;
@@ -37,7 +37,7 @@ impl AvatarData {
     }
 
     pub fn redraw_pixbuf(&mut self) {
-        let path = cache_path(&self.uid).unwrap_or_default();
+        let path = cache_dir_path(None, &self.uid).unwrap_or_default();
         self.cache = load_pixbuf(&path, self.size);
         self.widget.queue_draw();
     }
@@ -99,7 +99,7 @@ impl AvatarExt for gtk::Overlay {
     ) -> Rc<RefCell<AvatarData>> {
         self.clean();
         let da = self.create_da(Some(size));
-        let path = cache_path(&uid).unwrap_or_default();
+        let path = cache_dir_path(None, &uid).unwrap_or_default();
         let user_avatar = load_pixbuf(&path, size);
         let uname = username.clone();
         /* remove IRC postfix from the username */
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index e40e501f..3b1e8dad 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -7,7 +7,7 @@ use crate::backend::types::Backend;
 use crate::error::Error;
 use std::thread;
 
-use crate::util::cache_path;
+use crate::util::cache_dir_path;
 use crate::util::media;
 use crate::util::ResultExpectLog;
 use crate::util::HTTP_CLIENT;
@@ -130,7 +130,7 @@ pub fn room_search(
                     .map(Into::into)
                     .inspect(|r: &Room| {
                         if let Some(avatar) = r.avatar.clone() {
-                            if let Ok(dest) = cache_path(&r.id) {
+                            if let Ok(dest) = cache_dir_path(None, &r.id) {
                                 let _ = media(&base, &avatar, Some(&dest));
                             }
                         }
diff --git a/fractal-matrix-api/src/backend/media.rs b/fractal-matrix-api/src/backend/media.rs
index 0fcaf318..e03413ec 100644
--- a/fractal-matrix-api/src/backend/media.rs
+++ b/fractal-matrix-api/src/backend/media.rs
@@ -122,7 +122,7 @@ pub fn get_file_async(url: String, tx: Sender<String>) -> Result<(), Error> {
     let fname;
     {
         let name = url.split('/').last().unwrap_or_default();
-        fname = cache_dir_path("files", name)?.clone();
+        fname = cache_dir_path(Some("files"), name)?.clone();
     }
 
     thread::spawn(move || {
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 55d1ae62..1cf2d3e5 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -14,7 +14,7 @@ use crate::globals;
 use std::thread;
 
 use crate::util;
-use crate::util::cache_path;
+use crate::util::cache_dir_path;
 use crate::util::json_q;
 use crate::util::put_media;
 use crate::util::thumb;
@@ -76,7 +76,7 @@ pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
         &url,
         |r: JsonValue| {
             let avatar = r["url"].as_str().and_then(|s| Url::parse(s).ok());
-            let dest = cache_path(&roomid).ok();
+            let dest = cache_dir_path(None, &roomid).ok();
             if let Some(ref avatar) = avatar {
                 let _ = thumb(&baseu, avatar.as_str(), dest.as_ref().map(String::as_str));
             }
diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs
index b31fdd44..efc13edb 100644
--- a/fractal-matrix-api/src/util.rs
+++ b/fractal-matrix-api/src/util.rs
@@ -8,6 +8,7 @@ use directories::ProjectDirs;
 use std::collections::HashMap;
 use std::io::Read;
 use std::path::Path;
+use std::path::PathBuf;
 use url::percent_encoding::{utf8_percent_encode, USERINFO_ENCODE_SET};
 use url::Url;
 
@@ -31,6 +32,11 @@ use crate::globals;
 
 lazy_static! {
     pub static ref HTTP_CLIENT: Client = Client::new();
+    static ref CACHE_PATH: PathBuf = ProjectDirs::from("org", "GNOME", "Fractal")
+        .as_ref()
+        .map(ProjectDirs::cache_dir)
+        .map(Into::into)
+        .unwrap_or(std::env::temp_dir().join("fractal"));
 }
 
 pub fn semaphore<F>(thread_count: Arc<(Mutex<u8>, Condvar)>, func: F)
@@ -290,8 +296,8 @@ pub fn dw_media(
     let url = media_url(base, &path, &params)?;
 
     let fname = match dest {
-        None if thumb => cache_dir_path("thumbs", &media)?,
-        None => cache_dir_path("medias", &media)?,
+        None if thumb => cache_dir_path(Some("thumbs"), &media)?,
+        None => cache_dir_path(Some("medias"), &media)?,
         Some(d) => String::from(d),
     };
 
@@ -396,7 +402,7 @@ pub fn get_user_avatar(base: &Url, userid: &str) -> Result<(String, String), Err
     let img = response
         .avatar_url
         .map(|url| {
-            let dest = cache_path(userid)?;
+            let dest = cache_dir_path(None, userid)?;
             thumb(base, &url, Some(&dest))
         })
         .unwrap_or(Ok(Default::default()))?;
@@ -433,19 +439,11 @@ pub fn media_url(base: &Url, path: &str, params: &[(&str, String)]) -> Result<Ur
     build_url(base, &format!("/_matrix/media/r0/{}", path), params)
 }
 
-pub fn cache_path(name: &str) -> Result<String, Error> {
-    cache_dir_path("", name)
-}
-
-pub fn cache_dir_path(dir: &str, name: &str) -> Result<String, Error> {
-    let path = &ProjectDirs::from("org", "GNOME", "Fractal")
-        .as_ref()
-        .map(|project_dir| project_dir.cache_dir())
-        .unwrap_or(&std::env::temp_dir().join("fractal"))
-        .join(dir);
+pub fn cache_dir_path(dir: Option<&str>, name: &str) -> Result<String, Error> {
+    let path = CACHE_PATH.join(dir.unwrap_or_default());
 
     if !path.is_dir() {
-        create_dir_all(path)?;
+        create_dir_all(&path)?;
     }
 
     path.join(name)
@@ -459,7 +457,7 @@ pub fn get_user_avatar_img(baseu: &Url, userid: &str, avatar: &str) -> Result<St
         return Ok(String::new());
     }
 
-    let dest = cache_path(&userid)?;
+    let dest = cache_dir_path(None, &userid)?;
     thumb(baseu, &avatar, Some(&dest))
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]