From 739027e7e0176ce7c37f422e719c734793e5774b Mon Sep 17 00:00:00 2001 From: Ross Burton Date: Fri, 6 Aug 2010 18:07:27 +0100 Subject: flickr: add flickr_proxy_new_upload_for_file --- rest-extras/flickr-proxy.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ rest-extras/flickr-proxy.h | 2 ++ 2 files changed, 79 insertions(+) (limited to 'rest-extras') diff --git a/rest-extras/flickr-proxy.c b/rest-extras/flickr-proxy.c index f0adc4a..caa159a 100644 --- a/rest-extras/flickr-proxy.c +++ b/rest-extras/flickr-proxy.c @@ -380,12 +380,89 @@ flickr_proxy_is_successful (RestXmlNode *root, GError **error) RestProxyCall * flickr_proxy_new_upload (FlickrProxy *proxy) { + g_return_val_if_fail (FLICKR_IS_PROXY (proxy), NULL); + return g_object_new (FLICKR_TYPE_PROXY_CALL, "proxy", proxy, "upload", TRUE, NULL); } +/** + * flickr_proxy_new_upload_for_file: + * @proxy: a valid #FlickrProxy + * @filename: the file to upload + * @error: #GError to set on error + + * Create a new #RestProxyCall that can be used for uploading. @filename will + * be set as the "photo" parameter for you, avoiding you from having to open the + * file and determine the MIME type. + * + * Note that this function can in theory block. + * + * See http://www.flickr.com/services/api/upload.api.html for details on + * uploading to Flickr. + */ +RestProxyCall * +flickr_proxy_new_upload_for_file (FlickrProxy *proxy, const char *filename, GError **error) +{ + GMappedFile *map; + GFile *file = NULL; + GFileInfo *fi = NULL; + GError *err = NULL; + const char *content_type; + char *basename = NULL; + RestParam *param; + RestProxyCall *call = NULL; + + g_return_val_if_fail (FLICKR_IS_PROXY (proxy), NULL); + g_return_val_if_fail (filename, NULL); + + /* Open the file */ + map = g_mapped_file_new (filename, FALSE, &err); + if (err) { + g_propagate_error (error, err); + return NULL; + } + + /* Get the file information */ + file = g_file_new_for_path (filename); + basename = g_file_get_basename (file); + + fi = g_file_query_info (file, + G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, + G_FILE_QUERY_INFO_NONE, + NULL, + &err); + + if (fi == NULL) { + g_propagate_error (error, err); + goto done; + } + + content_type = g_file_info_get_attribute_string (fi, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE); + + /* Make the call */ + call = flickr_proxy_new_upload (proxy); + + /* Now construct the param */ + param = rest_param_new_with_owner ("photo", + g_mapped_file_get_contents (map), + g_mapped_file_get_length (map), + content_type, + basename, + map, + (GDestroyNotify)g_mapped_file_unref); + rest_proxy_call_add_param_full (call, param); + + done: + g_free (basename); + g_object_unref (file); + g_object_unref (fi); + + return call; +} + #if BUILD_TESTS void test_flickr_error (void) diff --git a/rest-extras/flickr-proxy.h b/rest-extras/flickr-proxy.h index 249c02d..5cff622 100644 --- a/rest-extras/flickr-proxy.h +++ b/rest-extras/flickr-proxy.h @@ -93,6 +93,8 @@ gboolean flickr_proxy_is_successful (RestXmlNode *root, GError **error); RestProxyCall * flickr_proxy_new_upload (FlickrProxy *proxy); +RestProxyCall * flickr_proxy_new_upload_for_file (FlickrProxy *proxy, const char *filename, GError **error); + G_END_DECLS #endif /* _FLICKR_PROXY */ -- cgit v1.2.1