Compare commits
No commits in common. "6f93326ba00843e98aba61d510c02bfd4a11630b" and "6327c8f8780b21f94371e82c687a310d47428e80" have entirely different histories.
6f93326ba0
...
6327c8f878
Binary file not shown.
Binary file not shown.
|
|
@ -1,34 +1,23 @@
|
|||
import os
|
||||
|
||||
|
||||
def get_pictures_info(directory, page, page_size):
|
||||
def get_pictures_info(directory):
|
||||
pictures = []
|
||||
start_index = (page - 1) * page_size
|
||||
end_index = start_index + page_size
|
||||
counter = 0
|
||||
counter = 1
|
||||
|
||||
for filename in os.listdir(directory):
|
||||
# print(f"directory: {directory}")
|
||||
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
||||
if start_index <= counter < end_index:
|
||||
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 + 1,
|
||||
"photo_name": filename,
|
||||
"photo_size": round(size_mb, 2),
|
||||
"id": counter,
|
||||
"photo_name": photo_name,
|
||||
"photo_size": round(size_mb,2),
|
||||
"photo_url": file_path
|
||||
}
|
||||
pictures.append(picture_info)
|
||||
counter += 1
|
||||
if counter >= end_index:
|
||||
break
|
||||
return pictures
|
||||
|
||||
|
||||
def get_total_pages(directory, page_size):
|
||||
total_photos = 0
|
||||
for filename in os.listdir(directory):
|
||||
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
||||
total_photos += 1
|
||||
return (total_photos + page_size - 1) // page_size
|
||||
|
|
|
|||
13
website.py
13
website.py
|
|
@ -2,7 +2,6 @@ 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
|
||||
from utils.pictures_handle import get_total_pages
|
||||
import os
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
|
|
@ -39,17 +38,17 @@ async def get_menu():
|
|||
|
||||
|
||||
@app.get("/api/get-photo-list")
|
||||
async def get_photo_list(page: int = 1, page_size: int = 10):
|
||||
dir_path = "pictures"
|
||||
total_pages = get_total_pages(dir_path, page_size)
|
||||
photo_list = get_pictures_info(dir_path, page, page_size)
|
||||
async def get_photo_list():
|
||||
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,
|
||||
"total_pages": total_pages
|
||||
"data": photo_list
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue