[gnome-software] snap: Cache remote icons



commit d02149737da0efa218e5660492e0eb88e0aedda5
Author: Robert Ancell <robert ancell canonical com>
Date:   Wed Jun 14 11:52:14 2017 +1200

    snap: Cache remote icons

 plugins/snap/gs-plugin-snap.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
---
diff --git a/plugins/snap/gs-plugin-snap.c b/plugins/snap/gs-plugin-snap.c
index 936f00d..912a853 100644
--- a/plugins/snap/gs-plugin-snap.c
+++ b/plugins/snap/gs-plugin-snap.c
@@ -320,8 +320,29 @@ load_icon (GsPlugin *plugin, GsApp *app, const gchar *icon_url, GCancellable *ca
                        return FALSE;
                }
        } else {
+               g_autofree gchar *cache_fn = NULL;
                g_autoptr(SoupMessage) message = NULL;
                g_autoptr(GdkPixbufLoader) loader = NULL;
+               g_autoptr(GError) local_error = NULL;
+
+               /* attempt to load from cache */
+               cache_fn = gs_utils_get_cache_filename ("snap-icons", icon_url, GS_UTILS_CACHE_FLAG_USE_HASH 
| GS_UTILS_CACHE_FLAG_WRITEABLE, error);
+               if (cache_fn == NULL)
+                       return FALSE;
+               if (g_file_test (cache_fn, G_FILE_TEST_EXISTS)) {
+                       g_autofree gchar *data = NULL;
+                       gsize data_len;
+
+                       if (g_file_get_contents (cache_fn, &data, &data_len, &local_error) &&
+                           gs_plugin_snap_set_app_pixbuf_from_data (app,
+                                                                    data, data_len,
+                                                                    error))
+                               return TRUE;
+
+                       g_warning ("Failed to load cached icon: %s", local_error->message);
+               }
+
+               /* load from URL */
                message = soup_message_new (SOUP_METHOD_GET, icon_url);
                if (message == NULL) {
                        g_set_error (error,
@@ -339,6 +360,10 @@ load_icon (GsPlugin *plugin, GsApp *app, const gchar *icon_url, GCancellable *ca
                        g_prefix_error (error, "Failed to load %s: ", icon_url);
                        return FALSE;
                }
+
+               /* write to cache */
+               if (!g_file_set_contents (cache_fn, message->response_body->data, 
message->response_body->length, &local_error))
+                       g_warning ("Failed to save icon to cache: %s", local_error->message);
        }
 
        return TRUE;


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