def download(self): if not is_valid_blobhash(self.blob_hash): return defer.fail(Failure(InvalidBlobHashError(self.blob_hash))) def cancel_download(d): self.stop() self.finished_deferred = defer.Deferred(canceller=cancel_download) self.download_manager = DownloadManager(self.blob_manager) self.download_manager.blob_requester = BlobRequester( self.blob_manager, self.peer_finder, self.payment_rate_manager, self.wallet, self.download_manager) self.download_manager.blob_info_finder = SingleBlobMetadataHandler( self.blob_hash, self.download_manager) self.download_manager.progress_manager = SingleProgressManager( self.download_manager, self._blob_downloaded, self._download_timedout, self.timeout) self.download_manager.blob_handler = DummyBlobHandler() self.download_manager.wallet_info_exchanger = self.wallet.get_info_exchanger( ) self.download_manager.connection_manager = ConnectionManager( self, self.rate_limiter, [self.download_manager.blob_requester], [self.download_manager.wallet_info_exchanger]) d = self.download_manager.start_downloading() d.addCallback(lambda _: self.finished_deferred) return d
def get_files_in_blob_dir() -> typing.Set[str]: if not self.blob_dir: return set() return { item.name for item in os.scandir(self.blob_dir) if is_valid_blobhash(item.name) }
def is_blob_verified(self, blob_hash: str, length: typing.Optional[int] = None) -> bool: if not is_valid_blobhash(blob_hash): raise ValueError(blob_hash) if blob_hash in self.blobs: return self.blobs[blob_hash].get_is_verified() if not os.path.isfile(os.path.join(self.blob_dir, blob_hash)): return False return self._get_blob(blob_hash, length).get_is_verified()
def _get_blob(self, blob_hash: str, length: typing.Optional[int] = None): if self.config.save_blobs: return BlobFile(self.loop, blob_hash, length, self.blob_completed, self.blob_dir) else: if is_valid_blobhash(blob_hash) and os.path.isfile( os.path.join(self.blob_dir, blob_hash)): return BlobFile(self.loop, blob_hash, length, self.blob_completed, self.blob_dir) return BlobBuffer(self.loop, blob_hash, length, self.blob_completed, self.blob_dir)
def delete_blob(self, blob_hash: str): if not is_valid_blobhash(blob_hash): raise Exception("invalid blob hash to delete") if blob_hash not in self.blobs: if os.path.isfile(os.path.join(self.blob_dir, blob_hash)): os.remove(os.path.join(self.blob_dir, blob_hash)) else: self.blobs.pop(blob_hash).delete() if blob_hash in self.completed_blob_hashes: self.completed_blob_hashes.remove(blob_hash)
def initialize_blob_hashes(): self.completed_blob_hashes.update( item.name for item in os.scandir(self.blob_dir) if is_valid_blobhash(item.name))
def is_blob_request(self, request_dict): if BLOB_HASH not in request_dict or BLOB_SIZE not in request_dict: return False if not is_valid_blobhash(request_dict[BLOB_HASH]): raise InvalidBlobHashError(request_dict[BLOB_HASH]) return True
def is_descriptor_request(self, request_dict): if SD_BLOB_HASH not in request_dict or SD_BLOB_SIZE not in request_dict: return False if not is_valid_blobhash(request_dict[SD_BLOB_HASH]): raise InvalidBlobHashError(request_dict[SD_BLOB_HASH]) return True