Fix SVG uploads, fix entire Upload model deleted

This commit is contained in:
miruka
2019-11-06 07:50:31 -04:00
parent ace79a169c
commit 4c15b7dc62
4 changed files with 41 additions and 8 deletions

View File

@@ -3,7 +3,7 @@ import html
import xml.etree.cElementTree as xml_etree # FIXME: bandit warning
from enum import Enum
from enum import auto as autostr
from typing import IO, Optional
from typing import IO, Tuple, Union
import filetype
@@ -26,7 +26,7 @@ def dict_update_recursive(dict1, dict2):
dict1[k] = dict2[k]
def is_svg(file: IO) -> bool:
def is_svg(file: Union[IO, bytes, str]) -> bool:
try:
_, element = next(xml_etree.iterparse(file, ("start",)))
return element.tag == "{http://www.w3.org/2000/svg}svg"
@@ -34,6 +34,22 @@ def is_svg(file: IO) -> bool:
return False
def svg_dimensions(file: Union[IO, bytes, str]) -> Tuple[int, int]:
attrs = xml_etree.parse(file).getroot().attrib
try:
width = round(float(attrs.get("width", attrs["viewBox"].split()[3])))
except (KeyError, IndexError, ValueError, TypeError):
width = 256
try:
height = round(float(attrs.get("height", attrs["viewBox"].split()[4])))
except (KeyError, IndexError, ValueError, TypeError):
height = 256
return (width, height)
def guess_mime(file: IO) -> str:
if is_svg(file):
return "image/svg+xml"