diff options
author | Carlos Garcia Campos <cgarcia@igalia.com> | 2012-05-29 20:30:49 +0800 |
---|---|---|
committer | Carlos Garcia Campos <carlosgc@gnome.org> | 2012-06-14 19:41:42 +0800 |
commit | 2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc (patch) | |
tree | 774eb65bf0e127118de737852843967d5e3d01f7 /lib/ephy-file-helpers.c | |
parent | 8e096384a3e15ba1b68235045c6148a8af29cedb (diff) | |
download | gsoc2013-epiphany-2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc.tar gsoc2013-epiphany-2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc.tar.gz gsoc2013-epiphany-2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc.tar.bz2 gsoc2013-epiphany-2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc.tar.lz gsoc2013-epiphany-2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc.tar.xz gsoc2013-epiphany-2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc.tar.zst gsoc2013-epiphany-2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc.zip |
e-file-helpers: Add ephy_file_create_data_uri_for_filename()
It creates a data URI for the given filename. Use the new function when
building error and applications pages.
https://bugzilla.gnome.org/show_bug.cgi?id=677025
Diffstat (limited to 'lib/ephy-file-helpers.c')
-rw-r--r-- | lib/ephy-file-helpers.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/ephy-file-helpers.c b/lib/ephy-file-helpers.c index 13ae06de2..4975811f8 100644 --- a/lib/ephy-file-helpers.c +++ b/lib/ephy-file-helpers.c @@ -970,3 +970,54 @@ ephy_file_delete_uri (const char *uri) } g_object_unref (file); } + +/** + * ephy_file_create_data_uri_for_filename: + * @filename: the filename of a local path + * @mime_type: the MIME type of the filename, or %NULL + * + * Create a data uri using the contents of @filename. + * If @mime_type is %NULL, the %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE + * attribute of @filename will be used. + * + * Returns: a new allocated string containg the data uri, or %NULL if the + * data uri could not be created + */ +char *ephy_file_create_data_uri_for_filename (const char *filename, + const char *mime_type) +{ + gchar *data; + gsize data_length; + gchar *base64; + gchar *uri = NULL; + GFileInfo *file_info = NULL; + + g_return_val_if_fail (filename != NULL, NULL); + + if (!g_file_get_contents (filename, &data, &data_length, NULL)) + return NULL; + + base64 = g_base64_encode ((const guchar *)data, data_length); + g_free (data); + + if (!mime_type) { + GFile *file; + + file = g_file_new_for_path (filename); + file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, + G_FILE_QUERY_INFO_NONE, NULL, NULL); + if (file_info) + mime_type = g_file_info_get_content_type (file_info); + + g_object_unref (file); + } + + if (mime_type) + uri = g_strdup_printf ("data:%s;charset=utf8;base64,%s", mime_type, base64); + g_free(base64); + + if (file_info) + g_object_unref (file_info); + + return uri; +} |