Skip to content

Commit

Permalink
feat: add download archive button
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopr committed Jun 7, 2023
1 parent 22f02a1 commit 6603ed7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
28 changes: 20 additions & 8 deletions apps/client/src/components/dynamics/my-dynamics/heading.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FileDown, RefreshCw } from "lucide-react";
import { useRouter } from "next/router";
import { Archive, FileDown, RefreshCw } from "lucide-react";
import NextLink from "next/link";
import useTranslation from "next-translate/useTranslation";

import { Button } from "@app/components/general/buttons";
Expand All @@ -20,16 +20,28 @@ export function MyDynamicsHeader({
isRefetching
}: MyDynamicsHeader) {
const { t } = useTranslation();
const router = useRouter();

return (
<div className="flex flex-col gap-3 md:flex-row">
<Button
LeftIcon={FileDown}
onClick={() => router.push("/api/downloads/mdp")}
<NextLink
href="/api/downloads/mdp"
target="_blank"
rel="noopener noreferrer"
>
{t("my-dynamics:downloads.mdp")}
</Button>
<Button LeftIcon={FileDown}>{t("my-dynamics:downloads.mdp")}</Button>
</NextLink>
<NextLink
href="/api/downloads/archive"
target="_blank"
rel="noopener noreferrer"
>
<Button
LeftIcon={Archive}
title={t("my-dynamics:downloads.archive-info")}
>
{t("my-dynamics:downloads.archive")}
</Button>
</NextLink>
<div className="flex flex-1">
<TextButton
iconClassName={cnMerge({
Expand Down
23 changes: 23 additions & 0 deletions apps/client/src/pages/api/downloads/archive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";

import { api } from "@app/lib/api";
import { authOptions } from "@app/pages/api/auth/[...nextauth]";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = await getServerSession(req, res, authOptions);

if (req.method === "GET") {
if (session && session.user) {
return res.redirect(
302,
`${api.defaults.baseURL}/downloads/archive?username=${session.user.username}`
);
}

return res;
}
}
2 changes: 2 additions & 0 deletions apps/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from server.config import Config
from server.resources.celery.active_tasks import CeleryActiveTasks
from server.resources.celery.reserved_tasks import CeleryReservedTasks
from server.resources.downloads.archive import DownloadDynamicArchive
from server.resources.downloads.commands import DownloadDynamicCommands
from server.resources.downloads.figures import DownloadDynamicFigures
from server.resources.downloads.log import DownloadDynamicLog
Expand Down Expand Up @@ -41,6 +42,7 @@
api.add_resource(DownloadDynamicLog, "/api/v1/downloads/log")
api.add_resource(DownloadDynamicResults, "/api/v1/downloads/results")
api.add_resource(DownloadMDP, "/api/v1/downloads/mdp")
api.add_resource(DownloadDynamicArchive, "/api/v1/downloads/archive")

# API status
api.add_resource(Health, "/api/v1/health")
Expand Down
37 changes: 37 additions & 0 deletions apps/server/server/resources/downloads/archive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from io import BytesIO
import os
from flask_restful import Resource
from flask import request, send_file
from server.config import Config


class DownloadDynamicArchive(Resource):
def get(self):
args = request.args

username = args["username"]

folder_vdfiles = os.path.abspath(Config.UPLOAD_FOLDER)
file_archive = os.path.abspath(
os.path.join(folder_vdfiles, username, "archive.zip")
)

download_filename = f"{username}-archive.zip"

if not os.path.exists(file_archive):
buffer = BytesIO()
file_text = (
f"The user {username} doesn't have any dynamics on the old system."
)
buffer.write(file_text.encode("utf-8"))
buffer.seek(0)
return send_file(
buffer,
as_attachment=True,
download_name=f"{username}-no-archive.txt",
mimetype="text/txt",
)

return send_file(
file_archive, as_attachment=True, download_name=download_filename
)

0 comments on commit 6603ed7

Please sign in to comment.