Compare commits

...

4 Commits

Author SHA1 Message Date
wangsiyuan 6327c8f878 更新 website.py 2024-01-23 18:02:56 +08:00
wangsiyuan 9233f0224d 创建 pictures_handle.py 2024-01-23 18:02:54 +08:00
wangsiyuan f82c7db414 创建 pictures_handle.cpython-311.pyc 2024-01-23 18:02:51 +08:00
wangsiyuan 953a4b22f6 更新 website.cpython-311.pyc 2024-01-23 18:02:44 +08:00
4 changed files with 48 additions and 14 deletions

Binary file not shown.

Binary file not shown.

23
utils/pictures_handle.py Normal file
View File

@ -0,0 +1,23 @@
import os
def get_pictures_info(directory):
pictures = []
counter = 1
for filename in os.listdir(directory):
# print(f"directory: {directory}")
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
file_path = os.path.join(directory, filename)
size = os.path.getsize(file_path)
size_mb = size / (1024 * 1024)
photo_name = filename
picture_info = {
"id": counter,
"photo_name": photo_name,
"photo_size": round(size_mb,2),
"photo_url": file_path
}
pictures.append(picture_info)
counter += 1
return pictures

View File

@ -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
}