add reader, for BlobFile, change original reader to v0

This commit is contained in:
Kay Kurokawa 2017-09-26 00:13:47 -04:00
parent df9cec66be
commit 09e25ba285
2 changed files with 24 additions and 3 deletions

View file

@ -9,7 +9,7 @@ from lbrynet import conf
from lbrynet.core.Error import DownloadCanceledError, InvalidDataError, InvalidBlobHashError
from lbrynet.core.utils import is_valid_blobhash
from lbrynet.blob.writer import HashBlobWriter
from lbrynet.blob.reader import HashBlobReader
from lbrynet.blob.reader import HashBlobReader, HashBlobReader_v0
log = logging.getLogger(__name__)
@ -155,7 +155,7 @@ class BlobFile(object):
return args[0]
file_sender = FileSender()
reader = HashBlobReader(write_func)
reader = HashBlobReader_v0(write_func)
file_handle = self.open_for_reading()
if file_handle is not None:
d = file_sender.beginFileTransfer(file_handle, reader)

View file

@ -5,7 +5,7 @@ from zope.interface import implements
log = logging.getLogger(__name__)
class HashBlobReader(object):
class HashBlobReader_v0(object):
implements(interfaces.IConsumer)
def __init__(self, write_func):
@ -28,3 +28,24 @@ class HashBlobReader(object):
self.write_func(data)
if self.streaming is False:
reactor.callLater(0, self.producer.resumeProducing)
class HashBlobReader(object):
def __init__(self, file_path, finished_cb):
self.finished_cb = finished_cb
self.finished_cb_d = None
self.read_handle = open(file_path, 'rb')
def __del__(self):
self.close()
def read(self, size=-1):
return self.read_handle.read(size)
def close(self):
# if we've already closed and called finished_cb, do nothing
if self.finished_cb_d is not None:
return
self.read_handle.close()
self.finished_cb_d = self.finished_cb(self)