Compare commits
4 Commits
ee0762b0b1
...
6327c8f878
| Author | SHA1 | Date |
|---|---|---|
|
|
6327c8f878 | |
|
|
9233f0224d | |
|
|
f82c7db414 | |
|
|
953a4b22f6 |
Binary file not shown.
Binary file not shown.
|
|
@ -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
|
||||||
39
website.py
39
website.py
|
|
@ -1,10 +1,13 @@
|
||||||
from fastapi import FastAPI, HTTPException, Request
|
from fastapi import FastAPI, HTTPException, Request
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
|
from utils.pictures_handle import get_pictures_info
|
||||||
import os
|
import os
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
app.mount("/static", StaticFiles(directory="pictures"), name="static")
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["http://localhost:5173"], # 允许的源列表
|
allow_origins=["http://localhost:5173"], # 允许的源列表
|
||||||
|
|
@ -36,22 +39,30 @@ async def get_menu():
|
||||||
|
|
||||||
@app.get("/api/get-photo-list")
|
@app.get("/api/get-photo-list")
|
||||||
async def get_photo_list():
|
async def get_photo_list():
|
||||||
photo_list = [
|
base_url = "http://127.0.0.1:5173/"
|
||||||
{"id": 1, "photo_name": "1", "photo_size": "1", "url": "/index.svg"},
|
dir = "pictures"
|
||||||
{"id": 2, "photo_name": "2", "photo_size": "1", "url": "/photo.svg"},
|
photo_list = get_pictures_info(dir)
|
||||||
{"id": 3, "photo_name": "3", "photo_size": "1", "url": "/videos.svg"},
|
for photo in photo_list:
|
||||||
{"id": 4, "photo_name": "4", "photo_size": "1", "url": "/about.svg"},
|
photo["photo_url"] = f"http://127.0.0.1:8000/static/{photo['photo_name']}"
|
||||||
{"id": 5, "photo_name": "5", "photo_size": "1", "url": "/index.svg"},
|
# print(photo_list)
|
||||||
{"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"}
|
|
||||||
]
|
|
||||||
return {
|
return {
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"msg": "ok",
|
"msg": "ok",
|
||||||
"data": photo_list
|
"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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue