Skip to content
Snippets Groups Projects
Commit fa00212a authored by Antoine Fontaine's avatar Antoine Fontaine :8ball:
Browse files

Storage now makes symlinks so we don't save a file twice

parent bda7a75c
Branches
Tags
1 merge request!11Added folder support
......@@ -36,11 +36,12 @@ typedef struct MoodleProviderState {
static MoodleProviderState self;
typedef void (*handle_stream_cb) (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
provider_cb cb,
gpointer user_data);
typedef void (*handle_stream_cb) (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
SoupMessageHeaders *headers,
provider_cb cb,
gpointer user_data);
struct cb_data {
handle_stream_cb stream_cb; // the function that should handle the response stream
......@@ -70,7 +71,9 @@ request_url_cb (GObject *unused,
res,
NULL);
info->stream_cb (is, save_to_disk, info->filename, info->final_cb, info->user_data);
SoupMessageHeaders *headers;
g_object_get (G_OBJECT (info->msg), "response-headers", &headers, NULL);
info->stream_cb (is, save_to_disk, info->filename, headers, info->final_cb, info->user_data);
g_free (info->filename);
free (info);
......@@ -101,11 +104,12 @@ request_url (const gchar *url,
} else { // offline_mode
// this is not quite async, but works «fast enough»
g_autoptr (GFile) file = g_file_new_for_uri (filename);
GInputStream *is;
is = G_INPUT_STREAM (g_file_read (file, NULL, NULL));
if (G_IS_INPUT_STREAM (is))
stream_cb (is, FALSE, filename, final_cb, user_data);
stream_cb (is, FALSE, filename, NULL, final_cb, user_data);
else
g_warning ("Could not read file %s from cache\n", filename);
......@@ -114,14 +118,30 @@ request_url (const gchar *url,
static void
moodle_downloaded_file_cb (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
provider_cb final_cb,
gpointer user_data)
moodle_downloaded_file_cb (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
SoupMessageHeaders *headers,
provider_cb final_cb,
gpointer user_data)
{
if (should_save_to_disk)
write_g_input_stream_to_file (is, filename);
if (should_save_to_disk) {
// we first try to read the real name (serie3.pdf)
// and if it exists, we make file-123456 a symlink to it
const char *header = soup_message_headers_get_one (headers, "Content-Disposition");
g_autofree char *real_filename = NULL, *name = NULL;
if (sscanf (header, "inline; filename=\"%m[^\"]\"", &name) == 1) {
real_filename = get_resource_filename (RESOURCE_FILE, 0, name);
g_autoptr (GFile) link_file = g_file_new_for_uri (filename);
g_file_make_symbolic_link (link_file, name, NULL, NULL);
} else {
g_warning ("could not read header '%s'\n", header);
}
write_g_input_stream_to_file (is, real_filename ?: filename);
}
final_cb (user_data, g_strdup (filename));
}
......@@ -130,7 +150,7 @@ void
moodle_provider_download_file_async (guint id, provider_cb cb, gpointer cb_arg)
{
g_autofree char *url = g_strdup_printf (EPFL_FILE_LINK"%u&redirect=1", id);
g_autofree char *filename = get_resource_filename (RESOURCE_FILE, id);
g_autofree char *filename = get_resource_filename (RESOURCE_FILE, id, NULL);
request_url (url, filename, SOUP_MESSAGE_PRIORITY_HIGH, moodle_downloaded_file_cb, cb, cb_arg);
}
......@@ -202,11 +222,12 @@ moodle_provider_connect (GInputStream **response)
static void
got_course_content_cb (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
provider_cb final_cb,
gpointer user_data)
got_course_content_cb (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
SoupMessageHeaders *headers,
provider_cb final_cb,
gpointer user_data)
{
gchar *html;
gsize html_size;
......@@ -219,11 +240,12 @@ got_course_content_cb (GInputStream *is,
static void
got_folder_content_cb (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
provider_cb final_cb,
gpointer user_data)
got_folder_content_cb (GInputStream *is,
gboolean should_save_to_disk,
const gchar *filename,
SoupMessageHeaders *headers,
provider_cb final_cb,
gpointer user_data)
{
gchar *html;
gsize html_size;
......@@ -280,7 +302,7 @@ void
moodle_provider_get_folder_content_async (provider_cb cb, guint id, gpointer cb_arg)
{
g_autofree char *url = g_strdup_printf (EPFL_FOLDER_LINK"%u", id);
g_autofree char *filename = get_resource_filename (RESOURCE_FOLDER, id);
g_autofree char *filename = get_resource_filename (RESOURCE_FOLDER, id, NULL);
request_url (url, filename, SOUP_MESSAGE_PRIORITY_HIGH, got_folder_content_cb, cb, cb_arg);
}
......
......@@ -38,13 +38,15 @@ get_cache_dir (void)
}
gchar *
get_resource_filename (gint res_type, guint id/*, gchar *filename*/)
get_resource_filename (gint res_type, guint id, gchar *filename)
{
gchar *filepath;
switch (res_type) {
case RESOURCE_FILE:
//g_warn_if_fail (filename!=NULL);
filepath = g_strdup_printf ("file://%s/file-%u", get_cache_dir (), id);
if (filename)
filepath = g_strdup_printf ("file://%s/%s", get_cache_dir (), filename);
else
filepath = g_strdup_printf ("file://%s/file-%u", get_cache_dir (), id);
break;
case RESOURCE_FOLDER:
filepath = g_strdup_printf ("file://%s/folder-%u.html", get_cache_dir (), id);
......
......@@ -19,7 +19,7 @@
#pragma once
#include <gio/gio.h>
gchar *get_resource_filename (gint res_type, guint id/*, gchar *filename*/);
gchar *get_resource_filename (gint res_type, guint id, gchar *filename); // takes either a filename or an id.
gchar *get_course_filename (guint id);
gchar *get_index_filename (void);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment