aboutsummaryrefslogtreecommitdiff
path: root/qc_app/files.py
blob: 03042963522a99d5161d42c2aed81cd605758879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""Utilities to deal with uploaded files."""
from pathlib import Path
from typing import Union

from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage

def save_file(fileobj: FileStorage, upload_dir: Path) -> Union[Path, bool]:
    """Save the uploaded file and return the path."""
    if not bool(fileobj):
        return False
    filename = Path(secure_filename(fileobj.filename)) # type: ignore[arg-type]
    if not upload_dir.exists():
        upload_dir.mkdir()

    filepath = Path(upload_dir, filename)
    fileobj.save(filepath)
    return filepath