Codebase list gnome-twitch / debian/0.4.1-1 src / gt-resource-downloader.c
debian/0.4.1-1

Tree @debian/0.4.1-1 (Download .tar.gz)

gt-resource-downloader.c @debian/0.4.1-1raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "gt-resource-downloader.h"
#include "utils.h"
#include "config.h"
#include <glib/gprintf.h>

#define TAG "GtResourceDownloader"
#include "gnome-twitch/gt-log.h"

typedef struct
{
    gchar* filepath;
    gchar* image_filetype;
    SoupSession* soup;
    GMutex mutex;
} GtResourceDownloaderPrivate;

typedef struct
{
    gchar* uri;
    gchar* name;
    ResourceDownloaderFunc cb;
    gpointer udata;
    GtResourceDownloader* self;
    SoupMessage* msg;
    GInputStream* istream;
} ResourceData; /* FIXME: Better name? */

static GThreadPool* dl_pool;

G_DEFINE_TYPE_WITH_PRIVATE(GtResourceDownloader, gt_resource_downloader, G_TYPE_OBJECT);

static ResourceData*
resource_data_new()
{
    return g_slice_new0(ResourceData);
}

static void
resource_data_free(ResourceData* data)
{
    if (!data) return;

    g_free(data->uri);
    g_free(data->name);
    g_object_unref(data->self);
    g_object_unref(data->msg);
    g_object_unref(data->istream);

    g_slice_free(ResourceData, data);
}

/* FIXME: Throw error */
static GdkPixbuf*
download_image(GtResourceDownloader* self,
    const gchar* uri, const gchar* name,
    SoupMessage* msg, GInputStream* istream,
    gboolean* from_file, GError** error)
{
    RETURN_VAL_IF_FAIL(GT_IS_RESOURCE_DOWNLOADER(self), NULL);
    RETURN_VAL_IF_FAIL(!utils_str_empty(uri), NULL);
    RETURN_VAL_IF_FAIL(SOUP_IS_MESSAGE(msg), NULL);
    RETURN_VAL_IF_FAIL(G_IS_INPUT_STREAM(istream), NULL);

    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);
    g_autofree gchar* filename = NULL;
    gint64 file_timestamp = 0;
    gboolean file_exists = FALSE;
    g_autoptr(GdkPixbuf) ret = NULL;
    g_autoptr(GError) err = NULL;

        /* NOTE: If we aren't supplied a filename, we'll just create one by hashing the uri */
    if (utils_str_empty(name))
    {
        gchar hash_str[15];
        guint hash = 0;

        hash = g_str_hash(uri); /* TODO: Replace this with murmur3 hash */

        g_sprintf(hash_str, "%ud", hash);

        filename = g_build_filename(priv->filepath, hash_str, NULL);
    }
    else
        filename = g_build_filename(priv->filepath, name, NULL);

    if (priv->filepath && (file_exists = g_file_test(filename, G_FILE_TEST_EXISTS)))
    {
        file_timestamp = utils_timestamp_file(filename, NULL);
    }

    if (SOUP_STATUS_IS_SUCCESSFUL(msg->status_code))
    {
        const gchar* last_modified_str = NULL;

        DEBUG("Successful return code from uri '%s'", uri);

        last_modified_str = soup_message_headers_get_one(msg->response_headers, "Last-Modified");

        if (utils_str_empty(last_modified_str))
        {
            DEBUG("No 'Last-Modified' header in response from uri '%s'", uri);

            goto download;
        }
        else if (utils_http_full_date_to_timestamp(last_modified_str) < file_timestamp)
        {
            DEBUG("No new image at uri '%s'", uri);

            if (file_exists)
            {
                DEBUG("Loading image from file '%s'", filename);

                ret = gdk_pixbuf_new_from_file(filename, NULL);

                if (from_file) *from_file = TRUE;
            }
            else
            {
                DEBUG("Image doesn't exist locally");

                goto download;
            }
        }
        else
        {
        download:
            DEBUG("New image at uri '%s'", uri);

            ret = gdk_pixbuf_new_from_stream(istream, NULL, &err);

            if (err)
            {
                WARNING("Unable to download image from uri '%s' because: %s",
                    uri, err->message);

                g_propagate_prefixed_error(error, g_steal_pointer(&err),
                    "Unable to download image from uri '%s' because: ", uri);

                return NULL;
            }

            if (priv->filepath && STRING_EQUALS(priv->image_filetype, GT_IMAGE_FILETYPE_JPEG))
            {
                gdk_pixbuf_save(ret, filename, priv->image_filetype,
                    NULL, "quality", "100", NULL);
            }
            else if (priv->filepath)
            {
                gdk_pixbuf_save(ret, filename, priv->image_filetype,
                    NULL, NULL);
            }

            if (from_file) *from_file = FALSE;
        }
    }

    return g_steal_pointer(&ret);
}

