[libgepub] Work with file paths, not URI substrings, in custom URI scheme



commit 98d613be0e83f2de58e9196e62566cd0bd561af6
Author: IBBoard <dev ibboard co uk>
Date:   Wed Apr 24 19:41:02 2019 +0100

    Work with file paths, not URI substrings, in custom URI scheme
    
    Using `…_get_uri()` gives a string that includes the anchor, which
    isn't a valid entry path in the archive. `…_get_path()` gets just
    the path (but with a leading slash).
    
    Rather than special case the widget code to handle the starting slash,
    the archive and doc now ignore the leading slash in paths. This allows
    `…_get_path()` to be used anywhere in the code base without additional
    text manipulation each time.

 libgepub/gepub-archive.c | 10 +++++++++-
 libgepub/gepub-doc.c     | 10 +++++++++-
 libgepub/gepub-widget.c  |  7 ++-----
 3 files changed, 20 insertions(+), 7 deletions(-)
---
diff --git a/libgepub/gepub-archive.c b/libgepub/gepub-archive.c
index fdca802..4305a18 100644
--- a/libgepub/gepub-archive.c
+++ b/libgepub/gepub-archive.c
@@ -135,12 +135,20 @@ gepub_archive_read_entry (GepubArchive *archive,
     struct archive_entry *entry;
     guchar *buffer;
     gint size;
+    const gchar *_path;
+
+    if (path[0] == '/') {
+        _path = path + 1;
+    }
+    else {
+        _path = path;
+    }
 
     if (!gepub_archive_open (archive))
         return NULL;
 
     while (archive_read_next_header (archive->archive, &entry) == ARCHIVE_OK) {
-        if (g_ascii_strcasecmp (path, archive_entry_pathname (entry)) == 0)
+        if (g_ascii_strcasecmp (_path, archive_entry_pathname (entry)) == 0)
             break;
         archive_read_data_skip (archive->archive);
     }
diff --git a/libgepub/gepub-doc.c b/libgepub/gepub-doc.c
index bb88f4c..e486bf5 100644
--- a/libgepub/gepub-doc.c
+++ b/libgepub/gepub-doc.c
@@ -595,15 +595,23 @@ gepub_doc_get_resource_mime (GepubDoc *doc, const gchar *path)
 {
     GepubResource *gres;
     GList *keys;
+    const gchar *_path;
 
     g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
     g_return_val_if_fail (path != NULL, NULL);
 
+    if (path[0] == '/') {
+        _path = path + 1;
+    }
+    else {
+        _path = path;
+    }
+
     keys = g_hash_table_get_keys (doc->resources);
 
     while (keys) {
         gres = ((GepubResource*)g_hash_table_lookup (doc->resources, keys->data));
-        if (!strcmp (gres->uri, path))
+        if (!strcmp (gres->uri, _path))
             break;
         keys = keys->next;
     }
diff --git a/libgepub/gepub-widget.c b/libgepub/gepub-widget.c
index f6d5003..fce91c5 100644
--- a/libgepub/gepub-widget.c
+++ b/libgepub/gepub-widget.c
@@ -243,7 +243,6 @@ resource_callback (WebKitURISchemeRequest *request, gpointer user_data)
 {
     GInputStream *stream;
     gchar *path;
-    gchar *uri;
     gchar *mime;
     GepubWidget *widget = user_data;
     GBytes *contents;
@@ -251,9 +250,7 @@ resource_callback (WebKitURISchemeRequest *request, gpointer user_data)
     if (!widget->doc)
       return;
 
-    uri = g_strdup (webkit_uri_scheme_request_get_uri (request));
-    // removing "epub:///"
-    path = uri + 8;
+    path = g_strdup (webkit_uri_scheme_request_get_path (request));
     contents = gepub_doc_get_resource (widget->doc, path);
     mime = gepub_doc_get_resource_mime (widget->doc, path);
 
@@ -275,7 +272,7 @@ resource_callback (WebKitURISchemeRequest *request, gpointer user_data)
     g_object_unref (stream);
     g_bytes_unref (contents);
     g_free (mime);
-    g_free (uri);
+    g_free (path);
 }
 
 static void


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