diff --git a/website.py b/website.py index 29e0c0f..0a97edf 100644 --- a/website.py +++ b/website.py @@ -1,10 +1,13 @@ from fastapi import FastAPI, HTTPException, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse +from utils.pictures_handle import get_pictures_info import os +from fastapi.staticfiles import StaticFiles app = FastAPI() +app.mount("/static", StaticFiles(directory="pictures"), name="static") app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:5173"], # 允许的源列表 @@ -36,22 +39,30 @@ async def get_menu(): @app.get("/api/get-photo-list") async def get_photo_list(): - photo_list = [ - {"id": 1, "photo_name": "1", "photo_size": "1", "url": "/index.svg"}, - {"id": 2, "photo_name": "2", "photo_size": "1", "url": "/photo.svg"}, - {"id": 3, "photo_name": "3", "photo_size": "1", "url": "/videos.svg"}, - {"id": 4, "photo_name": "4", "photo_size": "1", "url": "/about.svg"}, - {"id": 5, "photo_name": "5", "photo_size": "1", "url": "/index.svg"}, - {"id": 6, "photo_name": "6", "photo_size": "1", "url": "/photo.svg"}, - {"id": 7, "photo_name": "7", "photo_size": "1", "url": "/videos.svg"}, - {"id": 8, "photo_name": "8", "photo_size": "1", "url": "/about.svg"}, - {"id": 9, "photo_name": "9", "photo_size": "1", "url": "/index.svg"}, - {"id": 10, "photo_name": "10", "photo_size": "1", "url": "/photo.svg"}, - {"id": 11, "photo_name": "11", "photo_size": "1", "url": "/videos.svg"}, - {"id": 12, "photo_name": "12", "photo_size": "1", "url": "/about.svg"} - ] + base_url = "http://127.0.0.1:5173/" + dir = "pictures" + photo_list = get_pictures_info(dir) + for photo in photo_list: + photo["photo_url"] = f"http://127.0.0.1:8000/static/{photo['photo_name']}" + # print(photo_list) return { "code": 0, "msg": "ok", "data": photo_list } + + +@app.get("/static/{photo_name}") +async def get_photo(photo_name: str,request: Request): + request_path = str(request.url) + dir = "pictures" + file_path = os.path.join(dir, photo_name) + if os.path.exists(file_path): + return FileResponse(file_path) + else: + print(f"ERROR: {request_path}") + return { + "code": -1, + "msg": "error", + "data": None + }