[recipes/image-download: 11/13] Update GrImage to hosting location



commit 02cfd8a55df04508673d7e3bd5b6928055b07a59
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Mar 30 07:28:22 2017 -0400

    Update GrImage to hosting location
    
    We now have a place to host images on static.gnome.org, so
    update the image downloading code for that. I also decided
    to make the recipe / chef ID part of the path to ensure
    uniqueness, so we need to make an ID part of the GrImage
    API.
    
    Places where we create GrImage objects for local paths use
    the ID "unknown" for their content.

 src/gr-chef-dialog.c     |    3 +-
 src/gr-chef-tile.c       |    1 +
 src/gr-image-viewer.c    |    4 +-
 src/gr-image.c           |   52 +++++++++++++++++++++++++++++----------------
 src/gr-image.h           |    3 ++
 src/gr-recipe-importer.c |    8 +++---
 src/gr-recipe-store.c    |    2 +-
 7 files changed, 46 insertions(+), 27 deletions(-)
---
diff --git a/src/gr-chef-dialog.c b/src/gr-chef-dialog.c
index 405e42f..fb8f786 100644
--- a/src/gr-chef-dialog.c
+++ b/src/gr-chef-dialog.c
@@ -136,6 +136,7 @@ file_chooser_response (GtkNativeDialog *self,
 
                 g_clear_object (&prefs->ri);
                 prefs->ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())),
+                                          "local",
 import_image (path));
 
                 if (prefs->ri)
@@ -332,7 +333,7 @@ gr_chef_dialog_set_chef (GrChefDialog *self,
                 gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->description)),
                                           description ? description : "", -1);
 
-                self->ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), 
image_path);
+                self->ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), 
gr_chef_get_id (chef), image_path);
 
 
                 if (gr_chef_is_readonly (chef)) {
diff --git a/src/gr-chef-tile.c b/src/gr-chef-tile.c
index c7bc688..52fbf22 100644
--- a/src/gr-chef-tile.c
+++ b/src/gr-chef-tile.c
@@ -100,6 +100,7 @@ gr_chef_tile_set_chef (GrChefTile *tile,
                 path = gr_chef_get_image (chef);
                 if (path && path[0]) {
                         tile->ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default 
())),
+                                                 gr_chef_get_id (chef),
                                                  path);
                         tile->cancellable = g_cancellable_new ();
 
diff --git a/src/gr-image-viewer.c b/src/gr-image-viewer.c
index 4722e68..8af3467 100644
--- a/src/gr-image-viewer.c
+++ b/src/gr-image-viewer.c
@@ -404,7 +404,7 @@ image_received (GtkClipboard *clipboard,
                         return;
                 }
 
-                ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), path);
+                ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), "local", 
path);
                 add_image (viewer, ri, TRUE);
         }
 }
@@ -597,7 +597,7 @@ file_chooser_response (GtkNativeDialog *self,
                         g_autofree char *path = NULL;
 
                         path = import_image (l->data);
-                        ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), 
path);
+                        ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), 
"local", path);
                         add_image (viewer, ri, TRUE);
 
                         g_ptr_array_add (viewer->additions, g_strdup (path));
diff --git a/src/gr-image.c b/src/gr-image.c
index 5310c72..7e7f7e8 100644
--- a/src/gr-image.c
+++ b/src/gr-image.c
@@ -29,6 +29,7 @@
 struct _GrImage
 {
         GObject parent_instance;
+        char *id;
         char *path;
 
         SoupSession *session;
@@ -56,6 +57,7 @@ gr_image_finalize (GObject *object)
         g_clear_object (&ri->image_message);
         g_clear_object (&ri->session);
         g_free (ri->path);
+        g_free (ri->id);
 
         G_OBJECT_CLASS (gr_image_parent_class)->finalize (object);
 }
