- def download_file(self, locations, hash, size, path, d):
- getDefer = self.peers.get(locations)
- getDefer.addCallback(self.mirrors.save_file, hash, size, path)
- getDefer.addErrback(self.mirrors.save_error, path)
- getDefer.addCallbacks(d.callback, d.errback)
+ def check_response(self, response, hash, path):
+ if response.code < 200 or response.code >= 300:
+ log.msg('Download from peers failed, going to direct download: %s' % path)
+ getDefer = self.peers.get(hash, path)
+ return getDefer
+ return response
+
+ def new_cached_file(self, file_path, hash, new_hash, url = None, forceDHT = False):
+ """Add a newly cached file to the appropriate places.
+
+ If the file was downloaded, set url to the path it was downloaded for.
+ Doesn't add a file to the DHT unless a hash was found for it
+ (but does add it anyway if forceDHT is True).
+ """
+ if url:
+ self.mirrors.updatedFile(url, file_path)
+
+ if self.my_contact and hash and new_hash and (hash.expected() is not None or forceDHT):
+ return self.store(hash)
+ return None
+
+ def store(self, hash):
+ """Add a file to the DHT."""
+ key = hash.norm(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
+ value = {'c': self.my_contact}
+ pieces = hash.pieceDigests()
+ if len(pieces) <= 1:
+ pass
+ elif len(pieces) <= DHT_PIECES:
+ value['t'] = {'t': ''.join(pieces)}
+ elif len(pieces) <= TORRENT_PIECES:
+ s = sha.new().update(''.join(pieces))
+ value['h'] = s.digest()
+ else:
+ s = sha.new().update(''.join(pieces))
+ value['l'] = s.digest()
+ storeDefer = self.dht.storeValue(key, value)
+ storeDefer.addCallback(self.store_done, hash)
+ return storeDefer
+
+ def store_done(self, result, hash):
+ log.msg('Added %s to the DHT: %r' % (hash.hexdigest(), result))
+ pieces = hash.pieceDigests()
+ if len(pieces) > DHT_PIECES and len(pieces) <= TORRENT_PIECES:
+ s = sha.new().update(''.join(pieces))
+ key = s.digest()
+ value = {'t': ''.join(pieces)}
+ storeDefer = self.dht.storeValue(key, value)
+ storeDefer.addCallback(self.store_torrent_done, key)
+ return storeDefer
+ return result
+
+ def store_torrent_done(self, result, key):
+ log.msg('Added torrent string %s to the DHT: %r' % (b2ahex(key), result))
+ return result
+
\ No newline at end of file