Skip to content

Instantly share code, notes, and snippets.

@julien-c
Created May 15, 2015 16:36
Show Gist options
  • Save julien-c/4eb463ccb27f14f6c90c to your computer and use it in GitHub Desktop.
Save julien-c/4eb463ccb27f14f6c90c to your computer and use it in GitHub Desktop.
Index: web/javascript/transmission.js
===================================================================
--- web/javascript/transmission.js (revision 14528)
+++ web/javascript/transmission.js (working copy)
@@ -519,43 +519,43 @@
},
dragenter: function(ev) {
- if (ev.dataTransfer && ev.dataTransfer.types) {
- var types = ["text/uri-list", "text/plain"];
- for (var i = 0; i < types.length; ++i) {
- // it would be better to look at the links here;
- // sadly, with Firefox, trying would throw.
- if (ev.dataTransfer.types.contains(types[i])) {
- ev.stopPropagation();
- ev.preventDefault();
- ev.dropEffect = "copy";
- return false;
- }
- }
- }
- else if (ev.dataTransfer) {
- ev.dataTransfer.dropEffect = "none";
- }
- return true;
+ ev.dataTransfer.dropEffect = "copy";
+ ev.preventDefault();
+ ev.stopPropagation();
+ return false;
},
drop: function(ev)
{
var i, uri, uris=null,
- types = ["text/uri-list", "text/plain"],
+ textTypes = ["text/uri-list", "text/plain"],
paused = this.shouldAddedTorrentsStart();
- if (!ev.dataTransfer || !ev.dataTransfer.types)
+ if (!ev.dataTransfer || !ev.dataTransfer.files)
return true;
-
- for (i=0; !uris && i<types.length; ++i)
- if (ev.dataTransfer.types.contains(types[i]))
- uris = ev.dataTransfer.getData(types[i]).split("\n");
-
- for (i=0; uri=uris[i]; ++i) {
- if (/^#/.test(uri)) // lines which start with "#" are comments
- continue;
- if (/^[a-z-]+:/i.test(uri)) // close enough to a url
- this.remote.addTorrentByUrl(uri, paused);
+
+ var files = ev.dataTransfer.files;
+ for (var f = 0; f < files.length; f++) {
+ var file = files[f];
+
+ if (file.type === 'application/x-bittorrent') {
+ this.addTorrentByMetainfo(file, paused);
+ }
+ else if (textTypes.indexOf(file.type) !== -1) {
+ var thatRemote = this.remote;
+ var reader = new FileReader();
+ reader.onload = function(e) {
+ uris = e.target.result.split("\n");
+ for (i=0; uri=uris[i]; ++i) {
+ if (/^#/.test(uri)) // lines which start with "#" are comments
+ continue;
+ if (/^[a-z-]+:/i.test(uri)) // close enough to a url
+ thatRemote.addTorrentByUrl(uri, paused);
+ }
+ };
+ reader.readAsText(file);
+ }
+
}
ev.preventDefault();
@@ -909,7 +909,34 @@
{
return this.prefsDialog.shouldAddedTorrentsStart();
},
-
+
+
+ addTorrentByMetainfo: function(file, paused, destination) {
+ var reader = new FileReader();
+ var thatRemote = this.remote;
+ 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
+ }
+ };
+ thatRemote.sendRequest(o, function(response) {
+ if (response.result != 'success')
+ alert ('Error adding "' + file.name + '": ' + response.result);
+ });
+ }
+ }
+ reader.readAsDataURL(file);
+ },
+
/*
* Select a torrent file to upload
*/
@@ -939,33 +966,11 @@
else
{
var paused = !startInput.is(':checked'),
- destination = folderInput.val(),
- remote = this.remote;
+ destination = folderInput.val();
- 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);
- });
+ jQuery.each(fileInput[0].files, $.proxy(function(i, file) {
+ this.addTorrentByMetainfo(file, paused, destination);
+ }, this));
var url = $('#torrent_upload_url').val();
if (url != '') {
@@ -979,7 +984,7 @@
'filename': url
}
};
- remote.sendRequest (o, function(response) {
+ this.remote.sendRequest (o, function(response) {
if (response.result != 'success')
alert ('Error adding "' + url + '": ' + response.result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment