Mia Cheng hai 1 ano
pai
achega
4377fc3fc0
Modificáronse 1 ficheiros con 31 adicións e 2 borrados
  1. 31 2
      app/api/classes.py

+ 31 - 2
app/api/classes.py

@@ -1,4 +1,8 @@
-from fastapi import APIRouter, Form, Depends, HTTPException
+from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile
+from fastapi.responses import FileResponse
+import os
+from random import randint
+import uuid
 from fastapi.security import OAuth2PasswordRequestForm
 from app.models.models import User
 from app.models.models import Class_list
@@ -205,6 +209,7 @@ async def get_class():
         return {"msg": "success", "code": 200, "classes": classes}
     except Exception as e:
         return {"msg": str(e), "code": 500}
+    
 @classes.get("/search_class_like")
 async def search_class_like(keyword: str):
     try:
@@ -234,7 +239,31 @@ Q(name__icontains=keyword) | Q(lecturer__icontains=keyword)
     except Exception as e:
         return {"msg": str(e), "code": 500}
 
-
+IMAGEDIR = "images/"
+
+@classes.post("/upload/")
+async def create_upload_file(file: UploadFile = File(...)):
+ 
+    file.filename = f"{uuid.uuid4()}.jpg"
+    contents = await file.read()
+ 
+    #save the file
+    with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
+        f.write(contents)
+ 
+    return {"filename": file.filename}
+ 
+ 
+@classes.get("/show/")
+async def read_random_file():
+ 
+    # get random file from the image directory
+    files = os.listdir(IMAGEDIR)
+    random_index = randint(0, len(files) - 1)
+ 
+    path = f"{IMAGEDIR}{files[random_index]}"
+     
+    return FileResponse(path)
 # @classes.post("/login")
 # async def login(data: OAuth2PasswordRequestForm = Depends()):
 #     username = data.username