From 07e2ef879c4bc4d929dc808bc1da037f7bf03e4e Mon Sep 17 00:00:00 2001 From: Jojo Ortiz Date: Thu, 5 Oct 2023 15:05:17 -0700 Subject: [PATCH 1/4] add /file_exists/ route to check if file exists --- pykoi/application.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pykoi/application.py b/pykoi/application.py index bc69c51..44dd8f0 100644 --- a/pykoi/application.py +++ b/pykoi/application.py @@ -731,6 +731,18 @@ async def get_components( ] ) + @app.get("/file_exists/") + async def check_file_exists( + file_name: str, + user: Union[None, UserInDB] = Depends(self.get_auth_dependency()), + ): + try: + file_path = f"{os.getcwd()}/{file_name}" + file_exists = os.path.exists(file_path) + return {"log": f"Check if {file_name} exists succeeded.", "file_exists": file_exists, "status": "200"} + except Exception as ex: + return {"log": f"Check if {file_name} exists failed: {ex}", "status": "500"} + def create_data_route(id: str, data_source: Any): """ Create data route for the application. From b188ea1d09ad42998a363ddfcb6ded4b4f3cfbf8 Mon Sep 17 00:00:00 2001 From: Jojo Ortiz Date: Thu, 5 Oct 2023 15:06:22 -0700 Subject: [PATCH 2/4] add OVERWRITE download state for checking if file overwrite ok --- .../Chatbots/Components/DownloadModal.svelte | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/pykoi/frontend/src/lib/Chatbots/Components/DownloadModal.svelte b/pykoi/frontend/src/lib/Chatbots/Components/DownloadModal.svelte index 1230cef..959f17e 100644 --- a/pykoi/frontend/src/lib/Chatbots/Components/DownloadModal.svelte +++ b/pykoi/frontend/src/lib/Chatbots/Components/DownloadModal.svelte @@ -8,11 +8,11 @@ FILE_INPUT: 0, DOWNLOADED: 1, FAILED_DOWNLOAD: 2, + OVERWRITE: 3, }; let downloadState = DOWNLOAD_STATE.FILE_INPUT; - const handleSubmit = async (e) => { - e.preventDefault(); + const saveToCSV = async (file_name) => { const request_body = { file_name, }; @@ -34,6 +34,19 @@ } }; + const handleSubmit = async (e) => { + e.preventDefault(); + const response = await fetch( + `/file_exists/?file_name=${file_name}.csv` + ); + const file_exists_response = await response.json(); + if (file_exists_response.file_exists === true) { + downloadState = DOWNLOAD_STATE.OVERWRITE; + } else { + saveToCSV(file_name); + } + }; + function handleClose() { showModal = false; downloadState = DOWNLOAD_STATE.FILE_INPUT; @@ -62,7 +75,7 @@ {/if} {#if downloadState === DOWNLOAD_STATE.DOWNLOADED}
- ✅ Data downloaded to ~/pykoi/{file_name}.csv + ✅ Data downloaded to /pykoi/{file_name}.csv
@@ -71,10 +84,19 @@ {#if downloadState === DOWNLOAD_STATE.FAILED_DOWNLOAD}
⚠️ Download failed. Please try again.
- +
{/if} + {#if downloadState === DOWNLOAD_STATE.OVERWRITE} +
+ ⚠️ {file_name}.csv already exists. Do you wish to overwrite it? +
+
+ + +
+ {/if}