fix 500 error when not b64 data
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: Hipstercat <tasty@hipstercat.fr>
This commit is contained in:
parent
6440ecf73b
commit
5f0333c0d2
@ -3,6 +3,7 @@ import io
|
|||||||
import os.path
|
import os.path
|
||||||
import pathlib
|
import pathlib
|
||||||
import zipfile
|
import zipfile
|
||||||
|
import binascii
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from fastapi import APIRouter, HTTPException, Query
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
from ..schemas import *
|
from ..schemas import *
|
||||||
@ -42,26 +43,31 @@ async def get_map_by_crc(human_crc: Optional[str] = Query(None, min_length=8, ma
|
|||||||
|
|
||||||
@router.post("/maps", tags=["maps"], response_model=MapOut)
|
@router.post("/maps", tags=["maps"], response_model=MapOut)
|
||||||
async def upload_map(map_in: MapIn):
|
async def upload_map(map_in: MapIn):
|
||||||
allowed_name = re.compile("[a-zA-Z-_.\d\(\)\[\] ]+.gck")
|
allowed_name = re.compile(r"[a-zA-Z-_.\d()\[\] ]+.gck")
|
||||||
filename = map_in.name
|
filename = map_in.name
|
||||||
if not allowed_name.fullmatch(filename):
|
if not allowed_name.fullmatch(filename):
|
||||||
raise HTTPException(status_code=400, detail="Invalid filename")
|
raise HTTPException(status_code=400, detail="Invalid filename")
|
||||||
|
|
||||||
|
try:
|
||||||
map_bytes = base64.b64decode(map_in.b64_data.encode("utf8"))
|
map_bytes = base64.b64decode(map_in.b64_data.encode("utf8"))
|
||||||
if len(map_bytes) > MAX_UPLOAD_SIZE:
|
except binascii.Error:
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid base64 data")
|
||||||
|
|
||||||
|
if len(map_in.b64_data) > MAX_UPLOAD_SIZE:
|
||||||
raise HTTPException(status_code=400, detail="File too big")
|
raise HTTPException(status_code=400, detail="File too big")
|
||||||
|
|
||||||
|
crc = crc32(map_bytes)
|
||||||
|
for existing_map in MAPS:
|
||||||
|
if existing_map["crc"] == crc:
|
||||||
|
return MapOut(**existing_map)
|
||||||
|
|
||||||
map_io = io.BytesIO(map_bytes)
|
map_io = io.BytesIO(map_bytes)
|
||||||
try:
|
try:
|
||||||
zipfile.ZipFile(map_io)
|
zipfile.ZipFile(map_io)
|
||||||
except zipfile.BadZipfile:
|
except zipfile.BadZipfile:
|
||||||
raise HTTPException(status_code=400, detail="File is not a valid map")
|
raise HTTPException(status_code=400, detail="File is not a valid map")
|
||||||
|
|
||||||
crc = crc32(map_bytes)
|
# upload that map
|
||||||
existing_map = [gmap for gmap in MAPS if gmap["crc"] == crc]
|
|
||||||
if existing_map:
|
|
||||||
return MapOut(**existing_map[0])
|
|
||||||
else:
|
|
||||||
uploaded_filename = filename
|
uploaded_filename = filename
|
||||||
i = 0
|
i = 0
|
||||||
while os.path.exists(f"{config['upload_path']}{uploaded_filename}"):
|
while os.path.exists(f"{config['upload_path']}{uploaded_filename}"):
|
||||||
|
Loading…
Reference in New Issue
Block a user