diff --git a/lbrynet/blob/blob_file.py b/lbrynet/blob/blob_file.py index f1a2010ee..56e3a4ed6 100644 --- a/lbrynet/blob/blob_file.py +++ b/lbrynet/blob/blob_file.py @@ -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) diff --git a/lbrynet/blob/reader.py b/lbrynet/blob/reader.py index c85cc38f3..fb4724d3a 100644 --- a/lbrynet/blob/reader.py +++ b/lbrynet/blob/reader.py @@ -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) + +