@@ -75,18 +77,28 @@ gr_image_init (GrImage *image)
 
 GrImage *
 gr_image_new (SoupSession *session,
+              const char  *id,
               const char  *path)
 {
         GrImage *image;
 
         image = g_object_new (GR_TYPE_IMAGE, NULL);
         image->session = g_object_ref (session);
+        gr_image_set_id (image, id);
         gr_image_set_path (image, path);
 
         return image;
 }
 
 void
+gr_image_set_id (GrImage    *image,
+                 const char *id)
+{
+        g_free (image->id);
+        image->id = g_strdup (id);
+}
+
+void
 gr_image_set_path (GrImage    *image,
                    const char *path)
 {
@@ -142,33 +154,35 @@ task_data_free (gpointer data)
         g_free (td);
 }
 
+#define BASE_URL "https://static.gnome.org/recipes/v1";
+
 static char *
-get_image_url (const char *path)
+get_image_url (GrImage *ri)
 {
         g_autofree char *basename = NULL;
 
-        basename = g_path_get_basename (path);
-        return g_strconcat ("http://mclasen.fedorapeople.org/recipes/images/";, basename, NULL);
+        basename = g_path_get_basename (ri->path);
+        return g_strconcat (BASE_URL, "/images/", ri->id, "/", basename, NULL);
 }
 
 static char *
-get_thumbnail_url (const char *path)
+get_thumbnail_url (GrImage *ri)
 {
         g_autofree char *basename = NULL;
 
-        basename = g_path_get_basename (path);
-        return g_strconcat ("http://mclasen.fedorapeople.org/recipes/thumbnails/";, basename, NULL);
+        basename = g_path_get_basename (ri->path);
+        return g_strconcat (BASE_URL, "/thumbnails/", ri->id, "/", basename, NULL);
 }
 
 static char *
-get_image_cache_path (const char *path)
+get_image_cache_path (GrImage *ri)
 {
         char *filename;
         g_autofree char *cache_dir = NULL;
         g_autofree char *basename = NULL;
 
-        basename = g_path_get_basename (path);
-        filename = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME, "images", basename, NULL);
+        basename = g_path_get_basename (ri->path);
+        filename = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME, "images", ri->id,  basename, 
NULL);
         cache_dir = g_path_get_dirname (filename);
         g_mkdir_with_parents (cache_dir, 0755);
 
@@ -176,14 +190,14 @@ get_image_cache_path (const char *path)
 }
 
 static char *
-get_thumbnail_cache_path (const char *path)
+get_thumbnail_cache_path (GrImage *ri)
 {
         char *filename;
         g_autofree char *cache_dir = NULL;
         g_autofree char *basename = NULL;
 
-        basename = g_path_get_basename (path);
-        filename = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME, "thumbnails", basename, NULL);
+        basename = g_path_get_basename (ri->path);
+        filename = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME, "thumbnails", ri->id, basename, 
NULL);
         cache_dir = g_path_get_dirname (filename);
         g_mkdir_with_parents (cache_dir, 0755);
 
@@ -225,10 +239,10 @@ set_image (SoupSession *session,
         if (msg == ri->thumbnail_message) {
                 if (ri->image_message == NULL) // already got the image, ignore the thumbnail
                         goto out;
-                cache_path = get_thumbnail_cache_path (ri->path);
+                cache_path = get_thumbnail_cache_path (ri);
         }
         else {
-                cache_path = get_image_cache_path (ri->path);
+                cache_path = get_image_cache_path (ri);
         }
 
         g_debug ("Saving image to %s", cache_path);
@@ -298,7 +312,7 @@ gr_image_load (GrImage         *ri,
                 }
         }
 
