about summary refs log tree commit diff
path: root/qc_app/files.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-12-07 16:29:05 +0300
committerFrederick Muriuki Muriithi2023-12-07 16:29:05 +0300
commit49b67d306147ab21f91aaf00831d5b36e28585f6 (patch)
treea6c423ea3a13528ba995f9d791968dd901654b7e /qc_app/files.py
parent8760d188faebe552028f3142a821d0851df4e1e9 (diff)
downloadgn-uploader-49b67d306147ab21f91aaf00831d5b36e28585f6.tar.gz
Samples: Read an save data to db.
Diffstat (limited to 'qc_app/files.py')
-rw-r--r--qc_app/files.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/qc_app/files.py b/qc_app/files.py
new file mode 100644
index 0000000..6485b27
--- /dev/null
+++ b/qc_app/files.py
@@ -0,0 +1,20 @@
+"""Utilities to deal with uploaded files."""
+from pathlib import Path
+from typing import Union
+
+from werkzeug.utils import secure_filename
+from flask import (
+    request,
+    current_app as app)
+
+def save_file(key: str, upload_dir: Path) -> Union[Path, bool]:
+    """Save the uploaded file and return the path."""
+    if not bool(request.files.get(key)):
+        return False
+    filename = Path(secure_filename(request.files[key].filename))
+    if not upload_dir.exists():
+        upload_dir.mkdir()
+
+    filepath = Path(upload_dir, filename)
+    request.files["samples_file"].save(filepath)
+    return filepath