🚧 Some progress
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from classquiz.db.models import Quiz, User
|
from classquiz.db.models import Quiz, User
|
||||||
import xlsxwriter
|
import xlsxwriter
|
||||||
@@ -107,10 +108,11 @@ async def meilisearch_init():
|
|||||||
print("Finished MeiliSearch synchronisation")
|
print("Finished MeiliSearch synchronisation")
|
||||||
|
|
||||||
|
|
||||||
def check_hashcash(data: str, input_data: str) -> bool:
|
def check_hashcash(data: str, input_data: str, claim_in: Optional[str] = "19") -> bool:
|
||||||
"""
|
"""
|
||||||
It checks that the hashcash is valid, and if it is, it returns True
|
It checks that the hashcash is valid, and if it is, it returns True
|
||||||
|
|
||||||
|
:param claim_in: The claim the data should have (bytes)
|
||||||
:param data: The hashcash string
|
:param data: The hashcash string
|
||||||
:type data: str
|
:type data: str
|
||||||
:param input_data: The claim, the data the server provided
|
:param input_data: The claim, the data the server provided
|
||||||
@@ -126,7 +128,7 @@ def check_hashcash(data: str, input_data: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
assert version == "1"
|
assert version == "1"
|
||||||
assert claim == "19"
|
assert claim == claim_in
|
||||||
assert res == input_data
|
assert res == input_data
|
||||||
assert ext == ""
|
assert ext == ""
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ async def get_pow_data(edit_id: str):
|
|||||||
|
|
||||||
class UploadImageReturn(BaseModel):
|
class UploadImageReturn(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
|
pow_data: str
|
||||||
|
|
||||||
|
|
||||||
@router.post("/image", response_model=UploadImageReturn)
|
@router.post("/image", response_model=UploadImageReturn)
|
||||||
@@ -86,22 +87,28 @@ async def upload_image(edit_id: str, pow_data: str, file: UploadFile = File()):
|
|||||||
print(pow_data)
|
print(pow_data)
|
||||||
session_data = await redis.get(f"edit_session:{edit_id}")
|
session_data = await redis.get(f"edit_session:{edit_id}")
|
||||||
pow_data_server = await redis.get(f"edit_session:{edit_id}:pow")
|
pow_data_server = await redis.get(f"edit_session:{edit_id}:pow")
|
||||||
|
uploaded_images = await redis.llen(f"edit_session:{edit_id}:images")
|
||||||
if pow_data_server is None:
|
if pow_data_server is None:
|
||||||
print("1")
|
|
||||||
raise HTTPException(status_code=401, detail="Edit ID not found!")
|
|
||||||
if not check_hashcash(pow_data, pow_data_server):
|
|
||||||
print("2")
|
|
||||||
raise HTTPException(status_code=401, detail="Edit ID not found!")
|
raise HTTPException(status_code=401, detail="Edit ID not found!")
|
||||||
if session_data is None:
|
if session_data is None:
|
||||||
raise HTTPException(status_code=401, detail="Edit ID not found!")
|
raise HTTPException(status_code=401, detail="Edit ID not found!")
|
||||||
|
|
||||||
|
if uploaded_images == 0 and not check_hashcash(pow_data, pow_data_server, "19"):
|
||||||
|
raise HTTPException(status_code=401, detail="Edit ID not found!")
|
||||||
|
if uploaded_images != 0 and not check_hashcash(pow_data, pow_data_server, "17"):
|
||||||
|
raise HTTPException(status_code=401, detail="Edit ID not found!")
|
||||||
|
|
||||||
file_bytes = await file.read()
|
file_bytes = await file.read()
|
||||||
lol = puremagic.magic_string(file_bytes)
|
pm_data = puremagic.magic_string(file_bytes)[0]
|
||||||
print(lol)
|
if pm_data.extension not in allowed_image_extensions:
|
||||||
|
raise HTTPException(status_code=400, detail="Image-type now allowed!")
|
||||||
session_data = EditSessionData.parse_raw(session_data)
|
session_data = EditSessionData.parse_raw(session_data)
|
||||||
file_name = f"{session_data.quiz_id}--{uuid.uuid4()}"
|
file_name = f"{session_data.quiz_id}--{uuid.uuid4()}"
|
||||||
# await storage.upload(file_name=file_name, file_data=file_bytes)
|
# await storage.upload(file_name=file_name, file_data=file_bytes) TODO: UNCOMMENT!!!!
|
||||||
# await redis.lpush(f"edit_session:{edit_id}:images", file_name)
|
await redis.lpush(f"edit_session:{edit_id}:images", file_name)
|
||||||
return UploadImageReturn(id=file_name)
|
random_str = os.urandom(8).hex()
|
||||||
|
await redis.set(f"edit_session:{edit_id}:pow", random_str, ex=3800)
|
||||||
|
return UploadImageReturn(id=file_name, pow_data=random_str)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/finish")
|
@router.post("/finish")
|
||||||
|
|||||||
Reference in New Issue
Block a user