-        cache_path = get_image_cache_path (ri->path);
+        cache_path = get_image_cache_path (ri);
         pixbuf = load_pixbuf (cache_path, width, height, fit);
 
         if (pixbuf) {
@@ -317,7 +331,7 @@ gr_image_load (GrImage         *ri,
 
         ri->pending = g_list_prepend (ri->pending, td);
 
-        thumbnail_cache_path = get_thumbnail_cache_path (ri->path);
+        thumbnail_cache_path = get_thumbnail_cache_path (ri);
         pixbuf = load_pixbuf (thumbnail_cache_path, 120, 120 * height / width, fit);
         if (pixbuf) {
                 g_autoptr(GdkPixbuf) blurred = NULL;
@@ -331,7 +345,7 @@ gr_image_load (GrImage         *ri,
                 g_autofree char *url = NULL;
                 g_autoptr(SoupURI) base_uri = NULL;
 
-                url = get_thumbnail_url (ri->path);
+                url = get_thumbnail_url (ri);
                 base_uri = soup_uri_new (url);
                 ri->thumbnail_message = soup_message_new_from_uri (SOUP_METHOD_GET, base_uri);
                 g_debug ("Load thumbnail for %s from %s", ri->path, url);
@@ -342,7 +356,7 @@ gr_image_load (GrImage         *ri,
                 g_autofree char *url = NULL;
                 g_autoptr(SoupURI) base_uri = NULL;
 
-                url = get_image_url (ri->path);
+                url = get_image_url (ri);
                 base_uri = soup_uri_new (url);
                 ri->image_message = soup_message_new_from_uri (SOUP_METHOD_GET, base_uri);
                 g_debug ("Load image for %s from %s", ri->path, url);
@@ -372,7 +386,7 @@ gr_image_load_sync (GrImage   *ri,
         else {
                 g_autofree char *cache_path = NULL;
 
-                cache_path = get_image_cache_path (ri->path);
+                cache_path = get_image_cache_path (ri);
                 pixbuf = load_pixbuf (cache_path, width, height, fit);
         }
 
diff --git a/src/gr-image.h b/src/gr-image.h
index eec924b..23fdb00 100644
--- a/src/gr-image.h
+++ b/src/gr-image.h
@@ -30,7 +30,10 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (GrImage, gr_image, GR, IMAGE, GObject)
 
 GrImage    *gr_image_new         (SoupSession       *session,
+                                  const char        *id,
                                   const char        *path);
+void        gr_image_set_id      (GrImage           *image,
+                                  const char        *id);
 void        gr_image_set_path    (GrImage           *image,
                                   const char        *path);
 const char *gr_image_get_path    (GrImage           *image);
diff --git a/src/gr-recipe-importer.c b/src/gr-recipe-importer.c
index a79066d..e952a3f 100644
--- a/src/gr-recipe-importer.c
+++ b/src/gr-recipe-importer.c
@@ -289,6 +289,9 @@ import_recipe (GrRecipeImporter *importer)
 
         store = gr_recipe_store_get ();
 
+        author = (const char *)g_hash_table_lookup (importer->chef_id_map, importer->recipe_author);
+        id = generate_id ("R_", importer->recipe_name, "_by_", author, NULL);
+
         images = gr_image_array_new ();
         if (importer->recipe_paths) {
                 int i;
@@ -301,15 +304,12 @@ import_recipe (GrRecipeImporter *importer)
                                 return FALSE;
                         }
 
-                        ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), 
new_path);
+                        ri = gr_image_new (gr_app_get_soup_session (GR_APP (g_application_get_default ())), 
id, new_path);
 
                         g_ptr_array_add (images, ri);
                 }
         }
 
-        author = (const char *)g_hash_table_lookup (importer->chef_id_map, importer->recipe_author);
-        id = generate_id ("R_", importer->recipe_name, "_by_", author, NULL);
-
         recipe = gr_recipe_new ();
         g_object_set (recipe,
                       "id", id,
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index 098f2fb..e75787c 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -360,7 +360,7 @@ load_recipes (GrRecipeStore *self,
                 if (paths) {
                         for (j = 0; paths[j]; j++) {
                                 GrImage *ri;
-                                ri = gr_image_new (gr_app_get_soup_session (GR_APP 
(g_application_get_default ())), paths[j]);
+                                ri = gr_image_new (gr_app_get_soup_session (GR_APP 
(g_application_get_default ())), id, paths[j]);
 
                                 g_ptr_array_add (images, ri);
                         }


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