Allow file upload

- add upload/simple
 - add upload/poll_upload
 - add "put" command to mfshell
 - add http POST method
This commit is contained in:
josch
2014-12-04 10:34:54 +01:00
parent 1e15dcc16b
commit f1bf926145
12 changed files with 515 additions and 12 deletions

View File

@@ -27,7 +27,10 @@ add_library(mfapi SHARED
mfapi/apicalls/device_get_status.c
mfapi/apicalls/device_get_changes.c
mfapi/apicalls/device_get_patch.c
mfapi/apicalls/device_get_updates.c)
mfapi/apicalls/device_get_updates.c
mfapi/apicalls/upload_simple.c
mfapi/apicalls/upload_poll_upload.c
)
add_library(mfutils SHARED
utils/http.c
@@ -46,6 +49,7 @@ add_executable(mediafire-shell
mfshell/commands/debug.c
mfshell/commands/file.c
mfshell/commands/get.c
mfshell/commands/put.c
mfshell/commands/help.c
mfshell/commands/host.c
mfshell/commands/lcd.c

View File

@@ -95,4 +95,13 @@ int mfconn_api_device_get_patch(mfconn * conn, mfpatch * patch,
uint64_t source_revision,
uint64_t target_revision);
int mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
const char *file_path,
const char *file_name,
char **upload_key);
int mfconn_api_upload_poll_upload(mfconn * conn,
const char *upload_key,
int *status, int *fileerror);
#endif

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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.
*
*/
#include <jansson.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "../../utils/http.h"
#include "../../utils/json.h"
#include "../mfconn.h"
#include "../apicalls.h" // IWYU pragma: keep
struct upload_poll_upload_response {
int status;
int fileerror;
};
static int _decode_upload_poll_upload(mfhttp * conn, void *data);
int
mfconn_api_upload_poll_upload(mfconn * conn, const char *upload_key,
int *status, int *fileerror)
{
const char *api_call;
int retval;
mfhttp *http;
struct upload_poll_upload_response response;
if (conn == NULL)
return -1;
if (upload_key == NULL)
return -1;
// make an UNSIGNED get
api_call = mfconn_create_unsigned_get(conn, 0,
"upload/poll_upload.php",
"?response_format=json"
"&key=%s", upload_key);
http = http_create();
retval = http_get_buf(http, api_call, _decode_upload_poll_upload,
&response);
http_destroy(http);
free((void *)api_call);
*status = response.status;
*fileerror = response.fileerror;
return retval;
}
static int _decode_upload_poll_upload(mfhttp * conn, void *user_ptr)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *j_obj;
struct upload_poll_upload_response *response;
response = (struct upload_poll_upload_response *)user_ptr;
if (response == NULL)
return -1;
response->status = 0;
response->fileerror = 0;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root, "response/doupload");
// make sure that the result code is zero (success)
j_obj = json_object_get(node, "result");
if (j_obj == NULL || strcmp(json_string_value(j_obj), "0") != 0) {
json_decref(root);
return -1;
}
j_obj = json_object_get(node, "status");
if (j_obj != NULL) {
response->status = atol(json_string_value(j_obj));
}
j_obj = json_object_get(node, "fileerror");
if (j_obj != NULL) {
response->fileerror = atol(json_string_value(j_obj));
}
if (root != NULL)
json_decref(root);
return 0;
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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.
*
*/
#define _POSIX_C_SOURCE 200809L // for strdup
#include <jansson.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <openssl/sha.h>
#include <sys/stat.h>
#include "../../utils/http.h"
#include "../../utils/json.h"
#include "../../utils/hash.h"
#include "../mfconn.h"
#include "../apicalls.h" // IWYU pragma: keep
static int _decode_upload_simple(mfhttp * conn, void *data);
int
mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
const char *file_path, const char *file_name,
char **upload_key)
{
const char *api_call;
int retval;
mfhttp *http;
FILE *fh;
unsigned char hash[SHA256_DIGEST_LENGTH];
char *file_hash;
struct stat file_info;
if (conn == NULL)
return -1;
fh = fopen(file_path, "r");
if (fh == NULL) {
perror("cannot open file");
fprintf(stderr, "cannot open %s\n", file_path);
return -1;
}
retval = calc_sha256(fh, hash);
fclose(fh);
if (retval != 0) {
fprintf(stderr, "failed to calculate hash\n");
return -1;
}
memset(&file_info, 0, sizeof(file_info));
retval = stat(file_path, &file_info);
if (retval != 0) {
fprintf(stderr, "stat failed\n");
return -1;
}
file_hash = binary2hex(hash, SHA256_DIGEST_LENGTH);
if (folderkey == NULL) {
api_call = mfconn_create_signed_get(conn, 0,
"upload/simple.php",
"?response_format=json");
} else {
api_call = mfconn_create_signed_get(conn, 0,
"upload/simple.php",
"?response_format=json"
"&folder_key=%s", folderkey);
}
http = http_create();
retval = http_post_file(http, api_call, file_path, file_name,
file_info.st_size, file_hash,
_decode_upload_simple, upload_key);
http_destroy(http);
free(file_hash);
free((void *)api_call);
return retval;
}
static int _decode_upload_simple(mfhttp * conn, void *user_ptr)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *j_obj;
char **upload_key;
upload_key = (char **)user_ptr;
if (upload_key == NULL)
return -1;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root, "response/doupload");
j_obj = json_object_get(node, "key");
if (j_obj != NULL) {
*upload_key = strdup(json_string_value(j_obj));
} else {
*upload_key = NULL;
}
if (root != NULL)
json_decref(root);
return 0;
}

