feat: upload - deel 03.02 - Skipp all toegevoegd
This commit is contained in:
Binary file not shown.
@@ -37,10 +37,11 @@ async def delete(
|
||||
@router.post("/upload", response_model=UploadResponse)
|
||||
async def upload(
|
||||
target_path: str = Form(...),
|
||||
overwrite: bool = Form(False),
|
||||
file: UploadFile = File(...),
|
||||
service: FileOpsService = Depends(get_file_ops_service),
|
||||
) -> UploadResponse:
|
||||
return service.upload(target_path=target_path, upload_file=file)
|
||||
return service.upload(target_path=target_path, upload_file=file, overwrite=overwrite)
|
||||
|
||||
|
||||
@router.get("/view", response_model=ViewResponse)
|
||||
|
||||
Binary file not shown.
@@ -140,8 +140,9 @@ class FilesystemAdapter:
|
||||
"modified": self.modified_iso(path),
|
||||
}
|
||||
|
||||
def write_uploaded_file(self, path: Path, file_stream, chunk_size: int = 1024 * 1024) -> dict:
|
||||
with path.open("xb") as handle:
|
||||
def write_uploaded_file(self, path: Path, file_stream, chunk_size: int = 1024 * 1024, overwrite: bool = False) -> dict:
|
||||
mode = "wb" if overwrite else "xb"
|
||||
with path.open(mode) as handle:
|
||||
while True:
|
||||
chunk = file_stream.read(chunk_size)
|
||||
if not chunk:
|
||||
|
||||
Binary file not shown.
@@ -204,7 +204,7 @@ class FileOpsService:
|
||||
self._record_history_error(operation="delete", path=path, error=error)
|
||||
raise error
|
||||
|
||||
def upload(self, target_path: str, upload_file) -> UploadResponse:
|
||||
def upload(self, target_path: str, upload_file, overwrite: bool = False) -> UploadResponse:
|
||||
destination_relative = None
|
||||
history_path = target_path
|
||||
try:
|
||||
@@ -216,14 +216,26 @@ class FileOpsService:
|
||||
resolved_destination = self._path_guard.resolve_path(destination_relative)
|
||||
|
||||
if resolved_destination.absolute.exists():
|
||||
raise AppError(
|
||||
code="already_exists",
|
||||
message="Target path already exists",
|
||||
status_code=409,
|
||||
details={"path": resolved_destination.relative},
|
||||
)
|
||||
if not overwrite:
|
||||
raise AppError(
|
||||
code="already_exists",
|
||||
message="Target path already exists",
|
||||
status_code=409,
|
||||
details={"path": resolved_destination.relative},
|
||||
)
|
||||
if resolved_destination.absolute.is_dir():
|
||||
raise AppError(
|
||||
code="type_conflict",
|
||||
message="Cannot overwrite an existing directory",
|
||||
status_code=409,
|
||||
details={"path": resolved_destination.relative},
|
||||
)
|
||||
|
||||
saved = self._filesystem.write_uploaded_file(resolved_destination.absolute, upload_file.file)
|
||||
saved = self._filesystem.write_uploaded_file(
|
||||
resolved_destination.absolute,
|
||||
upload_file.file,
|
||||
overwrite=overwrite,
|
||||
)
|
||||
self._record_history(
|
||||
operation="upload",
|
||||
status="completed",
|
||||
|
||||
Reference in New Issue
Block a user