Debian Codegraph @

rest-extras/youtube-proxy.c

raw
1 /*
2 * librest - RESTful web services access
3 * Copyright (c) 2008, 2009, Intel Corporation.
4 *
5 * Authors: Rob Bradford <[email protected]>
6 * Ross Burton <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU Lesser General Public License,
10 * version 2.1, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <rest/rest-proxy.h>
27 #include <rest/rest-xml-node.h>
28 #include <libsoup/soup.h>
29
30 #include "rest/rest-private.h"
31 #include "youtube-proxy.h"
32
33 typedef struct {
34 char *developer_key;
35 char *user_auth;
36 } YoutubeProxyPrivate;
37
38 G_DEFINE_TYPE_WITH_PRIVATE (YoutubeProxy, youtube_proxy, REST_TYPE_PROXY)
39
40 #define UPLOAD_URL \
41 "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads"
42
43 enum {
44 PROP_0,
45 PROP_DEVELOPER_KEY,
46 PROP_USER_AUTH,
47 N_PROPS,
48 };
49
50 static GParamSpec *properties [N_PROPS];
51
52 G_DEFINE_QUARK (rest-youtube-proxy-error-quark, youtube_proxy_error)
53
54 static void
55 youtube_proxy_get_property (GObject *object,
56 guint property_id,
57 GValue *value,
58 GParamSpec *pspec)
59 {
60 YoutubeProxy *self = YOUTUBE_PROXY (object);
61 YoutubeProxyPrivate *priv = youtube_proxy_get_instance_private (self);
62
63 switch (property_id) {
64 case PROP_DEVELOPER_KEY:
65 g_value_set_string (value, priv->developer_key);
66 break;
67 case PROP_USER_AUTH:
68 g_value_set_string (value, priv->user_auth);
69 break;
70 default:
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
72 }
73 }
74
75 static void
76 youtube_proxy_set_property (GObject *object,
77 guint property_id,
78 const GValue *value,
79 GParamSpec *pspec)
80 {
81 YoutubeProxy *self = YOUTUBE_PROXY (object);
82 YoutubeProxyPrivate *priv = youtube_proxy_get_instance_private (self);
83
84 switch (property_id) {
85 case PROP_DEVELOPER_KEY:
86 g_free (priv->developer_key);
87 priv->developer_key = g_value_dup_string (value);
88 break;
89 case PROP_USER_AUTH:
90 g_free (priv->user_auth);
91 priv->user_auth = g_value_dup_string (value);
92 break;
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
95 }
96 }
97
98 static void
99 youtube_proxy_finalize (GObject *object)
100 {
101 YoutubeProxy *self = YOUTUBE_PROXY (object);
102 YoutubeProxyPrivate *priv = youtube_proxy_get_instance_private (self);
103
104 g_free (priv->developer_key);
105 g_free (priv->user_auth);
106
107 G_OBJECT_CLASS (youtube_proxy_parent_class)->finalize (object);
108 }
109
110 static void
111 youtube_proxy_class_init (YoutubeProxyClass *klass)
112 {
113 GObjectClass *object_class = G_OBJECT_CLASS (klass);
114
115 object_class->get_property = youtube_proxy_get_property;
116 object_class->set_property = youtube_proxy_set_property;
117 object_class->finalize = youtube_proxy_finalize;
118
119 properties [PROP_DEVELOPER_KEY] =
120 g_param_spec_string ("developer-key",
121 "developer-key",
122 "The developer API key",
123 NULL,
124 (G_PARAM_READWRITE |
125 G_PARAM_CONSTRUCT_ONLY |
126 G_PARAM_STATIC_STRINGS));
127
128 properties [PROP_USER_AUTH] =
129 g_param_spec_string ("user-auth",
130 "user-auth",
131 "The ClientLogin token",
132 NULL,
133 (G_PARAM_READWRITE |
134 G_PARAM_STATIC_STRINGS));
135
136 g_object_class_install_properties (object_class, N_PROPS, properties);
137 }
138
139 static void
140 youtube_proxy_init (YoutubeProxy *self)
141 {
142 }
143
144 RestProxy *
145 youtube_proxy_new (const char *developer_key)
146 {
147 return youtube_proxy_new_with_auth (developer_key,
148 NULL);
149 }
150
151 RestProxy *
152 youtube_proxy_new_with_auth (const char *developer_key,
153 const char *user_auth)
154 {
155 return g_object_new (YOUTUBE_TYPE_PROXY,
156 "developer-key", developer_key,
157 "user-auth", user_auth,
158 NULL);
159 }
160
161 void
162 youtube_proxy_set_user_auth (YoutubeProxy *self,
163 const gchar *user_auth)
164 {
165 YoutubeProxyPrivate *priv = youtube_proxy_get_instance_private (self);
166
167 priv->user_auth = g_strdup (user_auth);
168 }
169
170 static gchar *
171 _construct_upload_atom_xml (GHashTable *fields,
172 gboolean incomplete)
173 {
174 GHashTableIter iter;
175 gpointer key, value;
176 RestXmlNode *entry = rest_xml_node_add_child (NULL, "entry");
177 RestXmlNode *group = rest_xml_node_add_child (entry, "media:group");
178 gchar *bare_xml;
179 gchar *full_xml;
180
181 rest_xml_node_add_attr (entry, "xmlns", "http://www.w3.org/2005/Atom");
182 rest_xml_node_add_attr (entry, "xmlns:media",
183 "http://search.yahoo.com/mrss/");
184 rest_xml_node_add_attr (entry, "xmlns:yt",
185 "http://gdata.youtube.com/schemas/2007");
186
187 if (incomplete)
188 rest_xml_node_add_child (group, "yt:incomplete");
189
190 g_hash_table_iter_init (&iter, fields);
191
192 while (g_hash_table_iter_next (&iter, &key, &value)) {
193 RestXmlNode *node;
194 gchar *tag_name;
195 const gchar* field_value = value;
196 const gchar* field_key = key;
197
198 tag_name = g_strdup_printf ("media:%s", field_key);
199
200 node = rest_xml_node_add_child (group, tag_name);
201
202 if (g_strcmp0 (field_key, "title") == 0 ||
203 g_strcmp0 (field_key, "description") == 0)
204 rest_xml_node_add_attr (node, "type", "plain");
205
206 if (g_strcmp0 (field_key, "category") == 0)
207 rest_xml_node_add_attr (node, "scheme", "http://gdata.youtube.com/"
208 "schemas/2007/categories.cat");
209
210 rest_xml_node_set_content (node, field_value);
211 }
212
213 bare_xml = rest_xml_node_print (entry);
214 full_xml = g_strdup_printf ("<?xml version=\"1.0\"?>\n%s", bare_xml);
215
216 rest_xml_node_unref (entry);
217 g_free (bare_xml);
218
219 return full_xml;
220 }
221
222 static void
223 _set_upload_headers (YoutubeProxy *self,
224 SoupMessageHeaders *headers,
225 const gchar *filename)
226 {
227 YoutubeProxyPrivate *priv = youtube_proxy_get_instance_private (self);
228 gchar *user_auth_header;
229 gchar *devkey_header;
230 gchar *basename;
231 const gchar *user_agent;
232
233 /* Set the user agent, if one was set in the proxy */
234 user_agent = rest_proxy_get_user_agent (REST_PROXY (self));
235 if (user_agent)
236 soup_message_headers_append (headers, "User-Agent", user_agent);
237
238 g_print ("%s\n", priv->user_auth);
239
240 user_auth_header = g_strdup_printf ("GoogleLogin auth=%s", priv->user_auth);
241 soup_message_headers_append (headers, "Authorization", user_auth_header);
242 devkey_header = g_strdup_printf ("key=%s", priv->developer_key);
243 soup_message_headers_append (headers, "X-GData-Key", devkey_header);
244 basename = g_path_get_basename (filename);
245 soup_message_headers_append (headers, "Slug", basename);
246
247 g_free (user_auth_header);
248 g_free (devkey_header);
249 }
250
251 typedef struct {
252 YoutubeProxy *proxy;
253 YoutubeProxyUploadCallback callback;
254 SoupMessage *message;
255 GObject *weak_object;
256 gpointer user_data;
257 gsize uploaded;
258 #ifndef WITH_SOUP_2
259 GCancellable *cancellable;
260 #endif
261 } YoutubeProxyUploadClosure;
262
263 static void
264 _upload_async_weak_notify_cb (gpointer *data,
265 GObject *dead_object)
266 {
267 YoutubeProxyUploadClosure *closure =
268 (YoutubeProxyUploadClosure *) data;
269
270 #ifdef WITH_SOUP_2
271 _rest_proxy_cancel_message (REST_PROXY (closure->proxy), closure->message);
272 #else
273 g_cancellable_cancel (closure->cancellable);
274 #endif
275 }
276
277 static void
278 _upload_async_closure_free (YoutubeProxyUploadClosure *closure)
279 {
280 if (closure->weak_object != NULL)
281 g_object_weak_unref (closure->weak_object,
282 (GWeakNotify) _upload_async_weak_notify_cb,
283 closure);
284
285 g_object_unref (closure->proxy);
286 #ifndef WITH_SOUP_2
287 g_object_unref (closure->cancellable);
288 #endif
289
290 g_slice_free (YoutubeProxyUploadClosure, closure);
291 }
292
293 static YoutubeProxyUploadClosure *
294 _upload_async_closure_new (YoutubeProxy *self,
295 YoutubeProxyUploadCallback callback,
296 SoupMessage *message,
297 GObject *weak_object,
298 gpointer user_data)
299 {
300 YoutubeProxyUploadClosure *closure =
301 g_slice_new0 (YoutubeProxyUploadClosure);
302
303 closure->proxy = g_object_ref (self);
304 closure->callback = callback;
305 closure->message = message;
306 closure->weak_object = weak_object;
307 closure->user_data = user_data;
308 #ifndef WITH_SOUP_2
309 closure->cancellable = g_cancellable_new ();
310 #endif
311
312 if (weak_object != NULL)
313 g_object_weak_ref (weak_object,
314 (GWeakNotify) _upload_async_weak_notify_cb,
315 closure);
316 return closure;
317 }
318
319 static void
320 _upload_completed_cb (SoupMessage *message,
321 GBytes *payload,
322 GError *error,
323 gpointer user_data)
324 {
325 YoutubeProxyUploadClosure *closure =
326 (YoutubeProxyUploadClosure *) user_data;
327 gsize length;
328 gconstpointer data;
329 guint status_code;
330 const char *reason_phrase;
331
332 if (closure->callback == NULL)
333 return;
334
335 #ifdef WITH_SOUP_2
336 status_code = message->status_code;
337 reason_phrase = message->reason_phrase;
338 #else
339 status_code = soup_message_get_status (message);
340 reason_phrase = soup_message_get_reason_phrase (message);
341 #endif
342
343 if (status_code < 200 || status_code >= 300)
344 {
345 g_clear_error (&error);
346 error = g_error_new_literal (REST_PROXY_ERROR,
347 status_code,
348 reason_phrase);
349 }
350
351 data = g_bytes_get_data (payload, &length);
352 closure->callback (closure->proxy, data, length, length,
353 error, closure->weak_object, closure->user_data);
354 g_bytes_unref (payload);
355
356 _upload_async_closure_free (closure);
357 }
358
359 static void
360 _message_wrote_data_cb (SoupMessage *msg,
361 #ifdef WITH_SOUP_2
362 SoupBuffer *chunk,
363 #else
364 gsize chunk_size,
365 #endif
366 YoutubeProxyUploadClosure *closure)
367 {
368 #ifdef WITH_SOUP_2
369 gsize chunk_size = chunk->length;
370 goffset content_length = msg->request_body->length;
371 #else
372 goffset content_length = soup_message_headers_get_content_length (soup_message_get_request_headers (msg));
373 #endif
374
375 closure->uploaded = closure->uploaded + chunk_size;
376
377 if (closure->uploaded < content_length)
378 closure->callback (closure->proxy,
379 NULL,
380 content_length,
381 closure->uploaded,
382 NULL,
383 closure->weak_object,
384 closure->user_data);
385 }
386
387 /**
388 * youtube_proxy_upload_async:
389 * @self: a #YoutubeProxy
390 * @filename: filename
391 * @fields: (element-type utf8 utf8): fields
392 * @incomplete: incomplete
393 * @callback: (scope call): callback to invoke upon completion
394 * @weak_object: (nullable): an object instance used to tie the life cycle of
395 * the proxy to
396 * @user_data: user data to pass to the callback
397 * @error: a #GError pointer, or %NULL
398 *
399 * Upload a file.
400 *
401 * Returns: %TRUE, or %FALSE if the file could not be opened
402 */
403 gboolean
404 youtube_proxy_upload_async (YoutubeProxy *self,
405 const gchar *filename,
406 GHashTable *fields,
407 gboolean incomplete,
408 YoutubeProxyUploadCallback callback,
409 GObject *weak_object,
410 gpointer user_data,
411 GError **error)
412 {
413 SoupMultipart *mp;
414 SoupMessage *message;
415 SoupMessageHeaders *part_headers;
416 SoupMessageHeaders *request_headers;
417 #ifdef WITH_SOUP_2
418 SoupBuffer *sb;
419 #else
420 GBytes *sb;
421 #endif
422 gchar *content_type;
423 gchar *atom_xml;
424 GMappedFile *map;
425 YoutubeProxyUploadClosure *closure;
426
427 map = g_mapped_file_new (filename, FALSE, error);
428 if (*error != NULL) {
429 g_warning ("Error opening file %s: %s", filename, (*error)->message);
430 return FALSE;
431 }
432
433 mp = soup_multipart_new ("multipart/related");
434
435 atom_xml = _construct_upload_atom_xml (fields, incomplete);
436
437 #ifdef WITH_SOUP_2
438 sb = soup_buffer_new_with_owner (atom_xml,
439 strlen(atom_xml),
440 atom_xml,
441 (GDestroyNotify) g_free);
442 #else
443 sb = g_bytes_new_with_free_func (atom_xml,
444 strlen (atom_xml),
445 (GDestroyNotify) g_free,
446 atom_xml);
447 #endif
448
449 part_headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_MULTIPART);
450
451 soup_message_headers_append (part_headers, "Content-Type",
452 "application/atom+xml; charset=UTF-8");
453
454
455 soup_multipart_append_part (mp, part_headers, sb);
456
457 #ifdef WITH_SOUP_2
458 soup_buffer_free (sb);
459 #else
460 g_bytes_unref (sb);
461 #endif
462
463 content_type = g_content_type_guess (
464 filename,
465 (const guchar*) g_mapped_file_get_contents (map),
466 g_mapped_file_get_length (map),
467 NULL);
468
469 #ifdef WITH_SOUP_2
470 sb = soup_buffer_new_with_owner (g_mapped_file_get_contents (map),
471 g_mapped_file_get_length (map),
472 map,
473 (GDestroyNotify) g_mapped_file_unref);
474 #else
475 sb = g_bytes_new_with_free_func (g_mapped_file_get_contents (map),
476 g_mapped_file_get_length (map),
477 (GDestroyNotify) g_mapped_file_unref,
478 map);
479 #endif
480
481 soup_message_headers_replace (part_headers, "Content-Type", content_type);
482
483 soup_multipart_append_part (mp, part_headers, sb);
484
485 #ifdef WITH_SOUP_2
486 soup_buffer_free (sb);
487 soup_message_headers_free (part_headers);
488 message = soup_form_request_new_from_multipart (UPLOAD_URL, mp);
489 request_headers = message->request_headers;
490 #else
491 g_bytes_unref (sb);
492 soup_message_headers_unref (part_headers);
493 message = soup_message_new_from_multipart (UPLOAD_URL, mp);
494 request_headers = soup_message_get_request_headers (message);
495 #endif
496
497 soup_multipart_free (mp);
498
499 _set_upload_headers (self, request_headers, filename);
500
501 closure = _upload_async_closure_new (self, callback, message, weak_object,
502 user_data);
503
504 g_signal_connect (message,
505 "wrote-body-data",
506 (GCallback) _message_wrote_data_cb,
507 closure);
508
509
510 _rest_proxy_queue_message (REST_PROXY (self), message,
511 #ifdef WITH_SOUP_2
512 NULL,
513 #else
514 closure->cancellable,
515 #endif
516 _upload_completed_cb,
517 closure);
518
519 return TRUE;
520 }