file_path can be None, add helper to check if stream output file exists instead

This commit is contained in:
Victor Shyba 2019-02-19 21:44:31 -03:00
parent 82ce2ccee6
commit d035c13883
2 changed files with 13 additions and 14 deletions

View file

@ -22,7 +22,7 @@ class ManagedStream:
STATUS_FINISHED = "finished"
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,
status: typing.Optional[str] = STATUS_STOPPED, claim: typing.Optional[StoredStreamClaim] = None):
self.loop = loop
@ -39,7 +39,7 @@ class ManagedStream:
self.tx = None
@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
@property
@ -112,14 +112,16 @@ class ManagedStream:
return self.blobs_in_stream - self.blobs_completed
@property
def full_path(self) -> str:
return os.path.join(self.download_directory, os.path.basename(self.file_name))
def full_path(self) -> typing.Optional[str]:
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:
full_path = self.full_path
if not os.path.isfile(full_path):
full_path = None
mime_type = guess_media_type(os.path.basename(self.file_name))
full_path = self.full_path if self.output_file_exists else None
mime_type = guess_media_type(os.path.basename(self.descriptor.suggested_file_name))
if self.downloader and self.downloader.written_bytes:
written_bytes = self.downloader.written_bytes

View file

@ -76,10 +76,7 @@ class StreamManager:
"""
Resume or rebuild a partial or completed stream
"""
path = os.path.join(stream.download_directory, stream.file_name)
if not stream.running and not os.path.isfile(path):
if not stream.running and not stream.output_file_exists:
if stream.downloader:
stream.downloader.stop()
stream.downloader = None
@ -117,7 +114,7 @@ class StreamManager:
async def stop_stream(self, stream: ManagedStream):
stream.stop_download()
if not stream.finished and os.path.isfile(stream.full_path):
if not stream.finished and stream.output_file_exists:
try:
os.remove(stream.full_path)
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]]
await self.blob_manager.delete_blobs(blob_hashes, delete_from_db=False)
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)
def wait_for_stream_finished(self, stream: ManagedStream):