Limits implemented

This commit is contained in:
Profitroll 2023-01-19 14:11:49 +01:00
parent 84be0c7154
commit 02552dce5a
3 changed files with 27 additions and 3 deletions

View File

@ -10,6 +10,10 @@
"data": "data"
},
"compression": 5,
"limits": {
"saves": -1,
"devices": -1
},
"messages": {
"key_expired": "API key expired",
"key_invalid": "Invalid API key",

View File

@ -3,7 +3,9 @@ from modules.database import col_devices, col_saves
from fastapi import HTTPException, Depends
from fastapi.responses import UJSONResponse, Response
from fastapi.openapi.models import APIKey
from starlette.status import HTTP_204_NO_CONTENT, HTTP_404_NOT_FOUND, HTTP_409_CONFLICT
from starlette.status import HTTP_204_NO_CONTENT, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND, HTTP_409_CONFLICT
from modules.utils import configGet
@app.get("/devices", response_class=UJSONResponse, description="Get all devices")
async def devices_get(apikey: APIKey = Depends(get_api_key)):
@ -48,6 +50,15 @@ async def devices_post(name: str, os: str, apikey: APIKey = Depends(get_api_key)
user = user_by_key(apikey)
if col_devices.count_documents({"user": user}) >= configGet("devices", "limits"):
return UJSONResponse(
{
"detail": f'Too many devices. This instance allows to register only {configGet("devices", "limits")} devices per user',
"limit": configGet("devices", "limits")
},
status_code=HTTP_403_FORBIDDEN
)
if col_devices.find_one({"user": user, "name": name}) is not None:
raise HTTPException(HTTP_409_CONFLICT, detail="Device with this name already exists.")

View File

@ -9,9 +9,9 @@ from modules.database import col_devices, col_saves
from fastapi import HTTPException, Depends, UploadFile
from fastapi.responses import UJSONResponse, FileResponse, Response
from fastapi.openapi.models import APIKey
from starlette.status import HTTP_204_NO_CONTENT, HTTP_404_NOT_FOUND, HTTP_406_NOT_ACCEPTABLE
from starlette.status import HTTP_204_NO_CONTENT, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND, HTTP_406_NOT_ACCEPTABLE
from modules.utils import zip_saves
from modules.utils import configGet, zip_saves
@app.get("/saves", response_class=UJSONResponse, response_model=Dict[str, StardewSave], description="Get all available game saves")
@ -137,6 +137,15 @@ async def saves_post(device: str, files: List[UploadFile], apikey: APIKey = Depe
error_return = HTTPException(HTTP_406_NOT_ACCEPTABLE, detail="You must provide two files: save file and SaveGameInfo for that save")
if col_saves.count_documents({"user": user}) >= configGet("saves", "limits"):
return UJSONResponse(
{
"detail": f'Too many save files. This instance allows to store only {configGet("saves", "limits")} saves per user',
"limit": configGet("saves", "limits")
},
status_code=HTTP_403_FORBIDDEN
)
if len(files) != 2:
return error_return