2018-10-16 15:04:20 -04:00
|
|
|
import asyncio
|
2016-05-04 23:27:40 -04:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2018-11-04 14:55:01 -05:00
|
|
|
from lbrynet.blob.EncryptedFileCreator import create_lbry_file
|
2019-01-08 15:27:13 -05:00
|
|
|
from lbrynet.extras.daemon.mime_types import guess_media_type
|
2016-06-07 04:19:51 -04:00
|
|
|
|
2016-01-20 22:00:28 -05:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-10-16 15:04:20 -04:00
|
|
|
def d2f(d):
|
|
|
|
return d.asFuture(asyncio.get_event_loop())
|
|
|
|
|
|
|
|
|
2018-07-21 18:34:59 -04:00
|
|
|
class Publisher:
|
2018-10-17 19:07:17 -04:00
|
|
|
def __init__(self, account, blob_manager, payment_rate_manager, storage,
|
|
|
|
lbry_file_manager, wallet, certificate):
|
|
|
|
self.account = account
|
2018-07-25 15:33:43 -04:00
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.payment_rate_manager = payment_rate_manager
|
|
|
|
self.storage = storage
|
2016-01-20 22:00:28 -05:00
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
self.wallet = wallet
|
2018-07-11 23:18:59 -04:00
|
|
|
self.certificate = certificate
|
2016-01-20 22:00:28 -05:00
|
|
|
self.lbry_file = None
|
|
|
|
|
2018-10-16 15:04:20 -04:00
|
|
|
async def create_and_publish_stream(self, name, bid, claim_dict, file_path, holding_address=None):
|
2017-04-25 14:23:05 -04:00
|
|
|
"""Create lbry file and make claim"""
|
2016-08-10 19:04:03 -05:00
|
|
|
log.info('Starting publish for %s', name)
|
2017-05-29 14:41:15 -04:00
|
|
|
if not os.path.isfile(file_path):
|
2018-10-18 13:42:45 +03:00
|
|
|
raise Exception(f"File {file_path} not found")
|
2017-05-29 14:41:15 -04:00
|
|
|
if os.path.getsize(file_path) == 0:
|
2018-10-18 13:42:45 +03:00
|
|
|
raise Exception(f"Cannot publish empty file {file_path}")
|
2017-05-29 14:41:15 -04:00
|
|
|
|
2017-02-09 16:12:30 -05:00
|
|
|
file_name = os.path.basename(file_path)
|
2018-07-27 21:35:01 -04:00
|
|
|
with open(file_path, 'rb') as read_handle:
|
2018-10-16 15:04:20 -04:00
|
|
|
self.lbry_file = await d2f(create_lbry_file(
|
2018-08-03 09:36:03 -04:00
|
|
|
self.blob_manager, self.storage, self.payment_rate_manager, self.lbry_file_manager, file_name,
|
|
|
|
read_handle
|
2018-10-16 15:04:20 -04:00
|
|
|
))
|
2018-02-12 14:13:30 -05:00
|
|
|
|
2017-04-03 15:58:20 -04:00
|
|
|
if 'source' not in claim_dict['stream']:
|
|
|
|
claim_dict['stream']['source'] = {}
|
2018-02-12 14:13:30 -05:00
|
|
|
claim_dict['stream']['source']['source'] = self.lbry_file.sd_hash
|
2017-04-03 15:58:20 -04:00
|
|
|
claim_dict['stream']['source']['sourceType'] = 'lbry_sd_hash'
|
2019-01-08 15:27:13 -05:00
|
|
|
claim_dict['stream']['source']['contentType'] = guess_media_type(file_path)
|
2018-01-04 21:28:09 -05:00
|
|
|
claim_dict['stream']['source']['version'] = "_0_0_1" # need current version here
|
2018-10-16 15:04:20 -04:00
|
|
|
tx = await self.wallet.claim_name(
|
2018-10-17 19:07:17 -04:00
|
|
|
self.account, name, bid, claim_dict, self.certificate, holding_address
|
2018-07-12 14:23:25 -04:00
|
|
|
)
|
2018-02-27 16:21:37 -05:00
|
|
|
|
|
|
|
# check if we have a file already for this claim (if this is a publish update with a new stream)
|
2018-12-15 15:31:02 -05:00
|
|
|
old_stream_hashes = await self.storage.get_old_stream_hashes_for_claim_id(
|
2018-08-04 12:10:41 -04:00
|
|
|
tx.outputs[0].claim_id, self.lbry_file.stream_hash
|
2018-12-15 15:31:02 -05:00
|
|
|
)
|
2018-02-27 16:21:37 -05:00
|
|
|
if old_stream_hashes:
|
2018-02-28 14:59:12 -05:00
|
|
|
for lbry_file in filter(lambda l: l.stream_hash in old_stream_hashes,
|
|
|
|
list(self.lbry_file_manager.lbry_files)):
|
2018-10-16 15:04:20 -04:00
|
|
|
await d2f(self.lbry_file_manager.delete_lbry_file(lbry_file, delete_file=False))
|
2018-02-28 14:59:12 -05:00
|
|
|
log.info("Removed old stream for claim update: %s", lbry_file.stream_hash)
|
2018-02-27 16:21:37 -05:00
|
|
|
|
2018-12-15 15:31:02 -05:00
|
|
|
await self.storage.save_content_claim(
|
2018-08-03 12:31:50 -04:00
|
|
|
self.lbry_file.stream_hash, tx.outputs[0].id
|
2018-12-15 15:31:02 -05:00
|
|
|
)
|
2018-10-16 15:04:20 -04:00
|
|
|
return tx
|
2017-02-09 16:12:30 -05:00
|
|
|
|
2018-10-16 15:04:20 -04:00
|
|
|
async def publish_stream(self, name, bid, claim_dict, stream_hash, holding_address=None):
|
2017-04-25 14:23:05 -04:00
|
|
|
"""Make a claim without creating a lbry file"""
|
2018-10-16 15:04:20 -04:00
|
|
|
tx = await self.wallet.claim_name(
|
2018-10-17 19:07:17 -04:00
|
|
|
self.account, name, bid, claim_dict, self.certificate, holding_address
|
2018-07-12 14:23:25 -04:00
|
|
|
)
|
2018-06-14 18:14:53 -04:00
|
|
|
if stream_hash: # the stream_hash returned from the db will be None if this isn't a stream we have
|
2019-01-10 13:06:42 -05:00
|
|
|
await self.storage.save_content_claim(
|
2018-10-02 10:26:02 -04:00
|
|
|
stream_hash, tx.outputs[0].id
|
2019-01-10 13:06:42 -05:00
|
|
|
)
|
2018-06-14 18:14:53 -04:00
|
|
|
self.lbry_file = [f for f in self.lbry_file_manager.lbry_files if f.stream_hash == stream_hash][0]
|
2018-10-16 15:04:20 -04:00
|
|
|
return tx
|