2024-11-06 23:13:32 +00:00
|
|
|
import abc
|
|
|
|
import os
|
2024-11-07 02:17:31 +00:00
|
|
|
from typing import BinaryIO, Iterator, Tuple
|
2024-11-06 23:13:32 +00:00
|
|
|
|
|
|
|
from typing import BinaryIO, Tuple
|
|
|
|
|
|
|
|
class StorageFile(abc.ABC):
|
|
|
|
local_path: str
|
|
|
|
|
|
|
|
def __init__(self, local_path: str) -> None:
|
|
|
|
self.local_path = local_path
|
|
|
|
|
|
|
|
def get_local_path(self) -> str:
|
|
|
|
return self.local_path
|
|
|
|
|
|
|
|
def cleanup_local_file(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
self.cleanup_local_file()
|
|
|
|
|
|
|
|
|
|
|
|
class LocalCachedFile(StorageFile):
|
|
|
|
def __init__(self, local_path: str) -> None:
|
|
|
|
super().__init__(local_path)
|
|
|
|
|
|
|
|
def cleanup_local_file(self) -> None:
|
|
|
|
os.remove(self.local_path)
|
|
|
|
pass
|
|
|
|
|
|
|
|
class LocalFile(StorageFile):
|
|
|
|
def __init__(self, local_path: str) -> None:
|
|
|
|
super().__init__(local_path)
|
|
|
|
|
|
|
|
class StorageProvider(abc.ABC):
|
|
|
|
@abc.abstractmethod
|
2024-11-07 02:17:31 +00:00
|
|
|
def upload_file(self, file: BinaryIO, filename: str) -> Tuple[bytes, str]:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Uploads a file to the storage and returns the file content bytes and path."""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2024-11-07 02:17:31 +00:00
|
|
|
def get_file(self, file_path: str) -> Iterator[bytes]:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Downloads file content"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2024-11-07 02:17:31 +00:00
|
|
|
def as_local_file(self, file_path: str) -> StorageFile:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Downloads a file from S3 and returns the file path."""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2024-11-07 02:17:31 +00:00
|
|
|
def delete_file(self, filename: str) -> None:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Deletes a file from S3."""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2024-11-07 04:51:53 +00:00
|
|
|
def delete_all_files(self, folder: str) -> None:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Deletes all files from the storage."""
|