[gnome-software/wip/mcrha/odrs-download-etag] odrs: Use the reviews' ETag to avoid download of unchanged files



commit a27b2d575fc179c5f17cba75fc5d9cdd99f2ee1e
Author: Milan Crha <mcrha redhat com>
Date:   Mon Oct 18 17:42:13 2021 +0200

    odrs: Use the reviews' ETag to avoid download of unchanged files
    
    The server can detect when the reviews changed using the ETag header,
    which can help to avoid download of a file, which is already stored
    locally.

 data/org.gnome.software.gschema.xml |  4 ++++
 lib/gs-odrs-provider.c              | 10 +++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)
---
diff --git a/data/org.gnome.software.gschema.xml b/data/org.gnome.software.gschema.xml
index 0e5706b7c..1a2ac527e 100644
--- a/data/org.gnome.software.gschema.xml
+++ b/data/org.gnome.software.gschema.xml
@@ -85,6 +85,10 @@
       <default>'https://odrs.gnome.org/1.0/reviews/api'</default>
       <summary>The server to use for application reviews</summary>
     </key>
+    <key name="review-server-etag" type="s">
+      <default>''</default>
+      <summary>The last ETag of the reviews file on the server</summary>
+    </key>
     <key name="review-karma-required" type="i">
       <default>0</default>
       <summary>The minimum karma score for reviews</summary>
diff --git a/lib/gs-odrs-provider.c b/lib/gs-odrs-provider.c
index 00502e27c..bec32b1dc 100644
--- a/lib/gs-odrs-provider.c
+++ b/lib/gs-odrs-provider.c
@@ -1235,8 +1235,11 @@ gs_odrs_provider_refresh (GsOdrsProvider  *self,
 {
        g_autofree gchar *cache_filename = NULL;
        g_autofree gchar *uri = NULL;
+       g_autofree gchar *last_etag = NULL;
+       g_autofree gchar *new_etag = NULL;
        g_autoptr(GError) error_local = NULL;
        g_autoptr(GsApp) app_dl = NULL;
+       g_autoptr(GSettings) settings = NULL;
 
        /* check cache age */
        cache_filename = gs_utils_get_cache_filename ("odrs",
@@ -1258,6 +1261,8 @@ gs_odrs_provider_refresh (GsOdrsProvider  *self,
                }
        }
 
+       settings = g_settings_new ("org.gnome.software");
+       last_etag = g_settings_get_string (settings, "review-server-etag");
        app_dl = gs_app_new ("odrs");
 
        /* download the complete file */
@@ -1266,7 +1271,7 @@ gs_odrs_provider_refresh (GsOdrsProvider  *self,
        gs_app_set_summary_missing (app_dl,
                                    /* TRANSLATORS: status text when downloading */
                                    _("Downloading application ratingsā€¦"));
-       if (!gs_plugin_download_file (plugin, app_dl, uri, cache_filename, cancellable, &error_local)) {
+       if (!gs_plugin_download_file_full (plugin, app_dl, uri, cache_filename, last_etag, &new_etag, 
cancellable, &error_local)) {
                g_autoptr(GsPluginEvent) event = gs_plugin_event_new ();
 
                gs_plugin_event_set_error (event, error_local);
@@ -1281,6 +1286,9 @@ gs_odrs_provider_refresh (GsOdrsProvider  *self,
                /* don't fail updates if the ratings server is unavailable */
                return TRUE;
        }
+       if (g_strcmp0 (last_etag, new_etag) != 0)
+               g_settings_set_string (settings, "review-server-etag", new_etag != NULL ? new_etag : "");
+
        return gs_odrs_provider_load_ratings (self, cache_filename, error);
 }
 


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