lbry-sdk/lbrynet/blob/client/EncryptedFileMetadataHandler.py

41 lines
1.3 KiB
Python
Raw Normal View History

2015-08-20 17:27:15 +02:00
import logging
from twisted.internet import defer
from lbrynet.extras.compat import f2d
2015-08-20 17:27:15 +02:00
log = logging.getLogger(__name__)
class EncryptedFileMetadataHandler:
2015-08-20 17:27:15 +02:00
def __init__(self, stream_hash, storage, download_manager):
2015-08-20 17:27:15 +02:00
self.stream_hash = stream_hash
self.storage = storage
2015-08-20 17:27:15 +02:00
self.download_manager = download_manager
self._final_blob_num = None
######### IMetadataHandler #########
@defer.inlineCallbacks
2015-08-20 17:27:15 +02:00
def get_initial_blobs(self):
blob_infos = yield f2d(self.storage.get_blobs_for_stream(self.stream_hash))
2019-01-07 20:27:21 +01:00
return self._format_initial_blobs_for_download_manager(blob_infos)
2015-08-20 17:27:15 +02:00
def final_blob_num(self):
return self._final_blob_num
######### internal calls #########
def _format_initial_blobs_for_download_manager(self, blob_infos):
infos = []
for i, crypt_blob in enumerate(blob_infos):
if crypt_blob.blob_hash is not None and crypt_blob.length:
infos.append(crypt_blob)
2015-08-20 17:27:15 +02:00
else:
if i != len(blob_infos) - 1:
raise Exception("Invalid stream terminator: %i of %i" %
(i, len(blob_infos) - 1))
log.debug("Setting _final_blob_num to %s", str(crypt_blob.blob_num - 1))
self._final_blob_num = crypt_blob.blob_num - 1
return infos