file_path can be None, add helper to check if stream output file exists instead
This commit is contained in:
parent
82ce2ccee6
commit
d035c13883
2 changed files with 13 additions and 14 deletions
|
@ -22,7 +22,7 @@ class ManagedStream:
|
||||||
STATUS_FINISHED = "finished"
|
STATUS_FINISHED = "finished"
|
||||||
|
|
||||||
def __init__(self, loop: asyncio.BaseEventLoop, blob_manager: 'BlobFileManager', rowid: int,
|
def __init__(self, loop: asyncio.BaseEventLoop, blob_manager: 'BlobFileManager', rowid: int,
|
||||||
descriptor: 'StreamDescriptor', download_directory: str, file_name: str,
|
descriptor: 'StreamDescriptor', download_directory: str, file_name: typing.Optional[str],
|
||||||
downloader: typing.Optional[StreamDownloader] = None,
|
downloader: typing.Optional[StreamDownloader] = None,
|
||||||
status: typing.Optional[str] = STATUS_STOPPED, claim: typing.Optional[StoredStreamClaim] = None):
|
status: typing.Optional[str] = STATUS_STOPPED, claim: typing.Optional[StoredStreamClaim] = None):
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
|
@ -39,7 +39,7 @@ class ManagedStream:
|
||||||
self.tx = None
|
self.tx = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def file_name(self):
|
def file_name(self) -> typing.Optional[str]:
|
||||||
return self.downloader.output_file_name if self.downloader else self._file_name
|
return self.downloader.output_file_name if self.downloader else self._file_name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -112,14 +112,16 @@ class ManagedStream:
|
||||||
return self.blobs_in_stream - self.blobs_completed
|
return self.blobs_in_stream - self.blobs_completed
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def full_path(self) -> str:
|
def full_path(self) -> typing.Optional[str]:
|
||||||
return os.path.join(self.download_directory, os.path.basename(self.file_name))
|
return os.path.join(self.download_directory, os.path.basename(self.file_name)) if self.file_name else None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def output_file_exists(self):
|
||||||
|
return os.path.isfile(self.full_path) if self.full_path else False
|
||||||
|
|
||||||
def as_dict(self) -> typing.Dict:
|
def as_dict(self) -> typing.Dict:
|
||||||
full_path = self.full_path
|
full_path = self.full_path if self.output_file_exists else None
|
||||||
if not os.path.isfile(full_path):
|
mime_type = guess_media_type(os.path.basename(self.descriptor.suggested_file_name))
|
||||||
full_path = None
|
|
||||||
mime_type = guess_media_type(os.path.basename(self.file_name))
|
|
||||||
|
|
||||||
if self.downloader and self.downloader.written_bytes:
|
if self.downloader and self.downloader.written_bytes:
|
||||||
written_bytes = self.downloader.written_bytes
|
written_bytes = self.downloader.written_bytes
|
||||||
|
|
|
@ -76,10 +76,7 @@ class StreamManager:
|
||||||
"""
|
"""
|
||||||
Resume or rebuild a partial or completed stream
|
Resume or rebuild a partial or completed stream
|
||||||
"""
|
"""
|
||||||
|
if not stream.running and not stream.output_file_exists:
|
||||||
path = os.path.join(stream.download_directory, stream.file_name)
|
|
||||||
|
|
||||||
if not stream.running and not os.path.isfile(path):
|
|
||||||
if stream.downloader:
|
if stream.downloader:
|
||||||
stream.downloader.stop()
|
stream.downloader.stop()
|
||||||
stream.downloader = None
|
stream.downloader = None
|
||||||
|
@ -117,7 +114,7 @@ class StreamManager:
|
||||||
|
|
||||||
async def stop_stream(self, stream: ManagedStream):
|
async def stop_stream(self, stream: ManagedStream):
|
||||||
stream.stop_download()
|
stream.stop_download()
|
||||||
if not stream.finished and os.path.isfile(stream.full_path):
|
if not stream.finished and stream.output_file_exists:
|
||||||
try:
|
try:
|
||||||
os.remove(stream.full_path)
|
os.remove(stream.full_path)
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
|
@ -272,7 +269,7 @@ class StreamManager:
|
||||||
blob_hashes = [stream.sd_hash] + [b.blob_hash for b in stream.descriptor.blobs[:-1]]
|
blob_hashes = [stream.sd_hash] + [b.blob_hash for b in stream.descriptor.blobs[:-1]]
|
||||||
await self.blob_manager.delete_blobs(blob_hashes, delete_from_db=False)
|
await self.blob_manager.delete_blobs(blob_hashes, delete_from_db=False)
|
||||||
await self.storage.delete_stream(stream.descriptor)
|
await self.storage.delete_stream(stream.descriptor)
|
||||||
if delete_file and os.path.isfile(stream.full_path):
|
if delete_file and stream.output_file_exists:
|
||||||
os.remove(stream.full_path)
|
os.remove(stream.full_path)
|
||||||
|
|
||||||
def wait_for_stream_finished(self, stream: ManagedStream):
|
def wait_for_stream_finished(self, stream: ManagedStream):
|
||||||
|
|
Loading…
Reference in a new issue