Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Rewrite major parts of the file browser #1873

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@

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(

Check warning on line 81 in backend/capellacollab/sessions/files/routes.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/sessions/files/routes.py#L81

Added line #L81 was not covered by tests
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 @@ -1085,13 +1085,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 @@ -223,10 +224,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
Loading