From ed8116c76194d340072e15a3c6f65da3476c861c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 1 Sep 2018 13:02:52 +0100 Subject: [PATCH] Move download handlers into new module Signed-off-by: Michael Tremer --- Makefile.am | 1 - src/web/__init__.py | 4 +-- src/web/download.py | 51 +++++++++++++++++++++++++++ src/web/handlers.py | 1 - src/web/handlers_download.py | 67 ------------------------------------ 5 files changed, 53 insertions(+), 71 deletions(-) delete mode 100644 src/web/handlers_download.py diff --git a/Makefile.am b/Makefile.am index bd0b71d7..0e4719e7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -81,7 +81,6 @@ web_PYTHON = \ src/web/handlers_admin.py \ src/web/handlers_base.py \ src/web/handlers_boot.py \ - src/web/handlers_download.py \ src/web/handlers_fireinfo.py \ src/web/handlers_iuse.py \ src/web/handlers_mirrors.py \ diff --git a/src/web/__init__.py b/src/web/__init__.py index 2db2a1a2..6c3d5479 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -134,8 +134,8 @@ class Application(tornado.web.Application): # downloads.ipfire.org self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [ - (r"/(iso|torrent)/(.*)", DownloadCompatHandler), - (r"/(.*)", DownloadFileHandler), + (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }), + (r"/(.*)", download.FileHandler), ]) # mirrors.ipfire.org diff --git a/src/web/download.py b/src/web/download.py index 689d2ebd..fd18a147 100644 --- a/src/web/download.py +++ b/src/web/download.py @@ -1,5 +1,6 @@ #!/usr/bin/python +import random import tornado.web from . import handlers_base as base @@ -24,6 +25,56 @@ class ReleaseHandler(base.BaseHandler): self.render("download/release.html", release=release) +class FileHandler(base.BaseHandler): + def prepare(self): + self.set_header("Pragma", "no-cache") + + def head(self, filename): + self.redirect_to_mirror(filename) + + def get(self, filename): + self.redirect_to_mirror(filename, log_download=True) + + def find_mirror(self, filename): + exists = self.mirrors.file_exists(filename) + if not exists: + raise tornado.web.HTTPError(404, "File not found: %s" % filename) + + # Find mirrors located near to the user. + # If we have not found any, we use a random one. + remote_location = self.get_remote_location() + + if remote_location: + mirrors = self.mirrors.get_for_location(remote_location, filename=filename) + + if mirrors: + return random.choice(mirrors) + + return self.mirrors.get_random(filename=filename) + + def redirect_to_mirror(self, filename, log_download=False): + # Find a random mirror. + mirror = self.find_mirror(filename) + + # Construct the redirection URL. + download_url = mirror.build_url(filename) + + # Redirect the request. + self.redirect(download_url) + + if not log_download: + return + + remote_location = self.get_remote_location() + if remote_location: + country_code = remote_location.country + else: + country_code = None + + self.db.execute("INSERT INTO log_download(filename, mirror, country_code) \ + VALUES(%s, %s, %s)", filename, mirror.id, country_code) + + class ButtonModule(ui_modules.UIModule): def render(self, release): for arch in release.arches: diff --git a/src/web/handlers.py b/src/web/handlers.py index 27ba7494..6a86e3dd 100644 --- a/src/web/handlers.py +++ b/src/web/handlers.py @@ -20,7 +20,6 @@ from handlers_accounts import * from handlers_admin import * from handlers_base import * from handlers_boot import * -from handlers_download import * from handlers_fireinfo import * from handlers_iuse import * from handlers_mirrors import * diff --git a/src/web/handlers_download.py b/src/web/handlers_download.py deleted file mode 100644 index 11a09853..00000000 --- a/src/web/handlers_download.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/python - -import random -import tornado.web - -from handlers_base import * - -class DownloadFileHandler(BaseHandler): - def prepare(self): - self.set_header("Pragma", "no-cache") - - def head(self, filename): - self.redirect_to_mirror(filename) - - def get(self, filename): - self.redirect_to_mirror(filename, log_download=True) - - def find_mirror(self, filename): - exists = self.mirrors.file_exists(filename) - if not exists: - raise tornado.web.HTTPError(404, "File not found: %s" % filename) - - # Find mirrors located near to the user. - # If we have not found any, we use a random one. - remote_location = self.get_remote_location() - - if remote_location: - mirrors = self.mirrors.get_for_location(remote_location, filename=filename) - - if mirrors: - return random.choice(mirrors) - - return self.mirrors.get_random(filename=filename) - - def redirect_to_mirror(self, filename, log_download=False): - # Find a random mirror. - mirror = self.find_mirror(filename) - - # Construct the redirection URL. - download_url = mirror.build_url(filename) - - # Redirect the request. - self.redirect(download_url) - - if not log_download: - return - - remote_location = self.get_remote_location() - if remote_location: - country_code = remote_location.country - else: - country_code = None - - self.db.execute("INSERT INTO log_download(filename, mirror, country_code) \ - VALUES(%s, %s, %s)", filename, mirror.id, country_code) - - -class DownloadCompatHandler(BaseHandler): - def get(self, path, url): - for filename in self.mirrors.get_all_files(): - if not filename.endswith("/%s" % url): - continue - - self.redirect("/%s" % filename) - return - - raise tornado.web.HTTPError(404) -- 2.39.2