Validate if requested svg icon is an svg
This commit is contained in:
parent
2b096baa88
commit
a659bbfffc
5 changed files with 36 additions and 3 deletions
4
icons/cart.svg
Normal file
4
icons/cart.svg
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?><!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M6.29977 5H21L19 12H7.37671M20 16H8L6 3H3M9 20C9 20.5523 8.55228 21 8 21C7.44772 21 7 20.5523 7 20C7 19.4477 7.44772 19 8 19C8.55228 19 9 19.4477 9 20ZM20 20C20 20.5523 19.5523 21 19 21C18.4477 21 18 20.5523 18 20C18 19.4477 18.4477 19 19 19C19.5523 19 20 19.4477 20 20Z" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 594 B |
|
|
@ -1,7 +1,7 @@
|
|||
from src.classes import product_classes
|
||||
|
||||
pizza = product_classes.Product(name="Pizza", price=3.0, barcode=1000 , image_filename="pizza.svg")
|
||||
banana = product_classes.Product(name="Banana", price=1.0, barcode=1001 ,image_filename="banana.svg")
|
||||
pizza = product_classes.Product(name="Pizza", price=3.0, barcode=1000 , image_filename="pizza")
|
||||
banana = product_classes.Product(name="Banana", price=1.0, barcode=1001 ,image_filename="banana")
|
||||
|
||||
product_list = [pizza, banana]
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
import os
|
||||
|
||||
from src.classes import product_classes
|
||||
from src.config import definitions
|
||||
from validators import image_validator
|
||||
from src import data_connection
|
||||
|
||||
app = FastAPI()
|
||||
|
|
@ -20,6 +22,7 @@ app.add_middleware(
|
|||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
|
@ -34,5 +37,8 @@ def get_single_product(barcode: int):
|
|||
|
||||
@app.get("/icons/{icon_filename}", response_class=FileResponse)
|
||||
def get_icon(icon_filename: str):
|
||||
full_filepath = f"{definitions.ICONS_PATH}/{icon_filename}"
|
||||
full_filepath = os.path.join(definitions.ICONS_PATH, icon_filename) + ".svg"
|
||||
assert os.path.exists(full_filepath), f"File {full_filepath} not found"
|
||||
assert image_validator.is_valid_svg(full_filepath), f"File {full_filepath} is not a valid image"
|
||||
|
||||
return full_filepath
|
||||
0
validators/__init__.py
Normal file
0
validators/__init__.py
Normal file
23
validators/image_validator.py
Normal file
23
validators/image_validator.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import re
|
||||
from PIL import Image
|
||||
|
||||
def is_valid_image(file_name):
|
||||
try:
|
||||
with Image.open(file_name) as img:
|
||||
img.verify()
|
||||
return True
|
||||
except (IOError, SyntaxError):
|
||||
return False
|
||||
|
||||
|
||||
def is_valid_svg(file_name):
|
||||
SVG_R = r'(?:<\?xml\b[^>]*>[^<]*)?(?:<!--.*?-->[^<]*)*(?:<svg|<!DOCTYPE svg)\b'
|
||||
SVG_RE = re.compile(SVG_R, re.DOTALL)
|
||||
|
||||
file_object = open(file_name, 'r')
|
||||
|
||||
file_contents = file_object.read()
|
||||
|
||||
is_svg = SVG_RE.match(file_contents) is not None
|
||||
|
||||
return is_svg
|
||||
Loading…
Reference in a new issue