Skip to content

Commit

Permalink
Implemented endpoint to start download from file or url
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Jun 11, 2016
1 parent cdbd3b0 commit 2d0efe1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Tribler/Core/Modules/restapi/downloads_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cgi
import json

from twisted.web import http, resource
Expand Down Expand Up @@ -112,6 +113,35 @@ def render_GET(self, request):
downloads_json.append(download_json)
return json.dumps({"downloads": downloads_json})

def render_PUT(self, request):
headers = request.getAllHeaders()
request_data = cgi.FieldStorage(fp=request.content, headers=headers,
environ={'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': headers['content-type']})

if 'source' not in request_data:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "source parameter missing"})

if request_data['source'].value not in ['file', 'url']:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "source parameter should be either file or url"})

if request_data['source'].value == 'url' and 'url' not in request_data:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "url parameter missing"})

if request_data['source'].value == 'file' and 'file' not in request_data:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "file parameter missing"})

if request_data['source'].value == 'url':
self.session.start_download_from_uri(request_data['url'].value)
elif request_data['source'].value == 'file':
tdef = TorrentDef.load_from_memory(request_data['file'].value)
self.session.start_download_from_tdef(tdef)

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


class DownloadSpecificEndpoint(DownloadBaseEndpoint):
"""
Expand Down

0 comments on commit 2d0efe1

Please sign in to comment.