fix 500 error when not b64 data
continuous-integration/drone/push Build is passing Details

Signed-off-by: Hipstercat <tasty@hipstercat.fr>
This commit is contained in:
Amazed 2022-09-01 20:02:09 +02:00
parent 6440ecf73b
commit 5f0333c0d2
Signed by: hipstercat
GPG Key ID: BF42C937290F5641
1 changed files with 25 additions and 19 deletions

View File

@ -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,34 +43,39 @@ 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")
map_bytes = base64.b64decode(map_in.b64_data.encode("utf8")) try:
if len(map_bytes) > MAX_UPLOAD_SIZE: map_bytes = base64.b64decode(map_in.b64_data.encode("utf8"))
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] uploaded_filename = filename
if existing_map: i = 0
return MapOut(**existing_map[0]) while os.path.exists(f"{config['upload_path']}{uploaded_filename}"):
else: file_wo_ext = ".".join(filename.split('.')[0:-1])
uploaded_filename = filename uploaded_filename = f"{file_wo_ext}-{i}.gck"
i = 0 i += 1
while os.path.exists(f"{config['upload_path']}{uploaded_filename}"): with open(f"{config['upload_path']}{uploaded_filename}", "wb") as fp:
file_wo_ext = ".".join(filename.split('.')[0:-1]) fp.write(map_bytes)
uploaded_filename = f"{file_wo_ext}-{i}.gck" gmap = map_file_to_dict(pathlib.Path(f"{config['upload_path']}{uploaded_filename}"))
i += 1 MAPS.append(gmap)
with open(f"{config['upload_path']}{uploaded_filename}", "wb") as fp: return MapOut(**gmap)
fp.write(map_bytes)
gmap = map_file_to_dict(pathlib.Path(f"{config['upload_path']}{uploaded_filename}"))
MAPS.append(gmap)
return MapOut(**gmap)