mirror of
https://github.com/transmission/transmission
synced 2024-12-22 07:42:37 +00:00
(web) #5290 'Use HTML5's FileReader to upload .torrent files': done
This commit is contained in:
parent
b24a550459
commit
a2ce709d8c
3 changed files with 32 additions and 95 deletions
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue