Compare commits
No commits in common. "6327c8f8780b21f94371e82c687a310d47428e80" and "ee0762b0b1d7a85e7b5a109805e9d8a59f922170" have entirely different histories.
6327c8f878
...
ee0762b0b1
Binary file not shown.
Binary file not shown.
|
|
@ -1,23 +0,0 @@
|
||||||
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,13 +1,10 @@
|
||||||
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"], # 允许的源列表
|
||||||
|
|
@ -39,30 +36,22 @@ 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():
|
||||||
base_url = "http://127.0.0.1:5173/"
|
photo_list = [
|
||||||
dir = "pictures"
|
{"id": 1, "photo_name": "1", "photo_size": "1", "url": "/index.svg"},
|
||||||
photo_list = get_pictures_info(dir)
|
{"id": 2, "photo_name": "2", "photo_size": "1", "url": "/photo.svg"},
|
||||||
for photo in photo_list:
|
{"id": 3, "photo_name": "3", "photo_size": "1", "url": "/videos.svg"},
|
||||||
photo["photo_url"] = f"http://127.0.0.1:8000/static/{photo['photo_name']}"
|
{"id": 4, "photo_name": "4", "photo_size": "1", "url": "/about.svg"},
|
||||||
# print(photo_list)
|
{"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"}
|
||||||
|
]
|
||||||
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