]> granicus.if.org Git - transmission/commitdiff
(web) #5290 'Use HTML5's FileReader to upload .torrent files': done
authorJordan Lee <jordan@transmissionbt.com>
Mon, 18 Feb 2013 00:12:51 +0000 (00:12 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Mon, 18 Feb 2013 00:12:51 +0000 (00:12 +0000)
extras/rpc-spec.txt
libtransmission/rpc-server.c
web/javascript/transmission.js

index d2b985abeea930c055b6f91cb82ab38c8d6facd4..fed41124a33de8048c9a195da4b831dbc23627f5 100644 (file)
       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.
index 80737de413e3df6f591afbaacf619f9e06f40531..8ccbcbe9b1779ed9d340e3485c112bd8ef35946b 100644 (file)
@@ -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 && i<n; ++i)
-            {
-              tr_variant test;
-              const struct tr_mimepart * p = tr_ptrArrayNth (&parts, i);
-              if (!tr_variantFromBenc (&test, p->body, 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);
index 7fe291794f9b93f8e679ee294f17cdd19efd45d8..18cf55dceb07cba386f7386c4a66e2953ab6d548 100644 (file)
@@ -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);
                        });
                }
        },