2015-08-20 17:27:15 +02:00
|
|
|
"""
|
|
|
|
Utilities for turning plain files into LBRY Files.
|
|
|
|
"""
|
|
|
|
|
2018-07-12 21:44:07 +02:00
|
|
|
import six
|
2015-08-20 17:27:15 +02:00
|
|
|
import binascii
|
|
|
|
import logging
|
|
|
|
import os
|
2018-02-12 20:03:39 +01:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
2015-08-20 17:27:15 +02:00
|
|
|
from twisted.protocols.basic import FileSender
|
|
|
|
|
2018-02-12 20:03:39 +01:00
|
|
|
from lbrynet.core.StreamDescriptor import BlobStreamDescriptorWriter, EncryptedFileStreamType
|
2018-02-22 01:09:10 +01:00
|
|
|
from lbrynet.core.StreamDescriptor import format_sd_info, get_stream_hash, validate_descriptor
|
2018-02-12 20:03:39 +01:00
|
|
|
from lbrynet.cryptstream.CryptStreamCreator import CryptStreamCreator
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileStreamCreator(CryptStreamCreator):
|
2015-08-20 17:27:15 +02:00
|
|
|
"""
|
2016-09-27 20:18:16 +02:00
|
|
|
A CryptStreamCreator which adds itself and its additional metadata to an EncryptedFileManager
|
2015-08-20 17:27:15 +02:00
|
|
|
"""
|
2018-02-12 20:03:39 +01:00
|
|
|
|
|
|
|
def __init__(self, blob_manager, lbry_file_manager, stream_name=None,
|
|
|
|
key=None, iv_generator=None):
|
2018-07-22 00:34:59 +02:00
|
|
|
super().__init__(blob_manager, stream_name, key, iv_generator)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
self.stream_hash = None
|
|
|
|
self.blob_infos = []
|
2018-02-12 20:03:39 +01:00
|
|
|
self.sd_info = None
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def _blob_finished(self, blob_info):
|
2017-09-07 18:00:55 +02:00
|
|
|
log.debug("length: %s", blob_info.length)
|
2018-02-12 20:03:39 +01:00
|
|
|
self.blob_infos.append(blob_info.get_dict())
|
2017-09-07 18:00:55 +02:00
|
|
|
return blob_info
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def _finished(self):
|
2018-02-12 20:03:39 +01:00
|
|
|
# calculate the stream hash
|
|
|
|
self.stream_hash = get_stream_hash(
|
|
|
|
hexlify(self.name), hexlify(self.key), hexlify(self.name),
|
|
|
|
self.blob_infos
|
|
|
|
)
|
2018-02-22 01:09:10 +01:00
|
|
|
|
2018-02-12 20:03:39 +01:00
|
|
|
# generate the sd info
|
|
|
|
self.sd_info = format_sd_info(
|
|
|
|
EncryptedFileStreamType, hexlify(self.name), hexlify(self.key),
|
2018-07-12 21:44:07 +02:00
|
|
|
hexlify(self.name), self.stream_hash.encode(), self.blob_infos
|
2018-02-12 20:03:39 +01:00
|
|
|
)
|
2018-02-22 01:09:10 +01:00
|
|
|
|
|
|
|
# sanity check
|
|
|
|
validate_descriptor(self.sd_info)
|
2018-02-12 20:03:39 +01:00
|
|
|
return defer.succeed(self.stream_hash)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2017-01-20 21:50:10 +01:00
|
|
|
# TODO: this should be run its own thread. Encrypting a large file can
|
|
|
|
# be very cpu intensive and there is no need to run that on the
|
|
|
|
# main reactor thread. The FileSender mechanism that is used is
|
|
|
|
# great when sending over the network, but this is all local so
|
|
|
|
# we can simply read the file from the disk without needing to
|
|
|
|
# involve reactor.
|
2018-02-12 20:03:39 +01:00
|
|
|
@defer.inlineCallbacks
|
2018-07-25 21:33:43 +02:00
|
|
|
def create_lbry_file(blob_manager, storage, payment_rate_manager, lbry_file_manager, file_name, file_handle,
|
|
|
|
key=None, iv_generator=None):
|
2016-11-04 17:39:52 +01:00
|
|
|
"""Turn a plain file into an LBRY File.
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
An LBRY File is a collection of encrypted blobs of data and the metadata that binds them
|
|
|
|
together which, when decrypted and put back together according to the metadata, results
|
|
|
|
in the original file.
|
|
|
|
|
|
|
|
The stream parameters that aren't specified are generated, the file is read and broken
|
|
|
|
into chunks and encrypted, and then a stream descriptor file with the stream parameters
|
|
|
|
and other metadata is written to disk.
|
|
|
|
|
2016-09-27 20:18:35 +02:00
|
|
|
@param session: An Session object.
|
|
|
|
@type session: Session
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
@param lbry_file_manager: The EncryptedFileManager object this LBRY File will be added to.
|
|
|
|
@type lbry_file_manager: EncryptedFileManager
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
@param file_name: The path to the plain file.
|
|
|
|
@type file_name: string
|
|
|
|
|
|
|
|
@param file_handle: The file-like object to read
|
|
|
|
@type file_handle: any file-like object which can be read by twisted.protocols.basic.FileSender
|
|
|
|
|
|
|
|
@param key: the raw AES key which will be used to encrypt the blobs. If None, a random key will
|
|
|
|
be generated.
|
|
|
|
@type key: string
|
|
|
|
|
2016-11-04 17:39:52 +01:00
|
|
|
@param iv_generator: a generator which yields initialization
|
|
|
|
vectors for the blobs. Will be called once for each blob.
|
2015-08-20 17:27:15 +02:00
|
|
|
@type iv_generator: a generator function which yields strings
|
|
|
|
|
|
|
|
@return: a Deferred which fires with the stream_hash of the LBRY File
|
|
|
|
@rtype: Deferred which fires with hex-encoded string
|
|
|
|
"""
|
|
|
|
|
|
|
|
base_file_name = os.path.basename(file_name)
|
2018-02-12 20:03:39 +01:00
|
|
|
file_directory = os.path.dirname(file_handle.name)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-11-04 17:39:52 +01:00
|
|
|
lbry_file_creator = EncryptedFileStreamCreator(
|
2018-07-25 21:33:43 +02:00
|
|
|
blob_manager, lbry_file_manager, base_file_name, key, iv_generator
|
2018-02-12 20:03:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
yield lbry_file_creator.setup()
|
|
|
|
# TODO: Using FileSender isn't necessary, we can just read
|
|
|
|
# straight from the disk. The stream creation process
|
|
|
|
# should be in its own thread anyway so we don't need to
|
|
|
|
# worry about interacting with the twisted reactor
|
|
|
|
file_sender = FileSender()
|
|
|
|
yield file_sender.beginFileTransfer(file_handle, lbry_file_creator)
|
|
|
|
|
|
|
|
log.debug("the file sender has triggered its deferred. stopping the stream writer")
|
|
|
|
yield lbry_file_creator.stop()
|
|
|
|
|
|
|
|
log.debug("making the sd blob")
|
|
|
|
sd_info = lbry_file_creator.sd_info
|
2018-07-25 21:33:43 +02:00
|
|
|
descriptor_writer = BlobStreamDescriptorWriter(blob_manager)
|
2018-02-12 20:03:39 +01:00
|
|
|
sd_hash = yield descriptor_writer.create_descriptor(sd_info)
|
|
|
|
|
|
|
|
log.debug("saving the stream")
|
2018-07-25 21:33:43 +02:00
|
|
|
yield storage.store_stream(
|
2018-02-12 20:03:39 +01:00
|
|
|
sd_info['stream_hash'], sd_hash, sd_info['stream_name'], sd_info['key'],
|
|
|
|
sd_info['suggested_file_name'], sd_info['blobs']
|
|
|
|
)
|
|
|
|
log.debug("adding to the file manager")
|
|
|
|
lbry_file = yield lbry_file_manager.add_published_file(
|
2018-07-12 21:44:07 +02:00
|
|
|
sd_info['stream_hash'], sd_hash, binascii.hexlify(file_directory.encode()), payment_rate_manager,
|
2018-07-25 21:33:43 +02:00
|
|
|
payment_rate_manager.min_blob_data_payment_rate
|
2018-02-12 20:03:39 +01:00
|
|
|
)
|
|
|
|
defer.returnValue(lbry_file)
|
2016-11-04 17:34:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def hexlify(str_or_unicode):
|
2018-07-12 21:44:07 +02:00
|
|
|
if isinstance(str_or_unicode, six.text_type):
|
2016-11-04 17:34:42 +01:00
|
|
|
strng = str_or_unicode.encode('utf-8')
|
|
|
|
else:
|
|
|
|
strng = str_or_unicode
|
|
|
|
return binascii.hexlify(strng)
|