]> git.ipfire.org Git - ipfire.org.git/blob - src/web/handlers_download.py
Remove old download pages
[ipfire.org.git] / src / web / handlers_download.py
1 #!/usr/bin/python
2
3 import random
4 import tornado.web
5
6 from handlers_base import *
7
8 class DownloadFileHandler(BaseHandler):
9 def prepare(self):
10 self.set_header("Pragma", "no-cache")
11
12 def head(self, filename):
13 self.redirect_to_mirror(filename)
14
15 def get(self, filename):
16 self.redirect_to_mirror(filename, log_download=True)
17
18 def find_mirror(self, filename):
19 exists = self.mirrors.file_exists(filename)
20 if not exists:
21 raise tornado.web.HTTPError(404, "File not found: %s" % filename)
22
23 # Find mirrors located near to the user.
24 # If we have not found any, we use a random one.
25 remote_location = self.get_remote_location()
26
27 if remote_location:
28 mirrors = self.mirrors.get_for_location(remote_location, filename=filename)
29
30 if mirrors:
31 return random.choice(mirrors)
32
33 return self.mirrors.get_random(filename=filename)
34
35 def redirect_to_mirror(self, filename, log_download=False):
36 # Find a random mirror.
37 mirror = self.find_mirror(filename)
38
39 # Construct the redirection URL.
40 download_url = mirror.build_url(filename)
41
42 # Redirect the request.
43 self.redirect(download_url)
44
45 if not log_download:
46 return
47
48 remote_location = self.get_remote_location()
49 if remote_location:
50 country_code = remote_location.country
51 else:
52 country_code = None
53
54 self.db.execute("INSERT INTO log_download(filename, mirror, country_code) \
55 VALUES(%s, %s, %s)", filename, mirror.id, country_code)
56
57
58 class DownloadCompatHandler(BaseHandler):
59 def get(self, path, url):
60 for filename in self.mirrors.get_all_files():
61 if not filename.endswith("/%s" % url):
62 continue
63
64 self.redirect("/%s" % filename)
65 return
66
67 raise tornado.web.HTTPError(404)