]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
Make downloaded files accessible via the HTTP server.
authorCameron Dale <camrdale@gmail.com>
Fri, 11 Jan 2008 00:40:23 +0000 (16:40 -0800)
committerCameron Dale <camrdale@gmail.com>
Fri, 11 Jan 2008 00:40:23 +0000 (16:40 -0800)
MirrorManager calls back to the main code, which adds an
appropriate ~ (tilde) directory to the web server.

apt_dht/HTTPServer.py
apt_dht/MirrorManager.py
apt_dht/apt_dht.py

index 181da4e4575f20a1ab66a6fba71f70dd62b71db2..ebb34f173900d8fd9c2ba12d47adbbccb9a57b8c 100644 (file)
@@ -47,8 +47,13 @@ class TopLevel(resource.Resource):
         self.subdirs = []
 
     def addDirectory(self, directory):
-        path = "~" + str(len(self.subdirs))
-        self.subdirs.append(directory)
+        assert directory
+        try:
+            idx = self.subdirs.index(directory)
+        except ValueError:
+            idx = len(self.subdirs)
+            self.subdirs.append(directory)
+        path = "~" + str(idx)
         return path
     
     def removeDirectory(self, directory):
index 7f7805a81eea6d068b51ea0869af54dfed170b39..dd1b163bf848b24693df4c05f2648f8c437cb469 100644 (file)
@@ -149,7 +149,8 @@ class ProxyFileStream(stream.SimpleStream):
 class MirrorManager:
     """Manages all requests for mirror objects."""
     
-    def __init__(self, cache_dir):
+    def __init__(self, manager, cache_dir):
+        self.manager = manager
         self.cache_dir = cache_dir
         self.cache = filepath.FilePath(self.cache_dir)
         self.apt_caches = {}
@@ -256,6 +257,8 @@ class MirrorManager:
             self.updatedFile(url, destFile.path)
             if ext:
                 self.updatedFile(url[:-len(ext)], decFile.path)
+                
+            self.manager.download_complete(hash, url, destFile.path)
         else:
             log.msg("Hashes don't match %s != %s: %s" % (hash.hexexpected(), hash.hexdigest(), url))
 
index 73cfcb57711bc70fc78d49acfdfa99e3fdfa643b..c3f1a768c5b2b85db6d90cab6c6927e2e78b5244 100644 (file)
@@ -1,5 +1,6 @@
 
 from binascii import b2a_hex
+import os.path
 
 from twisted.internet import defer
 from twisted.web2 import server, http, http_headers
@@ -18,7 +19,7 @@ class AptDHT:
         self.http_server = TopLevel(config.get('DEFAULT', 'cache_dir'), self)
         self.http_site = server.Site(self.http_server)
         self.peers = PeerManager()
-        self.mirrors = MirrorManager(config.get('DEFAULT', 'cache_dir'))
+        self.mirrors = MirrorManager(self, config.get('DEFAULT', 'cache_dir'))
     
     def getSite(self):
         return self.http_site
@@ -77,3 +78,16 @@ class AptDHT:
         getDefer.addCallback(self.mirrors.save_file, hash, path)
         getDefer.addErrback(self.mirrors.save_error, path)
         getDefer.addCallbacks(d.callback, d.errback)
+        
+    def download_complete(self, hash, url, file_path):
+        assert file_path.startswith(config.get('DEFAULT', 'cache_dir'))
+        directory = file_path[:len(config.get('DEFAULT', 'cache_dir'))]
+        url_path = file_path[len(config.get('DEFAULT', 'cache_dir')):]
+        if url_path[0] == '/':
+            url_path = url_path[1:]
+        top_directory = url_path.split('/',1)[0]
+        url_path = url_path[len(top_directory):]
+        http_dir = os.path.join(directory, top_directory)
+        new_top = self.http_server.addDirectory(http_dir)
+        url_path = '/' + new_top + url_path
+        log.msg('now avaliable at %s: %s' % (url_path, url))