2015-08-20 17:27:15 +02:00
|
|
|
"""
|
|
|
|
Keep track of which LBRY Files are downloading and store their LBRY File specific metadata
|
|
|
|
"""
|
2016-03-08 18:15:49 +01:00
|
|
|
import os
|
2018-02-12 20:03:39 +01:00
|
|
|
import logging
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-03-08 18:15:49 +01:00
|
|
|
from twisted.internet import defer, task, reactor
|
|
|
|
from twisted.python.failure import Failure
|
2018-03-19 18:36:53 +01:00
|
|
|
from lbrynet.reflector.reupload import reflect_file
|
2017-06-26 03:04:04 +02:00
|
|
|
from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader
|
|
|
|
from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloaderFactory
|
2018-05-16 02:44:30 +02:00
|
|
|
from lbrynet.core.StreamDescriptor import EncryptedFileStreamType, get_sd_info
|
2016-11-30 21:20:45 +01:00
|
|
|
from lbrynet.cryptstream.client.CryptStreamDownloader import AlreadyStoppedError
|
|
|
|
from lbrynet.cryptstream.client.CryptStreamDownloader import CurrentlyStoppingError
|
2017-08-04 05:13:59 +02:00
|
|
|
from lbrynet.core.utils import safe_start_looping_call, safe_stop_looping_call
|
2017-03-17 18:07:18 +01:00
|
|
|
from lbrynet import conf
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class EncryptedFileManager:
|
2017-10-02 20:52:04 +02:00
|
|
|
"""
|
|
|
|
Keeps track of currently opened LBRY Files, their options, and
|
2016-11-30 21:20:45 +01:00
|
|
|
their LBRY File specific metadata.
|
2015-08-20 17:27:15 +02:00
|
|
|
"""
|
2017-10-02 20:52:04 +02:00
|
|
|
# when reflecting files, reflect up to this many files at a time
|
|
|
|
CONCURRENT_REFLECTS = 5
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2018-07-25 21:33:43 +02:00
|
|
|
def __init__(self, peer_finder, rate_limiter, blob_manager, wallet, payment_rate_manager, storage, sd_identifier):
|
2018-05-08 19:51:02 +02:00
|
|
|
self.auto_re_reflect = conf.settings['reflect_uploads'] and conf.settings['auto_re_reflect_interval'] > 0
|
2017-08-04 05:13:59 +02:00
|
|
|
self.auto_re_reflect_interval = conf.settings['auto_re_reflect_interval']
|
2018-07-25 21:33:43 +02:00
|
|
|
self.peer_finder = peer_finder
|
|
|
|
self.rate_limiter = rate_limiter
|
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.wallet = wallet
|
|
|
|
self.payment_rate_manager = payment_rate_manager
|
|
|
|
self.storage = storage
|
2017-01-20 21:50:10 +01:00
|
|
|
# TODO: why is sd_identifier part of the file manager?
|
2015-08-20 17:27:15 +02:00
|
|
|
self.sd_identifier = sd_identifier
|
|
|
|
self.lbry_files = []
|
2017-02-10 16:56:22 +01:00
|
|
|
self.lbry_file_reflector = task.LoopingCall(self.reflect_lbry_files)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-09 01:03:35 +01:00
|
|
|
@defer.inlineCallbacks
|
2018-05-16 02:44:30 +02:00
|
|
|
def setup(self):
|
2017-02-09 01:03:35 +01:00
|
|
|
yield self._add_to_sd_identifier()
|
2018-05-16 02:44:30 +02:00
|
|
|
yield self._start_lbry_files()
|
2018-03-12 15:44:29 +01:00
|
|
|
log.info("Started file manager")
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-01-16 07:16:37 +01:00
|
|
|
def get_lbry_file_status(self, lbry_file):
|
2018-07-25 21:33:43 +02:00
|
|
|
return self.storage.get_lbry_file_status(lbry_file.rowid)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-01-16 07:16:37 +01:00
|
|
|
def set_lbry_file_data_payment_rate(self, lbry_file, new_rate):
|
2018-07-25 21:33:43 +02:00
|
|
|
return self.storage(lbry_file.rowid, new_rate)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-01-16 07:16:37 +01:00
|
|
|
def change_lbry_file_status(self, lbry_file, status):
|
|
|
|
log.debug("Changing status of %s to %s", lbry_file.stream_hash, status)
|
2018-07-25 21:33:43 +02:00
|
|
|
return self.storage.change_file_status(lbry_file.rowid, status)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def get_lbry_file_status_reports(self):
|
|
|
|
ds = []
|
|
|
|
|
|
|
|
for lbry_file in self.lbry_files:
|
|
|
|
ds.append(lbry_file.status())
|
|
|
|
|
|
|
|
dl = defer.DeferredList(ds)
|
|
|
|
|
|
|
|
def filter_failures(status_reports):
|
|
|
|
return [status_report for success, status_report in status_reports if success is True]
|
|
|
|
|
|
|
|
dl.addCallback(filter_failures)
|
|
|
|
return dl
|
|
|
|
|
|
|
|
def _add_to_sd_identifier(self):
|
2018-08-02 20:33:03 +02:00
|
|
|
downloader_factory = ManagedEncryptedFileDownloaderFactory(self, self.blob_manager)
|
2016-11-30 21:20:45 +01:00
|
|
|
self.sd_identifier.add_stream_downloader_factory(
|
|
|
|
EncryptedFileStreamType, downloader_factory)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2018-01-05 03:28:09 +01:00
|
|
|
def _get_lbry_file(self, rowid, stream_hash, payment_rate_manager, sd_hash, key,
|
2018-08-02 20:33:03 +02:00
|
|
|
stream_name, file_name, download_directory, suggested_file_name, download_mirrors=None):
|
2018-01-05 03:28:09 +01:00
|
|
|
return ManagedEncryptedFileDownloader(
|
|
|
|
rowid,
|
|
|
|
stream_hash,
|
2018-07-25 21:33:43 +02:00
|
|
|
self.peer_finder,
|
|
|
|
self.rate_limiter,
|
|
|
|
self.blob_manager,
|
|
|
|
self.storage,
|
2018-01-05 03:28:09 +01:00
|
|
|
self,
|
|
|
|
payment_rate_manager,
|
2018-07-25 21:33:43 +02:00
|
|
|
self.wallet,
|
2018-01-05 03:28:09 +01:00
|
|
|
download_directory,
|
2018-02-12 20:03:39 +01:00
|
|
|
file_name,
|
|
|
|
stream_name=stream_name,
|
2018-01-05 03:28:09 +01:00
|
|
|
sd_hash=sd_hash,
|
|
|
|
key=key,
|
2018-06-10 09:57:06 +02:00
|
|
|
suggested_file_name=suggested_file_name,
|
2018-08-02 20:33:03 +02:00
|
|
|
download_mirrors=download_mirrors
|
2018-01-05 03:28:09 +01:00
|
|
|
)
|
|
|
|
|
2018-08-02 20:33:03 +02:00
|
|
|
def _start_lbry_file(self, file_info, payment_rate_manager, claim_info, download_mirrors=None):
|
2018-03-19 18:34:33 +01:00
|
|
|
lbry_file = self._get_lbry_file(
|
|
|
|
file_info['row_id'], file_info['stream_hash'], payment_rate_manager, file_info['sd_hash'],
|
|
|
|
file_info['key'], file_info['stream_name'], file_info['file_name'], file_info['download_directory'],
|
2018-08-02 20:33:03 +02:00
|
|
|
file_info['suggested_file_name'], download_mirrors
|
2018-03-19 18:34:33 +01:00
|
|
|
)
|
2018-05-09 15:50:44 +02:00
|
|
|
if claim_info:
|
|
|
|
lbry_file.set_claim_info(claim_info)
|
2018-03-19 18:34:33 +01:00
|
|
|
try:
|
2018-05-16 02:44:30 +02:00
|
|
|
# restore will raise an Exception if status is unknown
|
|
|
|
lbry_file.restore(file_info['status'])
|
|
|
|
self.storage.content_claim_callbacks[lbry_file.stream_hash] = lbry_file.get_claim_info
|
|
|
|
self.lbry_files.append(lbry_file)
|
|
|
|
if len(self.lbry_files) % 500 == 0:
|
|
|
|
log.info("Started %i files", len(self.lbry_files))
|
|
|
|
except Exception:
|
|
|
|
log.warning("Failed to start %i", file_info.get('rowid'))
|
2018-03-19 18:34:33 +01:00
|
|
|
|
2017-02-09 19:58:56 +01:00
|
|
|
@defer.inlineCallbacks
|
2018-05-16 02:44:30 +02:00
|
|
|
def _start_lbry_files(self):
|
2018-07-25 21:33:43 +02:00
|
|
|
files = yield self.storage.get_all_lbry_files()
|
|
|
|
claim_infos = yield self.storage.get_claims_from_stream_hashes([file['stream_hash'] for file in files])
|
|
|
|
prm = self.payment_rate_manager
|
2018-02-12 20:03:39 +01:00
|
|
|
|
2018-03-19 18:34:33 +01:00
|
|
|
log.info("Starting %i files", len(files))
|
|
|
|
for file_info in files:
|
2018-05-09 15:50:44 +02:00
|
|
|
claim_info = claim_infos.get(file_info['stream_hash'])
|
2018-06-08 16:16:21 +02:00
|
|
|
self._start_lbry_file(file_info, prm, claim_info)
|
2018-03-19 18:34:33 +01:00
|
|
|
|
2017-12-29 20:10:16 +01:00
|
|
|
log.info("Started %i lbry files", len(self.lbry_files))
|
2017-10-23 07:17:54 +02:00
|
|
|
if self.auto_re_reflect is True:
|
2018-05-08 19:51:02 +02:00
|
|
|
safe_start_looping_call(self.lbry_file_reflector, self.auto_re_reflect_interval / 10)
|
2017-02-11 16:09:57 +01:00
|
|
|
|
2017-02-09 19:58:56 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _stop_lbry_file(self, lbry_file):
|
|
|
|
def wait_for_finished(lbry_file, count=2):
|
|
|
|
if count or lbry_file.saving_status is not False:
|
2018-01-05 03:28:09 +01:00
|
|
|
return task.deferLater(reactor, 1, self._stop_lbry_file, lbry_file,
|
|
|
|
count=count - 1)
|
2017-02-09 19:58:56 +01:00
|
|
|
try:
|
|
|
|
yield lbry_file.stop(change_status=False)
|
|
|
|
self.lbry_files.remove(lbry_file)
|
|
|
|
except CurrentlyStoppingError:
|
|
|
|
yield wait_for_finished(lbry_file)
|
|
|
|
except AlreadyStoppedError:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
defer.returnValue(None)
|
|
|
|
|
2018-08-13 04:04:55 +02:00
|
|
|
@defer.inlineCallbacks
|
2017-02-09 19:58:56 +01:00
|
|
|
def _stop_lbry_files(self):
|
|
|
|
log.info("Stopping %i lbry files", len(self.lbry_files))
|
2018-08-13 04:04:55 +02:00
|
|
|
yield defer.DeferredList([self._stop_lbry_file(lbry_file) for lbry_file in list(self.lbry_files)])
|
2017-02-09 19:58:56 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-02-12 20:03:39 +01:00
|
|
|
def add_published_file(self, stream_hash, sd_hash, download_directory, payment_rate_manager, blob_data_rate):
|
|
|
|
status = ManagedEncryptedFileDownloader.STATUS_FINISHED
|
2018-07-25 21:33:43 +02:00
|
|
|
stream_metadata = yield get_sd_info(self.storage, stream_hash, include_blobs=False)
|
2018-02-12 20:03:39 +01:00
|
|
|
key = stream_metadata['key']
|
|
|
|
stream_name = stream_metadata['stream_name']
|
|
|
|
file_name = stream_metadata['suggested_file_name']
|
|
|
|
rowid = yield self.storage.save_published_file(
|
|
|
|
stream_hash, file_name, download_directory, blob_data_rate, status
|
|
|
|
)
|
|
|
|
lbry_file = self._get_lbry_file(
|
|
|
|
rowid, stream_hash, payment_rate_manager, sd_hash, key, stream_name, file_name, download_directory,
|
2018-08-02 20:33:03 +02:00
|
|
|
stream_metadata['suggested_file_name'], download_mirrors=None
|
2018-02-12 20:03:39 +01:00
|
|
|
)
|
|
|
|
lbry_file.restore(status)
|
2018-02-28 20:20:33 +01:00
|
|
|
yield lbry_file.get_claim_info()
|
|
|
|
self.storage.content_claim_callbacks[stream_hash] = lbry_file.get_claim_info
|
2018-02-12 20:03:39 +01:00
|
|
|
self.lbry_files.append(lbry_file)
|
|
|
|
defer.returnValue(lbry_file)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def add_downloaded_file(self, stream_hash, sd_hash, download_directory, payment_rate_manager=None,
|
2018-08-02 20:33:03 +02:00
|
|
|
blob_data_rate=None, status=None, file_name=None, download_mirrors=None):
|
2018-02-12 20:03:39 +01:00
|
|
|
status = status or ManagedEncryptedFileDownloader.STATUS_STOPPED
|
2018-07-25 21:33:43 +02:00
|
|
|
payment_rate_manager = payment_rate_manager or self.payment_rate_manager
|
2018-02-12 20:03:39 +01:00
|
|
|
blob_data_rate = blob_data_rate or payment_rate_manager.min_blob_data_payment_rate
|
2018-07-25 21:33:43 +02:00
|
|
|
stream_metadata = yield get_sd_info(self.storage, stream_hash, include_blobs=False)
|
2018-01-05 03:28:09 +01:00
|
|
|
key = stream_metadata['key']
|
|
|
|
stream_name = stream_metadata['stream_name']
|
2018-02-12 20:03:39 +01:00
|
|
|
file_name = file_name or stream_metadata['suggested_file_name']
|
|
|
|
|
|
|
|
# when we save the file we'll atomic touch the nearest file to the suggested file name
|
|
|
|
# that doesn't yet exist in the download directory
|
|
|
|
rowid = yield self.storage.save_downloaded_file(
|
|
|
|
stream_hash, os.path.basename(file_name.decode('hex')).encode('hex'), download_directory, blob_data_rate
|
|
|
|
)
|
2018-07-25 21:33:43 +02:00
|
|
|
file_name = yield self.storage.get_filename_for_rowid(rowid)
|
2018-02-12 20:03:39 +01:00
|
|
|
lbry_file = self._get_lbry_file(
|
|
|
|
rowid, stream_hash, payment_rate_manager, sd_hash, key, stream_name, file_name, download_directory,
|
2018-08-02 20:33:03 +02:00
|
|
|
stream_metadata['suggested_file_name'], download_mirrors
|
2018-02-12 20:03:39 +01:00
|
|
|
)
|
|
|
|
lbry_file.restore(status)
|
2018-02-28 20:20:33 +01:00
|
|
|
yield lbry_file.get_claim_info(include_supports=False)
|
|
|
|
self.storage.content_claim_callbacks[stream_hash] = lbry_file.get_claim_info
|
2018-01-05 03:28:09 +01:00
|
|
|
self.lbry_files.append(lbry_file)
|
2017-02-09 19:58:56 +01:00
|
|
|
defer.returnValue(lbry_file)
|
2015-09-15 06:29:18 +02:00
|
|
|
|
2017-03-06 23:19:05 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def delete_lbry_file(self, lbry_file, delete_file=False):
|
|
|
|
if lbry_file not in self.lbry_files:
|
|
|
|
raise ValueError("Could not find that LBRY file")
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def wait_for_finished(count=2):
|
|
|
|
if count <= 0 or lbry_file.saving_status is False:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return task.deferLater(reactor, 1, wait_for_finished, count=count - 1)
|
|
|
|
|
2017-03-06 23:19:05 +01:00
|
|
|
full_path = os.path.join(lbry_file.download_directory, lbry_file.file_name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield lbry_file.stop()
|
|
|
|
except (AlreadyStoppedError, CurrentlyStoppingError):
|
|
|
|
yield wait_for_finished()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-03-06 23:19:05 +01:00
|
|
|
self.lbry_files.remove(lbry_file)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2018-02-28 20:20:33 +01:00
|
|
|
if lbry_file.stream_hash in self.storage.content_claim_callbacks:
|
|
|
|
del self.storage.content_claim_callbacks[lbry_file.stream_hash]
|
|
|
|
|
2017-03-06 23:19:05 +01:00
|
|
|
yield lbry_file.delete_data()
|
2018-07-25 21:33:43 +02:00
|
|
|
yield self.storage.delete_stream(lbry_file.stream_hash)
|
2017-03-06 23:19:05 +01:00
|
|
|
|
|
|
|
if delete_file and os.path.isfile(full_path):
|
|
|
|
os.remove(full_path)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-03-06 23:19:05 +01:00
|
|
|
defer.returnValue(True)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-01-16 07:16:37 +01:00
|
|
|
def toggle_lbry_file_running(self, lbry_file):
|
2015-08-20 17:27:15 +02:00
|
|
|
"""Toggle whether a stream reader is currently running"""
|
|
|
|
for l in self.lbry_files:
|
2016-01-16 07:16:37 +01:00
|
|
|
if l == lbry_file:
|
2015-08-20 17:27:15 +02:00
|
|
|
return l.toggle_running()
|
2017-04-25 20:08:33 +02:00
|
|
|
return defer.fail(Failure(ValueError("Could not find that LBRY file")))
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-10 16:56:22 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def reflect_lbry_files(self):
|
2017-10-02 20:52:04 +02:00
|
|
|
sem = defer.DeferredSemaphore(self.CONCURRENT_REFLECTS)
|
|
|
|
ds = []
|
2018-05-08 19:51:02 +02:00
|
|
|
sd_hashes_to_reflect = yield self.storage.get_streams_to_re_reflect()
|
2017-10-02 20:52:04 +02:00
|
|
|
for lbry_file in self.lbry_files:
|
2018-05-08 19:51:02 +02:00
|
|
|
if lbry_file.sd_hash in sd_hashes_to_reflect:
|
|
|
|
ds.append(sem.run(reflect_file, lbry_file))
|
2017-10-02 20:52:04 +02:00
|
|
|
yield defer.DeferredList(ds)
|
2017-02-10 16:56:22 +01:00
|
|
|
|
2017-02-09 19:58:56 +01:00
|
|
|
@defer.inlineCallbacks
|
2015-08-20 17:27:15 +02:00
|
|
|
def stop(self):
|
2017-02-10 16:56:22 +01:00
|
|
|
safe_stop_looping_call(self.lbry_file_reflector)
|
2018-08-13 04:04:55 +02:00
|
|
|
yield self._stop_lbry_files()
|
2017-09-20 17:16:08 +02:00
|
|
|
log.info("Stopped encrypted file manager")
|
2017-02-09 19:58:56 +01:00
|
|
|
defer.returnValue(True)
|