2016-05-05 05:27:40 +02:00
|
|
|
import logging
|
2016-06-27 23:07:59 +02:00
|
|
|
import mimetypes
|
2016-05-05 05:27:40 +02:00
|
|
|
import os
|
|
|
|
|
2017-02-09 22:12:30 +01:00
|
|
|
from twisted.internet import defer
|
2017-04-03 21:58:20 +02:00
|
|
|
|
2017-02-09 22:12:30 +01:00
|
|
|
from lbrynet.core import file_utils
|
2017-06-26 03:04:04 +02:00
|
|
|
from lbrynet.file_manager.EncryptedFileCreator import create_lbry_file
|
2018-07-12 20:15:16 +02:00
|
|
|
from lbrynet.wallet.account import get_certificate_lookup
|
2016-06-07 10:19:51 +02:00
|
|
|
|
2016-01-21 04:00:28 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Publisher(object):
|
2018-07-12 05:18:59 +02:00
|
|
|
def __init__(self, blob_manager, payment_rate_manager, storage, lbry_file_manager, wallet, certificate):
|
2018-07-25 21:33:43 +02:00
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.payment_rate_manager = payment_rate_manager
|
|
|
|
self.storage = storage
|
2016-01-21 04:00:28 +01:00
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
self.wallet = wallet
|
2018-07-12 05:18:59 +02:00
|
|
|
self.certificate = certificate
|
2016-01-21 04:00:28 +01:00
|
|
|
self.lbry_file = None
|
|
|
|
|
2017-02-09 22:12:30 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-06-12 19:32:01 +02:00
|
|
|
def create_and_publish_stream(self, name, bid, claim_dict, file_path, claim_address=None,
|
|
|
|
change_address=None):
|
2017-04-25 20:23:05 +02:00
|
|
|
"""Create lbry file and make claim"""
|
2016-08-11 02:04:03 +02:00
|
|
|
log.info('Starting publish for %s', name)
|
2017-05-29 20:41:15 +02:00
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
raise Exception("File {} not found".format(file_path))
|
|
|
|
if os.path.getsize(file_path) == 0:
|
|
|
|
raise Exception("Cannot publish empty file {}".format(file_path))
|
|
|
|
|
2017-02-09 22:12:30 +01:00
|
|
|
file_name = os.path.basename(file_path)
|
|
|
|
with file_utils.get_read_handle(file_path) as read_handle:
|
2018-08-03 15:36:03 +02:00
|
|
|
self.lbry_file = yield create_lbry_file(
|
|
|
|
self.blob_manager, self.storage, self.payment_rate_manager, self.lbry_file_manager, file_name,
|
|
|
|
read_handle
|
|
|
|
)
|
2018-02-12 20:13:30 +01:00
|
|
|
|
2017-04-03 21:58:20 +02:00
|
|
|
if 'source' not in claim_dict['stream']:
|
|
|
|
claim_dict['stream']['source'] = {}
|
2018-02-12 20:13:30 +01:00
|
|
|
claim_dict['stream']['source']['source'] = self.lbry_file.sd_hash
|
2017-04-03 21:58:20 +02:00
|
|
|
claim_dict['stream']['source']['sourceType'] = 'lbry_sd_hash'
|
|
|
|
claim_dict['stream']['source']['contentType'] = get_content_type(file_path)
|
2018-01-05 03:28:09 +01:00
|
|
|
claim_dict['stream']['source']['version'] = "_0_0_1" # need current version here
|
2018-07-12 20:15:16 +02:00
|
|
|
tx = yield self.make_claim(name, bid, claim_dict, claim_address, change_address)
|
2018-02-27 22:21:37 +01:00
|
|
|
|
|
|
|
# check if we have a file already for this claim (if this is a publish update with a new stream)
|
2018-08-03 15:36:03 +02:00
|
|
|
old_stream_hashes = yield self.storage.get_old_stream_hashes_for_claim_id(
|
2018-07-12 20:15:16 +02:00
|
|
|
tx.get_claim_id(0), self.lbry_file.stream_hash
|
2018-08-03 15:36:03 +02:00
|
|
|
)
|
2018-02-27 22:21:37 +01:00
|
|
|
if old_stream_hashes:
|
2018-02-28 20:59:12 +01:00
|
|
|
for lbry_file in filter(lambda l: l.stream_hash in old_stream_hashes,
|
|
|
|
list(self.lbry_file_manager.lbry_files)):
|
|
|
|
yield self.lbry_file_manager.delete_lbry_file(lbry_file, delete_file=False)
|
|
|
|
log.info("Removed old stream for claim update: %s", lbry_file.stream_hash)
|
2018-02-27 22:21:37 +01:00
|
|
|
|
2018-07-25 21:33:43 +02:00
|
|
|
yield self.storage.save_content_claim(
|
2018-07-12 20:15:16 +02:00
|
|
|
self.lbry_file.stream_hash, get_certificate_lookup(tx, 0)
|
2018-02-12 20:13:30 +01:00
|
|
|
)
|
2018-07-12 20:15:16 +02:00
|
|
|
defer.returnValue(tx)
|
2017-02-09 22:12:30 +01:00
|
|
|
|
2017-03-30 21:12:07 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-02-12 20:13:30 +01:00
|
|
|
def publish_stream(self, name, bid, claim_dict, stream_hash, claim_address=None, change_address=None):
|
2017-04-25 20:23:05 +02:00
|
|
|
"""Make a claim without creating a lbry file"""
|
2017-06-12 19:32:01 +02:00
|
|
|
claim_out = yield self.make_claim(name, bid, claim_dict, claim_address, change_address)
|
2018-06-15 00:14:53 +02:00
|
|
|
if stream_hash: # the stream_hash returned from the db will be None if this isn't a stream we have
|
2018-08-03 15:36:03 +02:00
|
|
|
yield self.storage.save_content_claim(
|
|
|
|
stream_hash, "%s:%i" % (claim_out['txid'], claim_out['nout'])
|
|
|
|
)
|
2018-06-15 00:14:53 +02:00
|
|
|
self.lbry_file = [f for f in self.lbry_file_manager.lbry_files if f.stream_hash == stream_hash][0]
|
2017-02-09 22:12:30 +01:00
|
|
|
defer.returnValue(claim_out)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2017-06-12 19:32:01 +02:00
|
|
|
def make_claim(self, name, bid, claim_dict, claim_address=None, change_address=None):
|
2018-07-12 18:51:56 +02:00
|
|
|
claim_out = yield self.wallet.claim_name(
|
|
|
|
name, bid, claim_dict, certificate=self.certificate, claim_address=claim_address
|
|
|
|
)
|
2017-02-09 22:12:30 +01:00
|
|
|
defer.returnValue(claim_out)
|
2016-08-11 02:04:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_content_type(filename):
|
2016-10-14 15:55:45 +02:00
|
|
|
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|