Вынес uploader_backend в отдельный сущность
This commit is contained in:
@@ -26,13 +26,15 @@ class ChunkUploadBackend(ABC):
|
||||
|
||||
|
||||
class DiskChunkUploadBackend(ChunkUploadBackend):
|
||||
def __init__(self, base_path: str):
|
||||
self.base_path = base_path
|
||||
def __init__(self, key_prefix: str = ""):
|
||||
self.base_path = str(settings.MEDIA_DIR)
|
||||
os.makedirs(self.base_path, exist_ok=True)
|
||||
self.key_prefix = key_prefix
|
||||
self._file = None
|
||||
|
||||
def start(self, filename):
|
||||
self._file = open(os.path.join(self.base_path, filename), "wb")
|
||||
name = f"{self.key_prefix}{filename}"
|
||||
self._file = open(os.path.join(self.base_path, name), "wb")
|
||||
|
||||
def upload_chunk(self, chunk: bytes):
|
||||
self._file.write(chunk)
|
||||
@@ -41,7 +43,8 @@ class DiskChunkUploadBackend(ChunkUploadBackend):
|
||||
if self._file is None:
|
||||
return
|
||||
self._file.close()
|
||||
return os.path.join(self.base_path, self._file.name)
|
||||
relative_path = settings.MEDIA_DIR.relative_to(settings.BASE_DIR)
|
||||
return os.path.join(relative_path, self._file.name)
|
||||
|
||||
def abort(self):
|
||||
if self._file:
|
||||
|
||||
Reference in New Issue
Block a user