更新 pictures_handle.py
parent
8a16672573
commit
fc8976efba
|
|
@ -1,23 +1,34 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def get_pictures_info(directory):
|
def get_pictures_info(directory, page, page_size):
|
||||||
pictures = []
|
pictures = []
|
||||||
counter = 1
|
start_index = (page - 1) * page_size
|
||||||
|
end_index = start_index + page_size
|
||||||
|
counter = 0
|
||||||
|
|
||||||
for filename in os.listdir(directory):
|
for filename in os.listdir(directory):
|
||||||
# print(f"directory: {directory}")
|
|
||||||
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
||||||
|
if start_index <= counter < end_index:
|
||||||
file_path = os.path.join(directory, filename)
|
file_path = os.path.join(directory, filename)
|
||||||
size = os.path.getsize(file_path)
|
size = os.path.getsize(file_path)
|
||||||
size_mb = size / (1024 * 1024)
|
size_mb = size / (1024 * 1024)
|
||||||
photo_name = filename
|
|
||||||
picture_info = {
|
picture_info = {
|
||||||
"id": counter,
|
"id": counter + 1,
|
||||||
"photo_name": photo_name,
|
"photo_name": filename,
|
||||||
"photo_size": round(size_mb, 2),
|
"photo_size": round(size_mb, 2),
|
||||||
"photo_url": file_path
|
"photo_url": file_path
|
||||||
}
|
}
|
||||||
pictures.append(picture_info)
|
pictures.append(picture_info)
|
||||||
counter += 1
|
counter += 1
|
||||||
|
if counter >= end_index:
|
||||||
|
break
|
||||||
return pictures
|
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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue