Skip to content

Commit

Permalink
Implemented endpoint to export a download to torrent file
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Jun 25, 2016
1 parent 4d13aeb commit d303f12
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Tribler/Core/Modules/restapi/downloads_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class DownloadSpecificEndpoint(DownloadBaseEndpoint):
def __init__(self, session, infohash):
DownloadBaseEndpoint.__init__(self, session)
self.infohash = bytes(infohash.decode('hex'))
self.putChild("torrent", DownloadExportTorrentEndpoint(session, self.infohash))

def render_DELETE(self, request):
"""
Expand Down Expand Up @@ -208,3 +209,37 @@ def render_PATCH(self, request):
return json.dumps({"error": "unknown state parameter"})

return json.dumps({"modified": True})


class DownloadExportTorrentEndpoint(DownloadBaseEndpoint):
"""
This class is responsible for requests that are exporting a download to a .torrent file.
"""

def __init__(self, session, infohash):
DownloadBaseEndpoint.__init__(self, session)
self.infohash = infohash

def render_GET(self, request):
"""
.. http:get:: /download/(string: infohash)/torrent
A GET request to this endpoint returns the .torrent file associated with the specified download.
**Example request**:
.. sourcecode:: none
curl -X GET http://localhost:8085/download/4344503b7e797ebf31582327a5baae35b11bda01/torrent
**Example response**:
The contents of the .torrent file.
"""
download = self.session.get_download(self.infohash)
if not download:
return DownloadExportTorrentEndpoint.return_404(request)

request.setHeader(b'content-type', 'application/x-bittorrent')
request.setHeader(b'Content-Disposition', 'attachment; filename=%s.torrent' % self.infohash.encode('hex'))
return self.session.get_collected_torrent(self.infohash)

0 comments on commit d303f12

Please sign in to comment.