Add support for referer argument in file downloads (Closes #697)

This commit is contained in:
Mathieu Comandon 2017-12-14 00:59:06 -08:00
parent 7d6ae79f18
commit bb3bc1226c
4 changed files with 24 additions and 9 deletions

View file

@ -19,10 +19,11 @@ class Downloader():
ERROR,
COMPLETED) = list(range(5))
def __init__(self, url, dest, overwrite=False):
def __init__(self, url, dest, overwrite=False, referer=None):
self.url = url
self.dest = dest
self.overwrite = overwrite
self.referer = referer
self.stop_request = None
# Read these after a check_progress()
@ -102,7 +103,12 @@ class Downloader():
self.file_pointer.close()
def async_download(self, url, queue, stop_request=None):
request = http.Request(url, stop_request=stop_request,
headers = {}
if self.referer:
headers['Referer'] = self.referer
request = http.Request(url,
stop_request=stop_request,
headers=headers,
thread_queue=queue)
return request.get()

View file

@ -327,11 +327,11 @@ class InstallerDialog(Gtk.Window):
return
self.interpreter.file_selected(file_path)
def start_download(self, file_uri, dest_file, callback=None, data=None):
def start_download(self, file_uri, dest_file, callback=None, data=None, referer=None):
self.clean_widgets()
logger.debug("Downloading %s to %s", file_uri, dest_file)
self.download_progress = DownloadProgressBox(
{'url': file_uri, 'dest': dest_file}, cancelable=True
{'url': file_uri, 'dest': dest_file, 'referer': referer}, cancelable=True
)
self.download_progress.cancel_button.hide()
callback = callback or self.on_download_complete

View file

@ -20,6 +20,7 @@ class DownloadProgressBox(Gtk.VBox):
self.downloader = downloader
self.url = params.get('url')
self.dest = params.get('dest')
self.referer = params.get('referer')
title = params.get('title', "Downloading {}".format(self.url))
self.main_label = Gtk.Label(title)
@ -57,7 +58,9 @@ class DownloadProgressBox(Gtk.VBox):
"""Start downloading a file."""
if not self.downloader:
try:
self.downloader = Downloader(self.url, self.dest,
self.downloader = Downloader(self.url,
self.dest,
referer=self.referer,
overwrite=True)
except RuntimeError as ex:
from lutris.gui.dialogs import ErrorDialog

View file

@ -254,15 +254,19 @@ class ScriptInterpreter(CommandsMixin):
if isinstance(game_file[file_id], dict):
filename = game_file[file_id]['filename']
file_uri = game_file[file_id]['url']
referer = game_file[file_id].get('referer')
else:
file_uri = game_file[file_id]
filename = os.path.basename(file_uri)
referer = None
if file_uri.startswith("/"):
file_uri = "file://" + file_uri
elif file_uri.startswith(("$WINESTEAM", "$STEAM")):
# Download Steam data
self._download_steam_data(file_uri, file_id)
return
if not filename:
raise ScriptingError("No filename provided, please provide 'url' and 'filename' parameters in the script")
@ -293,7 +297,7 @@ class ScriptInterpreter(CommandsMixin):
# Change parent's status
self.parent.set_status('')
self.game_files[file_id] = dest_file
self.parent.start_download(file_uri, dest_file)
self.parent.start_download(file_uri, dest_file, referer=referer)
def _download_steam_data(self, file_uri, file_id):
"""Download the game files from Steam to use them outside of Steam.
@ -766,9 +770,11 @@ class ScriptInterpreter(CommandsMixin):
data_path = self._get_steam_game_path(runner_class)
if not data_path or not os.path.exists(data_path):
raise ScriptingError("Unable to get Steam data for game")
logger.debug("got data path: %s" % data_path)
self.game_files[self.steam_data['file_id']] = \
os.path.join(data_path, self.steam_data['steam_rel_path'])
self.game_files[self.steam_data['file_id']] = os.path.abspath(
os.path.join(
data_path, self.steam_data['steam_rel_path']
)
)
self.iter_game_files()
def eject_wine_disc(self):