Skip to content

Commit

Permalink
Merge pull request #1873 from DSD-DBS/file-tree
Browse files Browse the repository at this point in the history
feat!: Rewrite major parts of the file browser
  • Loading branch information
MoritzWeber0 authored Oct 4, 2024
2 parents bd68e87 + 35c1aa1 commit ead6b4f
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 518 deletions.
36 changes: 36 additions & 0 deletions backend/capellacollab/core/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,39 @@ class SVGResponse(fastapi.responses.Response):
}
}
}


class ZIPFileResponse(fastapi.responses.StreamingResponse):
"""Custom error class for zip-file responses.
To use the class as response class, pass the following parameters
to the fastapi route definition.
```python
response_class=fastapi.responses.Response
responses=responses.ZIPFileResponse.responses
```
Don't use ZIPFileResponse as response_class as this will also change the
media type for all error responses, see:
https://github.com/tiangolo/fastapi/discussions/6799
To return an ZIP-file response in the route, use:
```python
return responses.ZIPFileResponse(
file_generator(),
)
```
"""

media_type = "application/zip"
responses: dict[int | str, dict[str, t.Any]] | None = {
200: {
"content": {
"application/zip": {
"schema": {"type": "string", "format": "binary"}
}
}
}
}
24 changes: 11 additions & 13 deletions backend/capellacollab/sessions/files/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fastapi
from fastapi import responses

from capellacollab.core import responses as core_responses
from capellacollab.sessions import injectables as sessions_injectables
from capellacollab.sessions import models as sessions_models
from capellacollab.sessions import operators
Expand All @@ -35,7 +36,7 @@ def list_files(
raise exceptions.SessionFileLoadingFailedError()


@router.post("")
@router.post("", status_code=204)
def upload_files(
files: list[fastapi.UploadFile],
session: sessions_models.DatabaseSession = fastapi.Depends(
Expand All @@ -55,7 +56,6 @@ def upload_files(
file.file.seek(0)

assert file.filename
file.filename = file.filename.replace(" ", "_")
tar.addfile(
tar.gettarinfo(arcname=file.filename, fileobj=file.file),
fileobj=file.file,
Expand All @@ -66,20 +66,18 @@ def upload_files(

operators.get_operator().upload_files(session.id, tar_bytes)

return {"message": "Upload successful"}


@router.get("/download", response_class=responses.StreamingResponse)
@router.get(
"/download",
response_class=responses.StreamingResponse,
responses=core_responses.ZIPFileResponse.responses,
)
def download_file(
filename: str,
path: str,
session: sessions_models.DatabaseSession = fastapi.Depends(
sessions_injectables.get_existing_session
),
) -> responses.StreamingResponse:
return responses.StreamingResponse(
operators.get_operator().download_file(session.id, filename),
headers={
"content-disposition": 'attachment; filename=f"{filename}.zip"',
"content-type": "application/zip",
},
) -> core_responses.ZIPFileResponse:
return core_responses.ZIPFileResponse(
operators.get_operator().download_file(session.id, path),
)
4 changes: 2 additions & 2 deletions backend/capellacollab/sessions/operators/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,13 +1088,13 @@ def upload_files(
)
raise

def download_file(self, _id: str, filename: str) -> t.Iterable[bytes]:
def download_file(self, _id: str, path: str) -> t.Iterable[bytes]:
pod_name = self._get_pod_name(_id)
try:
exec_command = [
"bash",
"-c",
f"zip -qr /tmp/archive.zip '{shlex.quote(filename)}' && base64 /tmp/archive.zip && rm -f /tmp/archive.zip",
f"zip -qr - {shlex.quote(path)} | base64",
]
stream = kubernetes.stream.stream(
self.v1_core.connect_get_namespaced_pod_exec,
Expand Down
34 changes: 12 additions & 22 deletions frontend/src/app/openapi/api/sessions.service.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 0 additions & 46 deletions frontend/src/app/services/load-files/load-files.service.ts

This file was deleted.

12 changes: 6 additions & 6 deletions frontend/src/app/sessions/service/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
SessionProvisioningRequest,
SessionsService,
SessionConnectionInformation,
FileTree,
} from 'src/app/openapi';
import { SessionHistoryService } from 'src/app/sessions/user-sessions-wrapper/create-sessions/create-session-history/session-history.service';

Expand Down Expand Up @@ -228,10 +229,9 @@ export interface SessionState {
success: boolean;
}

export interface PathNode {
path: string;
name: string;
type: 'file' | 'directory';
isNew: boolean;
export type PathNode = Omit<FileTree, 'children'> & {
isNew?: boolean;
isModified?: boolean;
isExpanded?: boolean;
children: PathNode[] | null;
}
};

This file was deleted.

Loading

0 comments on commit ead6b4f

Please sign in to comment.