View File

@@ -65,6 +65,8 @@ mfconn_api_user_get_session_token(mfconn * conn, const char *server,
mfconn_create_user_signature(conn, username, password, app_id,
app_key);
// FIXME: username and password have to be urlencoded (maybe using
// curl_easy_escape)
post_args = strdup_printf("email=%s"
"&password=%s"
"&application_id=%d"

View File

@@ -174,6 +174,69 @@ const char *mfconn_create_call_signature(mfconn * conn, const char *url,
return strdup((const char *)signature_hex);
}
const char *mfconn_create_unsigned_get(mfconn * conn, int ssl,
const char *api, const char *fmt,
...)
{
char *api_request = NULL;
char *api_args = NULL;
int bytes_to_alloc;
int api_args_len;
int api_len;
va_list ap;
if (conn == NULL)
return NULL;
if (conn->server == NULL)
return NULL;
// make sure the api (ex: user/get_info.php) is sane
if (api == NULL)
return NULL;
api_len = strlen(api);
if (api_len < 3)
return NULL;
// calculate how big of a buffer we need
va_start(ap, fmt);
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
va_end(ap);
// create the correctly sized buffer and process the args
api_args = (char *)calloc(api_args_len, sizeof(char));
// printf("\n\r%d\n\r",api_args_len);
va_start(ap, fmt);
vsnprintf(api_args, api_args_len, fmt, ap);
va_end(ap);
// correct user error of trailing slash
if (api[api_len - 1] == '/')
return NULL;
api_request = strdup_printf("%s//%s/api/%s",
(ssl ? "https:" : "http:"), conn->server, api);
// compute the amount of space requred to realloc() the request
bytes_to_alloc = api_args_len;
bytes_to_alloc += strlen(api_request);
bytes_to_alloc += 1; // null termination
// append api GET args to api request
api_request = (char *)realloc(api_request, bytes_to_alloc);
if (api_request == NULL) {
fprintf(stderr, "cannot allocate memory\n");
return NULL;
}
strncat(api_request, api_args, api_args_len);
free(api_args);
return api_request;
}
const char *mfconn_create_signed_get(mfconn * conn, int ssl,
const char *api, const char *fmt, ...)
{

View File

@@ -34,6 +34,10 @@ void mfconn_destroy(mfconn * conn);
ssize_t mfconn_download_direct(mffile * file, const char *local_dir);
const char *mfconn_create_unsigned_get(mfconn * conn, int ssl,
const char *api, const char *fmt,
...);
const char *mfconn_create_signed_get(mfconn * conn, int ssl,
const char *api, const char *fmt,
...);

View File

@@ -67,6 +67,9 @@ int mfshell_cmd_mkdir(mfshell * mfshell, int argc,
int mfshell_cmd_get(mfshell * mfshell, int argc,
char *const argv[]);
int mfshell_cmd_put(mfshell * mfshell, int argc,
char *const argv[]);
int mfshell_cmd_whoami(mfshell * mfshell, int argc,
char *const argv[]);

97
mfshell/commands/put.c Normal file
View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
*
* 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.
*
*/
#define _POSIX_C_SOURCE 200809L // for PATH_MAX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include "../../mfapi/apicalls.h"
#include "../mfshell.h"
#include "../../mfapi/mfconn.h"
#include "../../mfapi/folder.h"
#include "../commands.h" // IWYU pragma: keep
int mfshell_cmd_put(mfshell * mfshell, int argc, char *const argv[])
{
int retval;
const char *file_path;
char *temp;
char *file_name;
char *upload_key;
int status;
int fileerror;
if (mfshell == NULL)
return -1;
if (mfshell->conn == NULL) {
fprintf(stderr, "conn is NULL\n");
return -1;
}
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
if (argv[1] == NULL)
return -1;
file_path = argv[1];
// create copies because basename modifies it
temp = strdup(argv[1]);
file_name = basename(temp);
retval = mfconn_api_upload_simple(mfshell->conn,
folder_get_key(mfshell->folder_curr),
file_path, file_name, &upload_key);
mfconn_update_secret_key(mfshell->conn);
free(temp);
if (retval != 0) {
fprintf(stderr, "mfconn_api_upload_simple failed\n");
return -1;
}
fprintf(stderr, "upload_key: %s\n", upload_key);
for (;;) {
retval = mfconn_api_upload_poll_upload(mfshell->conn, upload_key,
&status, &fileerror);
if (retval != 0) {
fprintf(stderr, "mfconn_api_upload_poll_upload failed\n");
return -1;
}
fprintf(stderr, "status: %d, filerror: %d\n", status, fileerror);
if (status == 99) {
fprintf(stderr, "done\n");
break;
}
sleep(1);
}
free(upload_key);
return 0;
}

View File

@@ -52,6 +52,7 @@ struct mfcmd commands[] = {
{"links", "[quickkey]", "show access urls for the file",
mfshell_cmd_links},
{"get", "[quickkey]", "download a file", mfshell_cmd_get},
{"put", "[local filename]", "upload a file", mfshell_cmd_put},
{"rmdir", "[folderkey]", "remove directory", mfshell_cmd_rmdir},
{"rm", "[quickkey]", "remove file", mfshell_cmd_rm},
{"status", "", "device status", mfshell_cmd_status},

View File

@@ -21,13 +21,18 @@
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
#include "http.h"
#include "strings.h"
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
double ultotal, double ulnow);
static size_t http_read_buf_cb(char *data, size_t size, size_t nmemb,
void *user_ptr);
static size_t http_read_file_cb(char *data, size_t size, size_t nmemb,
void *user_ptr);
static size_t http_write_buf_cb(char *data, size_t size, size_t nmemb,
void *user_ptr);
static size_t http_write_file_cb(char *data, size_t size, size_t nmemb,
@@ -124,6 +129,7 @@ http_get_buf(mfhttp * conn, const char *url,
int retval;
curl_easy_reset(conn->curl_handle);
conn->write_buf_len = 0;
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION,
http_read_buf_cb);
@@ -139,7 +145,6 @@ http_get_buf(mfhttp * conn, const char *url,
}
if (data_handler != NULL)
retval = data_handler(conn, data);
conn->write_buf_len = 0;
return retval;
}
@@ -192,6 +197,7 @@ http_post_buf(mfhttp * conn, const char *url, const char *post_args,
int retval;
curl_easy_reset(conn->curl_handle);
conn->write_buf_len = 0;
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION,
http_read_buf_cb);
@@ -200,8 +206,8 @@ http_post_buf(mfhttp * conn, const char *url, const char *post_args,
http_write_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDS, post_args);
retval = curl_easy_perform(conn->curl_handle);
fprintf(stderr, "POST: %s\n", url);
retval = curl_easy_perform(conn->curl_handle);
if (retval != CURLE_OK) {
fprintf(stderr, "error curl_easy_perform \"%s\" \"%s\"\n\r",
curl_easy_strerror(retval), conn->error_buf);
@@ -209,7 +215,6 @@ http_post_buf(mfhttp * conn, const char *url, const char *post_args,
}
if (data_handler != NULL)
retval = data_handler(conn, data);
conn->write_buf_len = 0;
return retval;
}
@@ -227,6 +232,7 @@ int http_get_file(mfhttp * conn, const char *url, const char *path)
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
// FIXME: handle fopen() return value
conn->stream = fopen(path, "w+");
fprintf(stderr, "GET: %s\n", url);
retval = curl_easy_perform(conn->curl_handle);
fclose(conn->stream);
if (retval != CURLE_OK) {
@@ -240,19 +246,88 @@ static size_t
http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
{
mfhttp *conn;
size_t ret;
if (user_ptr == NULL)
return 0;
conn = (mfhttp *) user_ptr;
fwrite(data, size, nmemb, conn->stream);
ret = fwrite(data, size, nmemb, conn->stream);
fprintf(stderr, "\r %.0f / %.0f", conn->dl_now, conn->dl_len);
return size * nmemb;
return size * ret;
}
/*int
http_post_file(mfhttp *conn, const char *url, const char *post_args, FILE *fd)
static size_t
http_read_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
{
}*/
mfhttp *conn;
size_t ret;
if (user_ptr == NULL)
return 0;
conn = (mfhttp *) user_ptr;
ret = fread(data, size, nmemb, conn->stream);
fprintf(stderr, "\r %.0f / %.0f", conn->dl_now, conn->dl_len);
return size * ret;
}
int
http_post_file(mfhttp * conn, const char *url, const char *path,
const char *filename, uint64_t filesize, const char *fhash,
int (*data_handler) (mfhttp * conn, void *data), void *data)
{
struct curl_slist *custom_headers = NULL;
char *tmpheader;
int retval;
curl_easy_reset(conn->curl_handle);
conn->write_buf_len = 0;
// the following three pseudo headers are interpreted by the mediafire
// server
tmpheader = strdup_printf("x-filename: %s", filename);
custom_headers = curl_slist_append(custom_headers, tmpheader);
free(tmpheader);
tmpheader = strdup_printf("x-filesize: %" PRIu64, filesize);
custom_headers = curl_slist_append(custom_headers, tmpheader);
free(tmpheader);
tmpheader = strdup_printf("x-filehash: %s", fhash);
custom_headers = curl_slist_append(custom_headers, tmpheader);
free(tmpheader);
// when using POST, curl implicitly sets
// Content-Type: application/x-www-form-urlencoded
// make sure it is set to application/octet-stream instead
custom_headers = curl_slist_append(custom_headers,
"Content-Type: application/octet-stream");
// when using POST, curl implicitly sets Expect: 100-continue
// make sure it is not set
custom_headers = curl_slist_append(custom_headers, "Expect:");
curl_easy_setopt(conn->curl_handle, CURLOPT_POST, 1);
curl_easy_setopt(conn->curl_handle, CURLOPT_HTTPHEADER, custom_headers);
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION,
http_read_file_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
http_write_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDSIZE, filesize);
conn->stream = fopen(path, "r");
fprintf(stderr, "POST: %s\n", url);
retval = curl_easy_perform(conn->curl_handle);
fclose(conn->stream);
curl_slist_free_all(custom_headers);
if (retval != CURLE_OK) {
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
return retval;
}
if (data_handler != NULL)
retval = data_handler(conn, data);
return retval;
}

View File

@@ -21,7 +21,7 @@
#include <jansson.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
typedef struct mfhttp mfhttp;
@@ -36,9 +36,12 @@ int http_post_buf(mfhttp * conn, const char *url,
void *data);
int http_get_file(mfhttp * conn, const char *url,
const char *path);
int http_post_file(mfhttp * conn, const char *url,
const char *post_args, FILE * fd);
json_t *http_parse_buf_json(mfhttp * conn, size_t flags,
json_error_t * error);
int http_post_file(mfhttp * conn, const char *url,
const char *path, const char *filename,
uint64_t filesize, const char *fhash,
int (*data_handler) (mfhttp * conn, void *data),
void *data);
#endif