From 53e85e0de108ddb9bc8499da2538aec46a6ed724 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 27 Dec 2014 22:26:24 -0600 Subject: [PATCH] added the framework for supporting new link types in the file-get-links API --- fuse/filecache.c | 4 +++- mfapi/apicalls.c | 8 +++++++ mfapi/apicalls.h | 10 +++++++- mfapi/apicalls/file_get_links.c | 27 +++++++++++++++------- mfapi/link_type.def | 41 +++++++++++++++++++++++++++++++++ mfshell/commands/get.c | 4 +++- mfshell/commands/links.c | 6 ++++- 7 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 mfapi/link_type.def diff --git a/fuse/filecache.c b/fuse/filecache.c index d1d48e5..c53de89 100644 --- a/fuse/filecache.c +++ b/fuse/filecache.c @@ -334,7 +334,9 @@ static int filecache_download_file(const char *filecache_path, remote_revision); file = file_alloc(); - retval = mfconn_api_file_get_links(conn, file, (char *)quickkey); + retval = mfconn_api_file_get_links(conn, file, + (char *)quickkey, + LINK_TYPE_DIRECT_DOWNLOAD); if (retval != 0) { fprintf(stderr, "mfconn_api_file_get_links failed\n"); diff --git a/mfapi/apicalls.c b/mfapi/apicalls.c index 1b3735d..00459d6 100644 --- a/mfapi/apicalls.c +++ b/mfapi/apicalls.c @@ -22,6 +22,14 @@ #include "../utils/http.h" +#define X_LINK_TYPE(a,b,c) c, +const char *link_types[]={ +#include "link_type.def" +NULL +}; +#undef X_LINK_TYPE + + int mfapi_check_response(json_t * response, const char *apicall) { json_t *j_obj; diff --git a/mfapi/apicalls.h b/mfapi/apicalls.h index 32d42ee..77d657e 100644 --- a/mfapi/apicalls.h +++ b/mfapi/apicalls.h @@ -43,6 +43,13 @@ enum mfconn_device_change_type { MFCONN_DEVICE_CHANGE_END }; +#define X_LINK_TYPE(a,b,c) a, +enum { +#include "link_type.def" +}; +#undef X_LINK_TYPE + + struct mfconn_device_change { enum mfconn_device_change_type change; char key[16]; @@ -58,7 +65,8 @@ int mfconn_api_file_get_info(mfconn * conn, mffile * file, const char *quickkey); int mfconn_api_file_get_links(mfconn * conn, mffile * file, - const char *quickkey); + const char *quickkey, + uint32_t link_mask); int mfconn_api_file_move(mfconn * conn, const char *quickkey, const char *folderkey); diff --git a/mfapi/apicalls/file_get_links.c b/mfapi/apicalls/file_get_links.c index 43e87c1..40f3056 100644 --- a/mfapi/apicalls/file_get_links.c +++ b/mfapi/apicalls/file_get_links.c @@ -23,20 +23,24 @@ #include #include "../../utils/http.h" +#include "../../utils/strings.h" #include "../mfconn.h" #include "../file.h" #include "../apicalls.h" // IWYU pragma: keep + static int _decode_file_get_links(mfhttp * conn, void *data); int mfconn_api_file_get_links(mfconn * conn, mffile * file, - const char *quickkey) + const char *quickkey,uint32_t link_mask) { - const char *api_call; - int retval; - int len; - mfhttp *http; - int i; + const char *api_call; + extern const char *link_types[]; // declared in apicalls.c + char *link_params = NULL; + int retval; + int len; + mfhttp *http; + int i; if (conn == NULL) return -1; @@ -52,13 +56,18 @@ int mfconn_api_file_get_links(mfconn * conn, mffile * file, if (len != 11 && len != 15) return -1; + link_params = strdup_printf("link_type=%s", + link_types[link_mask]); + for (i = 0; i < mfconn_get_max_num_retries(conn); i++) { api_call = mfconn_create_signed_get(conn, 0, "file/get_links.php", "?quick_key=%s" - "&link_type=direct_download" - "&response_format=json", quickkey); + "&%s" + "&response_format=json", + link_params, quickkey); if (api_call == NULL) { fprintf(stderr, "mfconn_create_signed_get failed\n"); + if(link_params != NULL) free(link_params); return -1; } @@ -86,6 +95,8 @@ int mfconn_api_file_get_links(mfconn * conn, mffile * file, } } + if(link_params != NULL) free(link_params); + return retval; } diff --git a/mfapi/link_type.def b/mfapi/link_type.def new file mode 100644 index 0000000..134e74e --- /dev/null +++ b/mfapi/link_type.def @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2013 Bryan Christ + * 2014 Johannes Schauer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2, as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + x-macro for link type definitions used with the file/get_links as of + REST API version 1.2 and higher. + + + field definitions + + field 1 - enumerated name + field 2 - bit value + field 3 - REST API paramenter string +*/ + +X_LINK_TYPE(LINK_TYPE_SHARING, (1 << 0), "normal_download") +X_LINK_TYPE(LINK_TYPE_DIRECT_DOWNLOAD, (1 << 1), "direct_download") +X_LINK_TYPE(LINK_TYPE_VIEW, (1 << 2), "view") +X_LINK_TYPE(LINK_TYPE_EDIT, (1 << 3), "edit") +X_LINK_TYPE(LINK_TYPE_WATCH, (1 << 4), "watch") +X_LINK_TYPE(LINK_TYPE_LISTEN, (1 << 5), "listen") +X_LINK_TYPE(LINK_TYPE_STREAMING, (1 << 6), "streaming") + +X_LINK_TYPE(LINK_TYPE_ONE_TIME_DOWNLOAD, (1 << 16), "one_time_download") + diff --git a/mfshell/commands/get.c b/mfshell/commands/get.c index b8741af..0d5d835 100644 --- a/mfshell/commands/get.c +++ b/mfshell/commands/get.c @@ -78,7 +78,9 @@ int mfshell_cmd_get(mfshell * mfshell, int argc, char *const argv[]) return -1; } // request a direct download (streaming) link - retval = mfconn_api_file_get_links(mfshell->conn, file, (char *)quickkey); + retval = mfconn_api_file_get_links(mfshell->conn, file, + (char *)quickkey, + LINK_TYPE_DIRECT_DOWNLOAD); if (retval != 0) { file_free(file); diff --git a/mfshell/commands/links.c b/mfshell/commands/links.c index c21cb18..d3bb7f4 100644 --- a/mfshell/commands/links.c +++ b/mfshell/commands/links.c @@ -59,7 +59,11 @@ int mfshell_cmd_links(mfshell * mfshell, int argc, char *const argv[]) file = file_alloc(); - retval = mfconn_api_file_get_links(mfshell->conn, file, (char *)quickkey); + // when the lower-level call gets updated to support multiple link types + // this should be updated. + retval = mfconn_api_file_get_links(mfshell->conn, file, + (char *)quickkey, + LINK_TYPE_DIRECT_DOWNLOAD); if (retval != 0) { fprintf(stderr, "api call unsuccessful\n"); }