static void
download_cb(ResourceData* data,
    gpointer udata)
{
    RETURN_IF_FAIL(data != NULL);

    g_autoptr(GdkPixbuf) ret = NULL;
    g_autoptr(GError) err = NULL;
    gboolean from_file = FALSE;

    ret = download_image(data->self, data->uri, data->name, data->msg,
        data->istream, &from_file, &err);

    data->cb(from_file ? NULL : g_steal_pointer(&ret),
        data->udata, g_steal_pointer(&err));

    resource_data_free(data);
}

static void
send_message_cb(GObject* source,
    GAsyncResult* res, gpointer udata)
{
    RETURN_IF_FAIL(SOUP_IS_SESSION(source));
    RETURN_IF_FAIL(G_IS_ASYNC_RESULT(res));
    RETURN_IF_FAIL(udata != NULL);

    ResourceData* data = udata;
    GtResourceDownloader* self = GT_RESOURCE_DOWNLOADER(data->self);
    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);
    g_autoptr(GError) err = NULL;

    data->istream = soup_session_send_finish(SOUP_SESSION(source), res, &err);

    if (!err)
        g_thread_pool_push(dl_pool, data, NULL);
    else
    {
        data->cb(NULL, data->udata, g_steal_pointer(&err));
        resource_data_free(data);
    }
}

static void
finalize(GObject* obj)
{
    g_assert(GT_IS_RESOURCE_DOWNLOADER(obj));

    GtResourceDownloader* self = GT_RESOURCE_DOWNLOADER(obj);
    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);

    g_free(priv->filepath);

    G_OBJECT_CLASS(gt_resource_downloader_parent_class)->finalize(obj);
}

static void
dispose(GObject* obj)
{
    g_assert(GT_IS_RESOURCE_DOWNLOADER(obj));

    GtResourceDownloader* self = GT_RESOURCE_DOWNLOADER(obj);
    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);

    MESSAGE("Finalize");

    g_clear_object(&priv->soup);

    G_OBJECT_CLASS(gt_resource_downloader_parent_class)->dispose(obj);
}

static void
gt_resource_downloader_class_init(GtResourceDownloaderClass* klass)
{
    G_OBJECT_CLASS(klass)->finalize = finalize;
    G_OBJECT_CLASS(klass)->dispose = dispose;

    dl_pool = g_thread_pool_new((GFunc) download_cb, NULL, g_get_num_processors(), FALSE, NULL);
}

static void
gt_resource_downloader_init(GtResourceDownloader* self)
{
    g_assert(GT_IS_RESOURCE_DOWNLOADER(self));

    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);

    priv->soup = soup_session_new();

    g_mutex_init(&priv->mutex);
}

GtResourceDownloader*
gt_resource_downloader_new()
{
    GtResourceDownloader* ret = g_object_new(GT_TYPE_RESOURCE_DOWNLOADER, NULL);

    return ret;
}


GtResourceDownloader*
gt_resource_downloader_new_with_cache(const gchar* filepath)
{
    RETURN_VAL_IF_FAIL(!utils_str_empty(filepath), NULL);

    GtResourceDownloader* ret = g_object_new(GT_TYPE_RESOURCE_DOWNLOADER, NULL);
    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(ret);

    priv->filepath = g_strdup(filepath);
    priv->image_filetype = NULL;

    return ret;
}

GdkPixbuf*
gt_resource_downloader_download_image(GtResourceDownloader* self,
    const gchar* uri, const gchar* name, GError** error)
{
    RETURN_VAL_IF_FAIL(GT_IS_RESOURCE_DOWNLOADER(self), NULL);
    RETURN_VAL_IF_FAIL(!utils_str_empty(uri), NULL);

    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);
    g_autoptr(SoupMessage) msg = NULL;
    g_autoptr(GInputStream) istream = NULL;
    g_autoptr(GdkPixbuf) ret = NULL;
    g_autoptr(GError) err = NULL;

    DEBUG("Downloading image from uri '%s'", uri);

    msg = soup_message_new(SOUP_METHOD_GET, uri);

    /* NOTE: So libsoup isn't actually all that thread safe and
     * calling soup_session_send from multiple threads causes it to
     * crash, so we wrap a mutex around it. One should use the
     * download_image_immediately func if one wants to download
     * several images at the same time */
    g_mutex_lock(&priv->mutex);
    istream = soup_session_send(priv->soup, msg, NULL, &err);
    g_mutex_unlock(&priv->mutex);

    if (err)
    {
        WARNING("Unable to download image from uri '%s' because: %s",
            uri, err->message);

        g_propagate_prefixed_error(error, g_steal_pointer(&err),
            "Unable to download image from uri '%s' because: ", uri);

        return NULL;
    }

    ret = download_image(self, uri, name, msg, istream, NULL, error);

    return g_steal_pointer(&ret);
}


static void
download_image_async_cb(GTask* task, gpointer source,
    gpointer task_data, GCancellable* cancel)
{
    RETURN_IF_FAIL(GT_IS_RESOURCE_DOWNLOADER(source));
    RETURN_IF_FAIL(G_IS_TASK(task));
    RETURN_IF_FAIL(task_data != NULL);

    GtResourceDownloader* self = GT_RESOURCE_DOWNLOADER(source);
    GError* err = NULL;
    GenericTaskData* data = task_data;

    GdkPixbuf* ret = gt_resource_downloader_download_image(self,
        data->str_1, data->str_2, &err);

    if (err)
        g_task_return_error(task, err);
    else
        g_task_return_pointer(task, ret, (GDestroyNotify) g_object_unref);
}

void
gt_resource_downloader_download_image_async(GtResourceDownloader* self,
    const gchar* uri, const gchar* name, GAsyncReadyCallback cb,
    GCancellable* cancel, gpointer udata)
{
    RETURN_IF_FAIL(GT_IS_RESOURCE_DOWNLOADER(self));

    g_autoptr(GTask) task = g_task_new(self, cancel, cb, udata);
    GenericTaskData* data = generic_task_data_new();

    data->str_1 = g_strdup(uri);
    data->str_2 = g_strdup(name);

    g_task_set_task_data(task, data, (GDestroyNotify) generic_task_data_free);

    g_task_run_in_thread(task, download_image_async_cb);
}

GdkPixbuf*
gt_resource_donwloader_download_image_finish(GtResourceDownloader* self,
    GAsyncResult* result, GError** error)
{
    RETURN_VAL_IF_FAIL(GT_IS_RESOURCE_DOWNLOADER(self), NULL);
    RETURN_VAL_IF_FAIL(G_IS_ASYNC_RESULT(result), NULL);

    GdkPixbuf* ret = g_task_propagate_pointer(G_TASK(result), error);

    return ret;
}

void
gt_resource_downloader_set_image_filetype(GtResourceDownloader* self, const gchar* image_filetype)
{
    RETURN_IF_FAIL(GT_IS_RESOURCE_DOWNLOADER(self));
    RETURN_IF_FAIL(!utils_str_empty(image_filetype))

    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);

    priv->image_filetype = g_strdup(image_filetype);
}

/* FIXME: Make cancellable */
GdkPixbuf*
gt_resource_downloader_download_image_immediately(GtResourceDownloader* self,
    const gchar* uri, const gchar* name, ResourceDownloaderFunc cb,
    gpointer udata, GError** error)
{
    RETURN_VAL_IF_FAIL(GT_IS_RESOURCE_DOWNLOADER(self), NULL);
    RETURN_VAL_IF_FAIL(!utils_str_empty(uri), NULL);

    GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self);
    g_autofree gchar* filename = NULL;
    g_autoptr(GdkPixbuf) ret = NULL;
    g_autoptr(GError) err = NULL;
    g_autoptr(SoupMessage) msg = NULL;
    ResourceData* data = NULL;

    /* NOTE: If we aren't supplied a filename, we'll just create one by hashing the uri */
    if (utils_str_empty(name))
    {
        gchar hash_str[15];
        guint hash = 0;

        hash = g_str_hash(uri); /* TODO: Replace this with murmur3 hash */

        g_sprintf(hash_str, "%ud", hash);

        filename = g_build_filename(priv->filepath, hash_str, NULL);
    }
    else
        filename = g_build_filename(priv->filepath, name, NULL);

    if (priv->filepath && g_file_test(filename, G_FILE_TEST_EXISTS))
    {
        ret = gdk_pixbuf_new_from_file(filename, &err);

        if (err)
        {
            WARNING("Unable to download image because: %s", err->message);

            g_propagate_prefixed_error(error, g_steal_pointer(&err),
                "Unable to download image because: ");

            /* NOTE: Don't return here as we still might be able to
             * download a new image*/
        }
    }

    msg = soup_message_new(SOUP_METHOD_GET, uri);
    soup_message_headers_append(msg->request_headers, "Client-ID", CLIENT_ID);

    data = resource_data_new();
    data->uri = g_strdup(uri);
    data->name = g_strdup(name);
    data->cb = cb;
    data->udata = udata;
    data->self = g_object_ref(self);
    data->msg = g_steal_pointer(&msg);

    soup_session_send_async(priv->soup, data->msg, NULL, send_message_cb, data);

    /* NOTE: Return any found image immediately */
    return g_steal_pointer(&ret);
}