Skip to content

Commit

Permalink
feature: download all files from submission
Browse files Browse the repository at this point in the history
  • Loading branch information
xerbalind committed May 16, 2024
1 parent a018aeb commit 789d8fc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions backend/src/submission/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Sequence

from fastapi import APIRouter, Depends, BackgroundTasks
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, StreamingResponse
from sqlalchemy.ext.asyncio import AsyncSession

from src.dependencies import get_async_db
Expand All @@ -15,7 +15,7 @@
)
from src.submission.exceptions import FileNotFound
from src.submission.exceptions import FilesNotFound
from src.submission.utils import upload_files, remove_files
from src.submission.utils import upload_files, remove_files, zip_stream
from src.user.dependencies import admin_user_validation, get_authenticated_user
from src.user.schemas import User
from . import service
Expand Down Expand Up @@ -94,6 +94,12 @@ async def get_file(path: str, submission: Submission = Depends(retrieve_submissi
return FileResponse(path=path)


@router.get("/{submission_id}/zip", response_class=StreamingResponse)
async def get_all_files(submission: Submission = Depends(retrieve_submission)):
path = submission_path(submission.files_uuid, "")
return StreamingResponse(zip_stream(path, submission.group_id), media_type="application/zip")


@router.get("/{submission_id}/artifacts", response_model=list[File])
async def get_artifacts(submission: Submission = Depends(retrieve_submission)):
if submission.status == Status.InProgress:
Expand Down
13 changes: 13 additions & 0 deletions backend/src/submission/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import shutil
import zipfile
import pathlib
import io
import fnmatch
from uuid import uuid4

Expand Down Expand Up @@ -45,5 +47,16 @@ def upload_files(files: list[UploadFile], project: Project) -> str:
return uuid


def zip_stream(path, group_id: int):
base_path = pathlib.Path(path)
data = io.BytesIO()
with zipfile.ZipFile(data, mode='w') as z:
for f_name in base_path.iterdir():
name = f"group_{group_id}/{str(f_name).replace(path, "")}"
z.write(f_name, arcname=name)
data.seek(0)
yield from data


def remove_files(uuid: str):
shutil.rmtree(submissions_path(uuid))
12 changes: 12 additions & 0 deletions frontend/src/components/submission/SubmissionCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
</v-col>
</v-skeleton-loader>
</v-container>
<v-card-actions>
<v-btn @click="downloadAll">
{{ $t("submission.download_all_files") }}
</v-btn>
</v-card-actions>
</v-card-item>
</v-card>
</template>
Expand Down Expand Up @@ -91,6 +96,13 @@ const downloadFile = (index: number) => {
const file = files.value![index];
download_file(`/api/submissions/${submission.value!.id}/files/${file.filename}`, file.filename);
};
const downloadAll = () => {
download_file(
`/api/submissions/${submission.value!.id}/zip`,
`submission_group_${submission.value?.group_id}`
);
};
</script>

<style scoped>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default {
teacher_submissions_info:
"This page contains a list of the latest submission of each group for this project.",
docker_test: "Tests Output",
download_all_files: "Download all files",
},
project: {
deadline: "Deadline",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default {
teacher_submissions_info:
"Deze pagina bevat een lijst van de laatste indiening van elke groep voor dit project.",
docker_test: "Testen Output",
download_all_files: "Download alle bestanden",
},
project: {
deadline: "Deadline",
Expand Down

0 comments on commit 789d8fc

Please sign in to comment.