From c46bc049d14a3e28e3d64f3043bd48ecef2a02da Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Wed, 5 Oct 2011 01:28:06 +0100 Subject: web: add files resource to the web server Add a files resource that allows Ext to fetch the file tree via one of it's ajax proxies. --- deluge/ui/web/server.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/deluge/ui/web/server.py b/deluge/ui/web/server.py index 51534a04d..4fcb95da2 100644 --- a/deluge/ui/web/server.py +++ b/deluge/ui/web/server.py @@ -87,6 +87,7 @@ CONFIG_DEFAULTS = { "cert": "ssl/daemon.cert" } +FILES_KEYS = ["files", "file_progress", "file_priorities"] PEERS_KEYS = ["peers"] UI_CONFIG_KEYS = ( @@ -216,6 +217,60 @@ class TorrentResource(resource.Resource): request.write(compress(json.dumps(response), request)) request.finish() +class Files(TorrentResource): + """ + """ + + def on_got_files(self, torrent, request): + files = torrent.get("files") + file_progress = torrent.get("file_progress") + file_priorities = torrent.get("file_priorities") + + paths = [] + info = {} + + for i, torrent_file in enumerate(files): + path = torrent_file["path"] + paths.append(path) + torrent_file["progress"] = file_progress[i] + torrent_file["priority"] = file_priorities[i] + torrent_file["index"] = i + torrent_file["path"] = path + info[path] = torrent_file + + # Update the directory info + dirname = os.path.dirname(path) + while dirname: + dirinfo = info.setdefault(dirname, {}) + dirinfo["size"] = dirinfo.get("size", 0) + torrent_file["size"] + if "priority" not in dirinfo: + dirinfo["priority"] = torrent_file["priority"] + else: + if dirinfo["priority"] != torrent_file["priority"]: + dirinfo["priority"] = 9 + + progresses = dirinfo.setdefault("progresses", []) + progresses.append(torrent_file["size"] * (torrent_file["progress"] / 100.0)) + dirinfo["progress"] = float(sum(progresses)) / dirinfo["size"] * 100 + dirinfo["path"] = dirname + dirname = os.path.dirname(dirname) + + tree = uicommon.ExtFileTree(paths) + for path, item in tree.walk(): + item.update(info[path]) + self.send_response(tree.get_tree(), request) + + @secure + def render(self, request): + if not hasattr(request, 'torrent_id'): + request.setResponseCode(http.NOT_FOUND) + return '

Not Found

' + + component.get("SessionProxy" + ).get_torrent_status(request.torrent_id, FILES_KEYS + ).addCallback(self.on_got_files, request) + return server.NOT_DONE_YET + class Peers(TorrentResource): """ Returns a list of the peers that a torrent currently has in JSON format. @@ -230,6 +285,9 @@ class Peers(TorrentResource): @secure def render(self, request): + if not hasattr(request, 'torrent_id'): + request.setResponseCode(http.NOT_FOUND) + return '

Not Found

' component.get("SessionProxy" ).get_torrent_status(request.torrent_id, PEERS_KEYS ).addCallback(self.on_got_peers, request) @@ -504,6 +562,7 @@ class TopLevel(resource.Resource): self.putChild("tracker", Tracker()) # Torrent REST resources + self.putChild("files", Files()) self.putChild("peers", Peers()) theme = component.get("DelugeWeb").config["theme"] -- cgit