forked from FrederikBaerentsen/BrickTracker
bricktracker
sql
views
admin
__init__.py
add.py
error.py
exceptions.py
index.py
instructions.py
login.py
minifigure.py
part.py
set.py
upload.py
wish.py
__init__.py
app.py
config.py
configuration.py
configuration_list.py
exceptions.py
fields.py
instructions.py
instructions_list.py
login.py
minifigure.py
minifigure_list.py
navbar.py
parser.py
part.py
part_list.py
rebrickable.py
rebrickable_image.py
rebrickable_minifigures.py
rebrickable_parts.py
rebrickable_set.py
rebrickable_set_list.py
record.py
record_list.py
reload.py
retired.py
retired_list.py
set.py
set_checkbox.py
set_checkbox_list.py
set_list.py
socket.py
sql.py
sql_counter.py
sql_migration.py
sql_migration_list.py
sql_stats.py
theme.py
theme_list.py
version.py
wish.py
wish_list.py
docs
static
templates
.dockerignore
.env.sample
.gitignore
CHANGELOG.md
Dockerfile
LICENSE
README.md
__init__.py
app.py
compose.legacy.yml
compose.local.yaml
compose.yaml
entrypoint.sh
requirements.txt
test-server.sh
40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
import os
|
|
|
|
from flask import redirect, request, url_for
|
|
from werkzeug.datastructures import FileStorage
|
|
from werkzeug.wrappers.response import Response
|
|
|
|
from ..exceptions import ErrorException
|
|
|
|
|
|
# Helper for a standard file upload process
|
|
def upload_helper(
|
|
name: str,
|
|
endpoint: str,
|
|
/,
|
|
*,
|
|
extensions: list[str] = [],
|
|
) -> FileStorage | Response:
|
|
# Bogus submit
|
|
if name not in request.files:
|
|
return redirect(url_for(endpoint))
|
|
|
|
file = request.files[name]
|
|
|
|
# Empty submit
|
|
if not file or file.filename is None or file.filename == '':
|
|
return redirect(url_for(endpoint, empty_file=True))
|
|
|
|
# Not allowed extension
|
|
# Security: not really
|
|
if len(extensions):
|
|
_, extension = os.path.splitext(file.filename)
|
|
|
|
if extension not in extensions:
|
|
raise ErrorException('{file} extension is not an allowed. Expected: {allowed}'.format( # noqa: E501
|
|
file=file.filename,
|
|
allowed=', '.join(extensions)
|
|
))
|
|
|
|
return file
|