2015-08-20 17:27:15 +02:00
|
|
|
import binascii
|
|
|
|
|
|
|
|
from zope.interface import implements
|
|
|
|
|
2017-06-26 03:03:37 +02:00
|
|
|
from lbrynet.lbry_file.StreamDescriptor import save_sd_info
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.cryptstream.client.CryptStreamDownloader import CryptStreamDownloader
|
|
|
|
from lbrynet.core.client.StreamProgressManager import FullStreamProgressManager
|
2015-09-22 04:06:04 +02:00
|
|
|
from lbrynet.core.StreamDescriptor import StreamMetadata
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.interfaces import IStreamDownloaderFactory
|
2017-06-26 03:03:37 +02:00
|
|
|
from lbrynet.lbry_file.client.EncryptedFileMetadataHandler import EncryptedFileMetadataHandler
|
2015-08-20 17:27:15 +02:00
|
|
|
import os
|
2017-09-14 21:23:45 +02:00
|
|
|
from twisted.internet import defer, threads
|
2016-01-06 19:08:20 +01:00
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileDownloader(CryptStreamDownloader):
|
2015-08-20 17:27:15 +02:00
|
|
|
"""Classes which inherit from this class download LBRY files"""
|
|
|
|
|
|
|
|
def __init__(self, stream_hash, peer_finder, rate_limiter, blob_manager,
|
2017-12-29 20:09:54 +01:00
|
|
|
stream_info_manager, payment_rate_manager, wallet, key, stream_name,
|
|
|
|
suggested_file_name=None):
|
2015-08-20 17:27:15 +02:00
|
|
|
CryptStreamDownloader.__init__(self, peer_finder, rate_limiter, blob_manager,
|
2017-12-29 20:09:54 +01:00
|
|
|
payment_rate_manager, wallet, key, stream_name)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.stream_hash = stream_hash
|
|
|
|
self.stream_info_manager = stream_info_manager
|
2018-01-05 03:28:09 +01:00
|
|
|
self.suggested_file_name = binascii.unhexlify(suggested_file_name)
|
2015-08-20 17:27:15 +02:00
|
|
|
self._calculated_total_bytes = None
|
|
|
|
|
2016-01-16 07:16:37 +01:00
|
|
|
def delete_data(self):
|
|
|
|
d1 = self.stream_info_manager.get_blobs_for_stream(self.stream_hash)
|
|
|
|
|
|
|
|
def get_blob_hashes(blob_infos):
|
|
|
|
return [b[0] for b in blob_infos if b[0] is not None]
|
|
|
|
|
|
|
|
d1.addCallback(get_blob_hashes)
|
|
|
|
d2 = self.stream_info_manager.get_sd_blob_hashes_for_stream(self.stream_hash)
|
|
|
|
|
|
|
|
def combine_blob_hashes(results):
|
|
|
|
blob_hashes = []
|
|
|
|
for success, result in results:
|
|
|
|
if success is True:
|
|
|
|
blob_hashes.extend(result)
|
|
|
|
return blob_hashes
|
|
|
|
|
|
|
|
def delete_blobs(blob_hashes):
|
|
|
|
self.blob_manager.delete_blobs(blob_hashes)
|
|
|
|
return True
|
|
|
|
|
|
|
|
dl = defer.DeferredList([d1, d2], fireOnOneErrback=True)
|
|
|
|
dl.addCallback(combine_blob_hashes)
|
|
|
|
dl.addCallback(delete_blobs)
|
|
|
|
return dl
|
|
|
|
|
2015-09-17 07:43:41 +02:00
|
|
|
def stop(self, err=None):
|
2015-08-20 17:27:15 +02:00
|
|
|
d = self._close_output()
|
2015-09-17 07:43:41 +02:00
|
|
|
d.addCallback(lambda _: CryptStreamDownloader.stop(self, err=err))
|
2015-08-20 17:27:15 +02:00
|
|
|
return d
|
|
|
|
|
|
|
|
def _get_progress_manager(self, download_manager):
|
2016-11-30 21:20:45 +01:00
|
|
|
return FullStreamProgressManager(self._finished_downloading,
|
|
|
|
self.blob_manager, download_manager)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def _start(self):
|
|
|
|
d = self._setup_output()
|
|
|
|
d.addCallback(lambda _: CryptStreamDownloader._start(self))
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _setup_output(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _close_output(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_total_bytes(self):
|
2015-10-23 20:51:26 +02:00
|
|
|
d = self.stream_info_manager.get_blobs_for_stream(self.stream_hash)
|
|
|
|
|
|
|
|
def calculate_size(blobs):
|
|
|
|
return sum([b[3] for b in blobs])
|
|
|
|
|
|
|
|
d.addCallback(calculate_size)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def get_total_bytes_cached(self):
|
2015-08-20 17:27:15 +02:00
|
|
|
if self._calculated_total_bytes is None or self._calculated_total_bytes == 0:
|
|
|
|
if self.download_manager is None:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
self._calculated_total_bytes = self.download_manager.calculate_total_bytes()
|
|
|
|
return self._calculated_total_bytes
|
|
|
|
|
|
|
|
def get_bytes_left_to_output(self):
|
|
|
|
if self.download_manager is not None:
|
|
|
|
return self.download_manager.calculate_bytes_left_to_output()
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def get_bytes_left_to_download(self):
|
|
|
|
if self.download_manager is not None:
|
|
|
|
return self.download_manager.calculate_bytes_left_to_download()
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def _get_metadata_handler(self, download_manager):
|
2016-11-30 21:20:45 +01:00
|
|
|
return EncryptedFileMetadataHandler(self.stream_hash,
|
|
|
|
self.stream_info_manager, download_manager)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileDownloaderFactory(object):
|
2015-08-20 17:27:15 +02:00
|
|
|
implements(IStreamDownloaderFactory)
|
|
|
|
|
|
|
|
def __init__(self, peer_finder, rate_limiter, blob_manager, stream_info_manager,
|
|
|
|
wallet):
|
|
|
|
self.peer_finder = peer_finder
|
|
|
|
self.rate_limiter = rate_limiter
|
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.stream_info_manager = stream_info_manager
|
|
|
|
self.wallet = wallet
|
|
|
|
|
2015-08-27 21:41:17 +02:00
|
|
|
def can_download(self, sd_validator):
|
|
|
|
return True
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-14 20:18:42 +01:00
|
|
|
def make_downloader(self, metadata, options, payment_rate_manager, **kwargs):
|
|
|
|
assert len(options) == 1
|
|
|
|
data_rate = options[0]
|
2017-02-14 18:32:04 +01:00
|
|
|
payment_rate_manager.min_blob_data_payment_rate = data_rate
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-09-22 04:06:04 +02:00
|
|
|
def save_source_if_blob(stream_hash):
|
|
|
|
if metadata.metadata_source == StreamMetadata.FROM_BLOB:
|
2016-11-30 21:20:45 +01:00
|
|
|
d = self.stream_info_manager.save_sd_blob_hash_to_stream(
|
|
|
|
stream_hash, metadata.source_blob_hash)
|
2015-09-22 04:06:04 +02:00
|
|
|
else:
|
|
|
|
d = defer.succeed(True)
|
|
|
|
d.addCallback(lambda _: stream_hash)
|
|
|
|
return d
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def create_downloader(stream_hash):
|
2015-09-22 04:06:04 +02:00
|
|
|
downloader = self._make_downloader(stream_hash, payment_rate_manager,
|
2016-12-20 03:16:37 +01:00
|
|
|
metadata.validator.raw_info)
|
2015-08-20 17:27:15 +02:00
|
|
|
d = downloader.set_stream_info()
|
|
|
|
d.addCallback(lambda _: downloader)
|
|
|
|
return d
|
|
|
|
|
2015-09-22 04:06:04 +02:00
|
|
|
d = save_sd_info(self.stream_info_manager, metadata.validator.raw_info)
|
|
|
|
d.addCallback(save_source_if_blob)
|
2015-08-20 17:27:15 +02:00
|
|
|
d.addCallback(create_downloader)
|
|
|
|
return d
|
|
|
|
|
2016-12-20 03:16:37 +01:00
|
|
|
def _make_downloader(self, stream_hash, payment_rate_manager, stream_info):
|
2015-08-20 17:27:15 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileSaver(EncryptedFileDownloader):
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self, stream_hash, peer_finder, rate_limiter, blob_manager, stream_info_manager,
|
2017-12-29 20:09:54 +01:00
|
|
|
payment_rate_manager, wallet, download_directory, key, stream_name,
|
2018-01-05 03:28:09 +01:00
|
|
|
suggested_file_name):
|
2017-12-29 20:09:54 +01:00
|
|
|
EncryptedFileDownloader.__init__(self, stream_hash, peer_finder, rate_limiter,
|
|
|
|
blob_manager, stream_info_manager, payment_rate_manager,
|
|
|
|
wallet, key, stream_name, suggested_file_name)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.download_directory = download_directory
|
2018-01-05 03:28:09 +01:00
|
|
|
self.file_name = os.path.basename(self.suggested_file_name)
|
2016-01-23 00:16:00 +01:00
|
|
|
self.file_written_to = None
|
2015-08-20 17:27:15 +02:00
|
|
|
self.file_handle = None
|
|
|
|
|
2016-01-23 00:16:00 +01:00
|
|
|
def __str__(self):
|
|
|
|
if self.file_written_to is not None:
|
|
|
|
return str(self.file_written_to)
|
|
|
|
else:
|
|
|
|
return str(self.file_name)
|
|
|
|
|
2015-09-17 07:43:41 +02:00
|
|
|
def stop(self, err=None):
|
2016-09-27 20:18:16 +02:00
|
|
|
d = EncryptedFileDownloader.stop(self, err=err)
|
2015-08-20 17:27:15 +02:00
|
|
|
d.addCallback(lambda _: self._delete_from_info_manager())
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _get_progress_manager(self, download_manager):
|
2016-11-30 21:20:45 +01:00
|
|
|
return FullStreamProgressManager(self._finished_downloading,
|
|
|
|
self.blob_manager,
|
2016-12-20 03:16:37 +01:00
|
|
|
download_manager)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def _setup_output(self):
|
|
|
|
def open_file():
|
|
|
|
if self.file_handle is None:
|
|
|
|
file_name = self.file_name
|
|
|
|
if not file_name:
|
|
|
|
file_name = "_"
|
|
|
|
if os.path.exists(os.path.join(self.download_directory, file_name)):
|
|
|
|
ext_num = 1
|
2016-04-22 08:45:41 +02:00
|
|
|
|
|
|
|
def _get_file_name(ext):
|
|
|
|
if len(file_name.split(".")):
|
|
|
|
fn = ''.join(file_name.split(".")[:-1])
|
|
|
|
file_ext = ''.join(file_name.split(".")[-1])
|
|
|
|
return fn + "-" + str(ext) + "." + file_ext
|
|
|
|
else:
|
|
|
|
return file_name + "_" + str(ext)
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
while os.path.exists(os.path.join(self.download_directory,
|
2016-04-22 08:45:41 +02:00
|
|
|
_get_file_name(ext_num))):
|
2015-08-20 17:27:15 +02:00
|
|
|
ext_num += 1
|
2016-04-22 08:45:41 +02:00
|
|
|
|
|
|
|
file_name = _get_file_name(ext_num)
|
2016-01-06 19:08:20 +01:00
|
|
|
try:
|
|
|
|
self.file_handle = open(os.path.join(self.download_directory, file_name), 'wb')
|
2016-01-23 00:16:00 +01:00
|
|
|
self.file_written_to = os.path.join(self.download_directory, file_name)
|
2016-01-06 19:08:20 +01:00
|
|
|
except IOError:
|
|
|
|
log.error(traceback.format_exc())
|
2016-11-30 21:20:45 +01:00
|
|
|
raise ValueError(
|
|
|
|
"Failed to open %s. Make sure you have permission to save files to that"
|
|
|
|
" location." %
|
|
|
|
os.path.join(self.download_directory, file_name))
|
2015-08-20 17:27:15 +02:00
|
|
|
return threads.deferToThread(open_file)
|
|
|
|
|
|
|
|
def _close_output(self):
|
|
|
|
self.file_handle, file_handle = None, self.file_handle
|
|
|
|
|
|
|
|
def close_file():
|
|
|
|
if file_handle is not None:
|
|
|
|
name = file_handle.name
|
|
|
|
file_handle.close()
|
|
|
|
if self.completed is False:
|
|
|
|
os.remove(name)
|
|
|
|
|
|
|
|
return threads.deferToThread(close_file)
|
|
|
|
|
|
|
|
def _get_write_func(self):
|
|
|
|
def write_func(data):
|
|
|
|
if self.stopped is False and self.file_handle is not None:
|
|
|
|
self.file_handle.write(data)
|
|
|
|
return write_func
|
|
|
|
|
|
|
|
def _delete_from_info_manager(self):
|
|
|
|
return self.stream_info_manager.delete_stream(self.stream_hash)
|
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileSaverFactory(EncryptedFileDownloaderFactory):
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self, peer_finder, rate_limiter, blob_manager, stream_info_manager,
|
|
|
|
wallet, download_directory):
|
2016-09-27 20:18:16 +02:00
|
|
|
EncryptedFileDownloaderFactory.__init__(self, peer_finder, rate_limiter, blob_manager,
|
2015-08-20 17:27:15 +02:00
|
|
|
stream_info_manager, wallet)
|
|
|
|
self.download_directory = download_directory
|
|
|
|
|
2016-12-20 03:16:37 +01:00
|
|
|
def _make_downloader(self, stream_hash, payment_rate_manager, stream_info):
|
2018-01-05 03:28:09 +01:00
|
|
|
stream_name = stream_info.raw_info['stream_name']
|
2017-12-29 20:09:54 +01:00
|
|
|
key = stream_info.raw_info['key']
|
2018-01-05 03:28:09 +01:00
|
|
|
suggested_file_name = stream_info.raw_info['suggested_file_name']
|
2017-12-29 20:09:54 +01:00
|
|
|
return EncryptedFileSaver(stream_hash, self.peer_finder, self.rate_limiter,
|
|
|
|
self.blob_manager, self.stream_info_manager,
|
2018-01-05 03:28:09 +01:00
|
|
|
payment_rate_manager, self.wallet, self.download_directory,
|
|
|
|
key=key, stream_name=stream_name,
|
|
|
|
suggested_file_name=suggested_file_name)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-09-22 04:06:04 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_description():
|
2015-08-20 17:27:15 +02:00
|
|
|
return "Save"
|