✨ Files get deleted, if quiz gets deleted
This commit is contained in:
@@ -9,8 +9,9 @@ from fastapi.responses import JSONResponse
|
||||
import re
|
||||
|
||||
from classquiz.auth import get_current_user, get_current_user_optional
|
||||
from classquiz.config import redis, settings
|
||||
from classquiz.config import redis, settings, storage
|
||||
from classquiz.db.models import Quiz, QuizInput, User, PlayGame
|
||||
|
||||
settings = settings()
|
||||
|
||||
router = APIRouter()
|
||||
@@ -108,7 +109,17 @@ async def delete_quiz(quiz_id: str, user: User = Depends(get_current_user)):
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=400, detail="badly formed quiz id")
|
||||
quiz = await Quiz.objects.get_or_none(id=quiz_id, user_id=user.id)
|
||||
|
||||
if quiz is None:
|
||||
return JSONResponse(status_code=404, content={"detail": "quiz not found"})
|
||||
else:
|
||||
return await quiz.delete()
|
||||
pics_to_delete = []
|
||||
pic_name_regex = re.compile("^.*/(.{36}--.{36})$")
|
||||
for question in quiz.questions:
|
||||
try:
|
||||
if question["image"] is not None:
|
||||
if not str(question["image"]).startswith("https://i.imgur.com/"):
|
||||
pics_to_delete.append(pic_name_regex.match(question["image"]).group(1))
|
||||
except KeyError:
|
||||
pass
|
||||
await storage.delete(pics_to_delete)
|
||||
return await quiz.delete()
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from classquiz.config import settings
|
||||
from classquiz.config import settings, storage
|
||||
import re
|
||||
from classquiz.storage import Storage
|
||||
|
||||
settings = settings()
|
||||
|
||||
@@ -13,8 +12,6 @@ file_regex = r"^[a-z0-9]{8}-[a-z0-9-]{27}--[a-z0-9-]{36}$"
|
||||
|
||||
@router.get('/download/{file_name}')
|
||||
async def download_file(file_name: str):
|
||||
storage = Storage(backend=settings.storage_backend, deta_key=settings.deta_project_key,
|
||||
deta_id=settings.deta_project_id, storage_path=settings.storage_path)
|
||||
if not re.match(file_regex, file_name):
|
||||
raise HTTPException(status_code=400, detail="Invalid file name")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user