From a2ce709d8ca6d77a7bfce0c7e2f695c4ba4c88de Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Mon, 18 Feb 2013 00:12:51 +0000 Subject: [PATCH] (web) #5290 'Use HTML5's FileReader to upload .torrent files': done --- extras/rpc-spec.txt | 8 ++-- libtransmission/rpc-server.c | 72 ---------------------------------- web/javascript/transmission.js | 47 +++++++++++++--------- 3 files changed, 32 insertions(+), 95 deletions(-) diff --git a/extras/rpc-spec.txt b/extras/rpc-spec.txt index d2b985abe..fed41124a 100644 --- a/extras/rpc-spec.txt +++ b/extras/rpc-spec.txt @@ -770,8 +770,6 @@ Its functionality has been superceded by the 'free-space' method. 2. HTTP POSTs to http://server:port/transmission/upload will fail. - It's been superceded by http://server:port/transmission/upload2. - Example in https://trac.transmissionbt.com/changeset/14005/#file8 - NB: not to be confused with section 3.4's torrent-add method. - /upload and /upload2 are undocumented features to help web clients - upload .torrent files. + This was an undocumented hack to allow web clients to add files without + client-side access to the file. This functionality is superceded by + using HTML5's FileReader object + the documented 'torrent-add' method. diff --git a/libtransmission/rpc-server.c b/libtransmission/rpc-server.c index 80737de41..8ccbcbe9b 100644 --- a/libtransmission/rpc-server.c +++ b/libtransmission/rpc-server.c @@ -303,74 +303,6 @@ handle_upload (struct evhttp_request * req, **** ***/ -static void -handle_rpc_from_json (struct evhttp_request * req, - struct tr_rpc_server * server, - const char * json, - size_t json_len); - -static void -handle_upload2 (struct evhttp_request * req, - struct tr_rpc_server * server) -{ - if (req->type != EVHTTP_REQ_POST) - { - send_simple_response (req, 405, NULL); - } - else - { - const char * val; - tr_variant top; - tr_variant * args; - char * json; - int json_len; - - tr_variantInitDict (&top, 2); - tr_variantDictAddStr (&top, TR_KEY_method, "torrent-add"); - args = tr_variantDictAddDict (&top, TR_KEY_arguments, 3); - - if ((val = evhttp_find_header (req->input_headers, "X-Transmission-Add-Paused"))) - tr_variantDictAddBool (args, TR_KEY_paused, !tr_strcmp0(val,"true")); - - if ((val = evhttp_find_header (req->input_headers, "X-Transmission-Add-Download-Dir")) && *val) - tr_variantDictAddStr (args, TR_KEY_download_dir, val); - - if ((val = evhttp_find_header (req->input_headers, "X-Transmission-Add-URL")) && *val) - { - tr_variantDictAddStr (args, TR_KEY_filename, val); - } - else - { - int i; - int n; - bool have_source = false; - tr_ptrArray parts = TR_PTR_ARRAY_INIT; - - extract_parts_from_multipart (req->input_headers, req->input_buffer, &parts); - n = tr_ptrArraySize (&parts); - for (i=0; !have_source && ibody, p->body_len)) - { - char * b64 = tr_base64_encode (p->body, p->body_len, NULL); - tr_variantDictAddStr (args, TR_KEY_metainfo, b64); - have_source = true; - tr_free (b64); - tr_variantFree (&test); - } - } - tr_ptrArrayDestruct (&parts, (PtrArrayForeachFunc)tr_mimepart_free); - } - - json = tr_variantToStr (&top, TR_VARIANT_FMT_JSON, &json_len); - handle_rpc_from_json (req, server, json, json_len); - tr_free (json); - tr_variantFree (&top); - } -} - static const char* mimetype_guess (const char * path) { @@ -755,10 +687,6 @@ handle_request (struct evhttp_request * req, void * arg) tr_free (tmp); } #endif - else if (!strcmp (req->uri + strlen (server->url), "upload2")) - { - handle_upload2 (req, server); - } else if (!strncmp (req->uri + strlen (server->url), "rpc", 3)) { handle_rpc (req, server); diff --git a/web/javascript/transmission.js b/web/javascript/transmission.js index 7fe291794..18cf55dce 100644 --- a/web/javascript/transmission.js +++ b/web/javascript/transmission.js @@ -901,7 +901,8 @@ Transmission.prototype = */ uploadTorrentFile: function(confirmed) { - var formData, + var i, file, + reader, fileInput = $('input#torrent_upload_file'), folderInput = $('input#add-dialog-folder-input'), startInput = $('input#torrent_auto_start'), @@ -923,23 +924,33 @@ Transmission.prototype = } else { - formData = new FormData (); - jQuery.each(fileInput[0].files, function(i, file) { - formData.append ('file-'+i, file); - }); - $.ajax ({ - url: '../upload2', - data: formData, - headers : { - 'X-Transmission-Session-Id': this.remote._token, - 'X-Transmission-Add-Paused': !startInput.is(':checked'), - 'X-Transmission-Add-Download-Dir': folderInput.val(), - 'X-Transmission-Add-URL': urlInput.val() - }, - cache: false, - contentType: false, - processData: false, - type: 'POST' + var paused = !startInput.is(':checked'), + destination = folderInput.val(), + remote = this.remote; + + jQuery.each (fileInput[0].files, function(i,file) { + var reader = new FileReader(); + reader.onload = function(e) { + var contents = e.target.result; + var key = "base64," + var index = contents.indexOf (key); + if (index > -1) { + var metainfo = contents.substring (index + key.length); + var o = { + 'method': 'torrent-add', + arguments: { + 'paused': paused, + 'download-dir': destination, + 'metainfo': metainfo + } + }; + remote.sendRequest (o, function(response) { + if (response.result != 'success') + alert ('Error adding "' + file.name + '": ' + response.result); + }); + } + } + reader.readAsDataURL (file); }); } }, -- 2.40.0