]> git.ipfire.org Git - people/shoehn/ipfire.org.git/commitdiff
Add source download handler.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jan 2010 15:06:07 +0000 (16:06 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jan 2010 15:06:07 +0000 (16:06 +0100)
www/webapp/__init__.py
www/webapp/handlers.py

index 73be8c4c54951d30362b23b8a5969e2037ed1659..029b28c5bd3139f38fbbc039ca3aab2e3b15522a 100644 (file)
@@ -78,6 +78,7 @@ class Application(tornado.web.Application):
                        (r"/", MainHandler),
                        (r"/[A-Za-z]{2}/?", MainHandler),
                        (r"/[A-Za-z]{2}/index", SourceHandler),
+                       (r"(/source.*)", SourceDownloadHandler),
                ] + static_handlers)
 
                # torrent.ipfire.org
index 0985212031b4ffe046cb830fb3dcc5a088a76a46..e7e661b7925b9903261e8cb044b0474b4642dcc7 100644 (file)
@@ -1,9 +1,12 @@
 #!/usr/bin/python
 
+import datetime
 import httplib
+import mimetypes
 import operator
 import os
 import simplejson
+import stat
 import sqlite3
 import time
 import urlparse
@@ -241,3 +244,49 @@ class SourceHandler(BaseHandler):
                fileobjects.sort(key=operator.itemgetter("name"))
 
                self.render("sources.html", files=fileobjects)
+
+
+class SourceDownloadHandler(BaseHandler):
+       def prepare(self):
+               if not hasattr(self, "db"):
+                       self.db = sqlite3.connect("/srv/www/ipfire.org/source/hashes.db")
+                       c = self.db.cursor()
+                       c.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)")
+                       c.close()
+
+       def head(self, path):
+               self.get(path, include_body=False)
+
+       def get(self, path, include_body=True):
+               source_path = "/srv/sources"
+
+               path = os.path.abspath(os.path.join(source_path, path[1:]))
+
+               if not path.startswith(source_path):
+                       raise tornado.web.HTTPError(403)
+               if not os.path.exists(path):
+                       raise tornado.web.HTTPError(404)
+
+               stat_result = os.stat(path)
+               modified = datetime.datetime.fromtimestamp(stat_result[stat.ST_MTIME])
+
+               self.set_header("Last-Modified", modified)
+               self.set_header("Content-Length", stat_result[stat.ST_SIZE])
+
+               mime_type, encoding = mimetypes.guess_type(path)
+               if mime_type:
+                       self.set_header("Content-Type", mime_type)
+
+               c = self.db.cursor()
+               c.execute("SELECT sha1 FROM hashes WHERE file = '%s'" % os.path.basename(path))
+               hash = c.fetchone()
+               if hash:
+                       self.set_header("X-Hash-Sha1", "%s" % hash)
+
+               if not include_body:
+                       return
+               file = open(path, "r")
+               try:
+                       self.write(file.read())
+               finally:
+                       file.close()