[devhelp] util: Add dh_util_create_data_uri_for_filename()



commit b9a721a490ebfbd6cc45a600e010e1db381ff154
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Fri Feb 10 11:02:44 2012 +0100

    util: Add dh_util_create_data_uri_for_filename()
    
    It creates a data uri for the given filename and mime-type. It's useful
    to include resources in generated html and avoid problems with local
    paths when using webkit_web_view_load_html() in WebKit2.

 src/dh-util.c |   21 +++++++++++++++++++++
 src/dh-util.h |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)
---
diff --git a/src/dh-util.c b/src/dh-util.c
index b529567..27c029a 100644
--- a/src/dh-util.c
+++ b/src/dh-util.c
@@ -819,3 +819,24 @@ dh_util_ascii_strtitle (gchar *str)
                 str++;
         }
 }
+
+gchar *
+dh_util_create_data_uri_for_filename (const gchar *filename,
+                                      const gchar *mime_type)
+{
+        gchar *data;
+        gsize  data_length;
+        gchar *base64;
+        gchar *uri;
+
+        if (!g_file_get_contents (filename, &data, &data_length, NULL))
+                return NULL;
+
+        base64 = g_base64_encode ((const guchar *)data, data_length);
+        g_free (data);
+
+        uri = g_strdup_printf ("data:%s;charset=utf8;base64,%s", mime_type, base64);
+        g_free(base64);
+
+        return uri;
+}
diff --git a/src/dh-util.h b/src/dh-util.h
index 24835bb..ecb4b42 100644
--- a/src/dh-util.h
+++ b/src/dh-util.h
@@ -68,6 +68,8 @@ gint         dh_util_cmp_book                     (DhLink *a,
                                                    DhLink *b);
 
 void         dh_util_ascii_strtitle               (gchar *str);
+gchar       *dh_util_create_data_uri_for_filename (const gchar *filename,
+                                                   const gchar *mime_type);
 
 G_END_DECLS
 



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