aboutsummaryrefslogtreecommitdiff
path: root/qc_app/files.py
blob: 6485b278121c46a8db1793fcf27ee0df554cb3b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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