]> git.ipfire.org Git - ipfire.org.git/blob - src/web/download.py
Move download handlers into new module
[ipfire.org.git] / src / web / download.py
1 #!/usr/bin/python
2
3 import random
4 import tornado.web
5
6 from . import handlers_base as base
7
8 from . import ui_modules
9
10 class IndexHandler(base.BaseHandler):
11 def get(self):
12 release = self.backend.releases.get_latest()
13 if not release:
14 raise tornado.web.HTTPError(404)
15
16 self.render("download/index.html", release=release)
17
18
19 class ReleaseHandler(base.BaseHandler):
20 def get(self, slug):
21 release = self.backend.releases.get_by_sname(slug)
22 if not release:
23 raise tornado.web.HTTPError(404)
24
25 self.render("download/release.html", release=release)
26
27
28 class FileHandler(base.BaseHandler):
29 def prepare(self):
30 self.set_header("Pragma", "no-cache")
31
32 def head(self, filename):
33 self.redirect_to_mirror(filename)
34
35 def get(self, filename):
36 self.redirect_to_mirror(filename, log_download=True)
37
38 def find_mirror(self, filename):
39 exists = self.mirrors.file_exists(filename)
40 if not exists:
41 raise tornado.web.HTTPError(404, "File not found: %s" % filename)
42
43 # Find mirrors located near to the user.
44 # If we have not found any, we use a random one.
45 remote_location = self.get_remote_location()
46
47 if remote_location:
48 mirrors = self.mirrors.get_for_location(remote_location, filename=filename)
49
50 if mirrors:
51 return random.choice(mirrors)
52
53 return self.mirrors.get_random(filename=filename)
54
55 def redirect_to_mirror(self, filename, log_download=False):
56 # Find a random mirror.
57 mirror = self.find_mirror(filename)
58
59 # Construct the redirection URL.
60 download_url = mirror.build_url(filename)
61
62 # Redirect the request.
63 self.redirect(download_url)
64
65 if not log_download:
66 return
67
68 remote_location = self.get_remote_location()
69 if remote_location:
70 country_code = remote_location.country
71 else:
72 country_code = None
73
74 self.db.execute("INSERT INTO log_download(filename, mirror, country_code) \
75 VALUES(%s, %s, %s)", filename, mirror.id, country_code)
76
77
78 class ButtonModule(ui_modules.UIModule):
79 def render(self, release):
80 for arch in release.arches:
81 for file in release.get_files_by_arch(arch):
82 return self.render_string("download/modules/button.html", release=release, file=file)
83
84 # return nothing
85 return ""