[connections/fix-thumbnail-storage: 2/2] thumbnailer: Ensure cache directory existance before writing to it




commit 834c1ca41732fed845c51404dd77cc8369cf311a
Author: Felipe Borges <felipeborges gnome org>
Date:   Wed Jul 28 17:07:20 2021 +0200

    thumbnailer: Ensure cache directory existance before writing to it
    
    Fixes #25

 meson.build          |  1 +
 src/config.vapi      |  1 +
 src/thumbnailer.vala | 25 +++++++++++++++++--------
 3 files changed, 19 insertions(+), 8 deletions(-)
---
diff --git a/meson.build b/meson.build
index a252d34..6866533 100644
--- a/meson.build
+++ b/meson.build
@@ -16,6 +16,7 @@ conf = configuration_data ()
 conf.set_quoted ('GETTEXT_PACKAGE', meson.project_name ())
 conf.set_quoted ('LOCALEDIR', locale_dir)
 conf.set_quoted ('VERSION', '@0@-@VCS_TAG@'.format (meson.project_version()))
+conf.set_quoted ('PACKAGE_TARNAME', meson.project_name ())
 
 if get_option ('profile') == 'development'
   profile = '.Devel'
diff --git a/src/config.vapi b/src/config.vapi
index 634d251..f1b5b8e 100644
--- a/src/config.vapi
+++ b/src/config.vapi
@@ -4,4 +4,5 @@ namespace Config {
     public const string LOCALEDIR;
     public const string VERSION;
     public const string APPLICATION_ID;
+    public const string PACKAGE_TARNAME;
 }
diff --git a/src/thumbnailer.vala b/src/thumbnailer.vala
index 8893c61..6d6cdd3 100644
--- a/src/thumbnailer.vala
+++ b/src/thumbnailer.vala
@@ -25,18 +25,17 @@ namespace Connections {
 
         public signal void update ();
 
-        public string? thumbnail_path {
-            owned get {
-                return Path.build_filename (Environment.get_user_cache_dir (),
-                                            "connections",
-                                            connection.host + ".png");
-            }
-        }
+        public string? thumbnail_path;
 
         private uint thumbnail_id;
 
         public Thumbnailer (Connection connection) {
             this.connection = connection;
+
+            var cache_dir = Path.build_filename (Environment.get_user_cache_dir (),
+                                                 Config.PACKAGE_TARNAME);
+            ensure_directory (cache_dir);
+            thumbnail_path = Path.build_filename (cache_dir, connection.host);
         }
 
         public void update_thumbnail () {
@@ -49,7 +48,7 @@ namespace Connections {
                     try {
                         connection.thumbnail.save (thumbnail_path, "png");
                     } catch (GLib.Error error) {
-                        warning (error.message);
+                        debug (error.message);
 
                         return false;
                     }
@@ -58,5 +57,15 @@ namespace Connections {
                return true; 
             });            
         }
+
+        private void ensure_directory (string dir) {
+            try {
+                var file = GLib.File.new_for_path (dir);
+                file.make_directory_with_parents (null);
+            } catch (GLib.IOError.EXISTS error) {
+            } catch (GLib.Error error) {
+                debug (error.message);
+            }
+        }
     }
 }


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