Add magnet uri support to Add Url in Webui

This commit is contained in:
Calum Lind 2012-02-18 00:39:24 +00:00
parent 38210ae11e
commit f897f03227
4 changed files with 96 additions and 33 deletions

View File

@ -174,8 +174,6 @@ Deluge.add.AddWindow = Ext.extend(Deluge.add.Window, {
if (selections.length) {
var record = this.list.getRecord(selections[0]);
this.optionsPanel.setTorrent(record.get('info_hash'));
this.optionsPanel.files.setDisabled(false);
this.optionsPanel.form.setDisabled(false);
} else {
this.optionsPanel.files.setDisabled(true);
this.optionsPanel.form.setDisabled(true);
@ -220,6 +218,7 @@ Deluge.add.AddWindow = Ext.extend(Deluge.add.Window, {
r.set('text', info['name']);
this.list.getStore().commitChanges();
this.optionsPanel.addTorrent(info);
this.list.select(r);
}
},

View File

@ -96,6 +96,9 @@ Deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
var root = this.files.getRootNode();
var priorities = this.form.optionsManager.get('file_priorities');
this.form.setDisabled(false);
if (this.torrents[torrentId]['files_tree']) {
this.walkFileTree(this.torrents[torrentId]['files_tree'], function(filename, type, entry, parentNode) {
var node = new Ext.tree.TreeNode({
download: (entry.index) ? priorities[entry.index] : true,
@ -108,6 +111,14 @@ Deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
if (type == 'dir') return node;
}, this, root);
root.firstChild.expand();
this.files.setDisabled(false);
this.files.show();
} else {
// Files tab is empty so show options tab
this.form.show();
this.files.setDisabled(true);
}
},
walkFileTree: function(files, callback, scope, parentNode) {

View File

@ -81,17 +81,27 @@ Deluge.add.UrlWindow = Ext.extend(Deluge.add.Window, {
var cookies = this.cookieField.getValue();
var torrentId = this.createTorrentId();
if (url.substring(0,20) == 'magnet:?xt=urn:btih:') {
deluge.client.web.get_magnet_info(url, {
success: this.onGotInfo,
scope: this,
filename: url,
torrentId: torrentId
});
} else {
deluge.client.web.download_torrent_from_url(url, cookies, {
success: this.onDownload,
scope: this,
torrentId: torrentId
});
}
this.hide();
this.urlField.setValue('');
this.fireEvent('beforeadd', torrentId, url);
},
onDownload: function(filename, obj, resp, req) {
this.urlField.setValue('');
deluge.client.web.get_torrent_info(filename, {
success: this.onGotInfo,
scope: this,

View File

@ -41,6 +41,7 @@ import logging
import hashlib
import tempfile
from urlparse import urljoin
from urllib import unquote_plus
from types import FunctionType
from twisted.internet import reactor
@ -683,10 +684,8 @@ class WebApi(JSONComponent):
::
{
"filename": the torrent file,
"name": the torrent name,
"size": the total size of the torrent,
"files": the files the torrent contains,
"files_tree": the files the torrent contains,
"info_hash" the torrents info_hash
}
@ -699,6 +698,45 @@ class WebApi(JSONComponent):
log.exception(e)
return False
@export
def get_magnet_info(self, uri):
"""
Return information about a magnet link.
:param uri: the magnet link
:type uri: string
:returns: information about the magnet link:
::
{
"name": the torrent name,
"info_hash": the torrents info_hash,
"files_tree": empty value for magnet links
}
:rtype: dictionary
"""
try:
s = uri.split("&")[0][20:]
if len(s) == 32:
info_hash = base64.b32decode(s).encode("hex")
elif len(s) == 40:
info_hash = s
else:
return False
name = None
for i in uri.split("&")[1:]:
if i[:3] == "dn=":
name = unquote_plus(i.split("=")[1])
if not name:
name = info_hash
return {"name":name, "info_hash":info_hash, "files_tree":''}
except Exception, e:
log.exception(e)
return False
@export
def add_torrents(self, torrents):
"""
@ -717,6 +755,11 @@ class WebApi(JSONComponent):
"""
for torrent in torrents:
if common.is_magnet(torrent["path"]):
log.info("Adding torrent from magnet uri `%s` with options `%r`",
torrent["path"], torrent["options"])
client.core.add_torrent_magnet(torrent["path"], torrent["options"])
else:
filename = os.path.basename(torrent["path"])
fdump = base64.encodestring(open(torrent["path"], "rb").read())
log.info("Adding torrent from file `%s` with options `%r`",