2015-08-20 17:27:15 +02:00
|
|
|
import logging
|
|
|
|
from zope.interface import implements
|
2017-09-07 21:25:41 +02:00
|
|
|
from twisted.internet import defer
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.interfaces import IMetadataHandler
|
|
|
|
|
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileMetadataHandler(object):
|
2015-08-20 17:27:15 +02:00
|
|
|
implements(IMetadataHandler)
|
|
|
|
|
2018-02-12 20:03:39 +01:00
|
|
|
def __init__(self, stream_hash, storage, download_manager):
|
2015-08-20 17:27:15 +02:00
|
|
|
self.stream_hash = stream_hash
|
2018-02-12 20:03:39 +01:00
|
|
|
self.storage = storage
|
2015-08-20 17:27:15 +02:00
|
|
|
self.download_manager = download_manager
|
|
|
|
self._final_blob_num = None
|
|
|
|
|
|
|
|
######### IMetadataHandler #########
|
|
|
|
|
2017-09-07 21:25:41 +02:00
|
|
|
@defer.inlineCallbacks
|
2015-08-20 17:27:15 +02:00
|
|
|
def get_initial_blobs(self):
|
2018-02-12 20:03:39 +01:00
|
|
|
blob_infos = yield self.storage.get_blobs_for_stream(self.stream_hash)
|
2017-09-07 21:25:41 +02:00
|
|
|
formatted_infos = self._format_initial_blobs_for_download_manager(blob_infos)
|
|
|
|
defer.returnValue(formatted_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 = []
|
2018-02-12 20:03:39 +01:00
|
|
|
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:
|
2017-09-07 21:25:41 +02:00
|
|
|
if i != len(blob_infos) - 1:
|
2018-02-12 20:03:39 +01:00
|
|
|
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
|
2016-12-14 00:16:12 +01:00
|
|
|
return infos
|