diff --git a/utils/pictures_handle.py b/utils/pictures_handle.py index c4ffa4c..0f876e5 100644 --- a/utils/pictures_handle.py +++ b/utils/pictures_handle.py @@ -1,23 +1,34 @@ import os -def get_pictures_info(directory): +def get_pictures_info(directory, page, page_size): pictures = [] - counter = 1 + start_index = (page - 1) * page_size + end_index = start_index + page_size + counter = 0 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) + if start_index <= counter < end_index: + file_path = os.path.join(directory, filename) + size = os.path.getsize(file_path) + size_mb = size / (1024 * 1024) + picture_info = { + "id": counter + 1, + "photo_name": filename, + "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