Skip to content

Commit

Permalink
feat: size restrictions by pixel and file size
Browse files Browse the repository at this point in the history
  • Loading branch information
ElJocho committed Oct 11, 2023
1 parent 6359b6f commit 7595546
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
4 changes: 4 additions & 0 deletions sketch_map_tool/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def load_config_default() -> Dict[str, str]:
"wms-layers": "heigit:osm-carto@2xx",
"wms-read-timeout": 600,
"max-nr-simultaneous-uploads": 100,
"max_single_file_size": 838860800, # 100mb
"max_pixel_per_image": 10e8, # 10.000*10.000
}


Expand All @@ -51,6 +53,8 @@ def load_config_from_env() -> Dict[str, str]:
"wms-layers": os.getenv("SMT-WMS-LAYERS"),
"wms-read-timeout": os.getenv("SMT-WMS-READ-TIMEOUT"),
"max-nr-simultaneous-uploads": os.getenv("SMT-MAX-NR-SIM-UPLOADS"),
"max_single_file_size": os.getenv("MAX-SINGLE-FILE-SIZE"), # 100mb
"max_pixel_per_image": os.getenv("MAX-PIXEL-PER-IMAGE"),
}
return {k: v for k, v in cfg.items() if v is not None}

Expand Down
11 changes: 8 additions & 3 deletions sketch_map_tool/database/client_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

from sketch_map_tool.config import get_config_value
from sketch_map_tool.definitions import REQUEST_TYPES
from sketch_map_tool.exceptions import FileNotFoundError_, UUIDNotFoundError
from sketch_map_tool.exceptions import (
FileNotFoundError_,
UUIDNotFoundError,
)
from sketch_map_tool.validators import validate_uploaded_sketchmap


def open_connection():
Expand All @@ -31,7 +35,7 @@ def _insert_id_map(uuid: str, map_: dict):
create_query = """
CREATE TABLE IF NOT EXISTS uuid_map(
uuid uuid PRIMARY KEY,
ts timestamptz;
ts timestamptz,
map json NOT NULL
)
"""
Expand Down Expand Up @@ -96,7 +100,8 @@ def insert_files(files) -> list[int]:
curs.execute(create_query)
ids = []
for file in files:
curs.execute(insert_query, (secure_filename(file.filename), file.read()))
content = validate_uploaded_sketchmap(file)
curs.execute(insert_query, (secure_filename(file.filename), content))
ids.append(curs.fetchone()[0])
return ids

Expand Down
37 changes: 34 additions & 3 deletions sketch_map_tool/validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import os
from io import BytesIO
from typing import get_args
from uuid import UUID

import PIL.Image as Image

from sketch_map_tool import get_config_value
from sketch_map_tool.definitions import REQUEST_TYPES
from sketch_map_tool.exceptions import UploadLimitsExceededError
from sketch_map_tool.models import LiteratureReference


Expand All @@ -14,6 +20,28 @@ def validate_type(type_: REQUEST_TYPES):
)


def validate_uploaded_sketchmap(file):
max_single_file_size = int(get_config_value("max_single_file_size"))
max_pixel_per_image = int(get_config_value("max_pixel_per_image"))

file_length = file.seek(0, os.SEEK_END)
if file_length > max_single_file_size:
raise UploadLimitsExceededError(
f"You can only upload pictures "
f"up to a filesize of {max_single_file_size}."
)
file.seek(0, os.SEEK_SET)
content = file.read()
img = Image.open(BytesIO(content))
total_pxl_cnt = img.size[0] * img.size[1]
if total_pxl_cnt > max_pixel_per_image:
raise UploadLimitsExceededError(
f"You can only upload pictures up to "
f"a total pixel count of {max_pixel_per_image}."
)
return content


def validate_uuid(uuid: str):
"""validation function for endpoint parameter <uuid>"""
try:
Expand All @@ -26,13 +54,16 @@ def validate_literature_reference(literatur_reference: LiteratureReference):
"""Validate literatur reference to not include empty strings."""
if literatur_reference.citation == "":
raise ValueError(
"Literature reference JSON fields should not contain empty strings as values."
"Literature reference JSON fields "
"should not contain empty strings as values."
)
if literatur_reference.img_src == "":
raise ValueError(
"Literature reference JSON fields should not contain empty strings as values."
"Literature reference JSON fields should "
"not contain empty strings as values."
)
if literatur_reference.url == "":
raise ValueError(
"Literature reference JSON fields should not contain empty strings as values."
"Literature reference JSON fields should "
"not contain empty strings as values."
)
16 changes: 15 additions & 1 deletion tests/integration/test_database_client_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from psycopg2.extensions import connection

from sketch_map_tool.database import client_flask
from sketch_map_tool.exceptions import FileNotFoundError_
from sketch_map_tool.exceptions import FileNotFoundError_, UploadLimitsExceededError


def test_open_close_connection(flask_app):
Expand Down Expand Up @@ -62,6 +62,20 @@ def test_insert_files(flask_app, files):
client_flask.delete_file(i)


def test_insert_too_large_files(flask_app, files, monkeypatch):
with flask_app.app_context():
with pytest.raises(UploadLimitsExceededError):
monkeypatch.setenv("MAX-SINGLE-FILE-SIZE", "10")
client_flask.insert_files(files)


def test_insert_too_many_pixels_files(flask_app, files, monkeypatch):
with flask_app.app_context():
with pytest.raises(UploadLimitsExceededError):
monkeypatch.setenv("MAX-PIXEL-PER-IMAGE", "10")
client_flask.insert_files(files)


def test_delete_file(flask_app, files):
with flask_app.app_context():
ids = client_flask.insert_files(files)
Expand Down

0 comments on commit 7595546

Please sign in to comment.