feat(nodes): generalise list_images route for all types

This commit is contained in:
psychedelicious 2023-04-04 14:57:52 +10:00
parent 4f3be53d55
commit daf1bc6b67
2 changed files with 13 additions and 12 deletions

View File

@ -15,7 +15,6 @@ from ..dependencies import ApiDependencies
images_router = APIRouter(prefix="/v1/images", tags=["images"])
@images_router.get("/{image_type}/{image_name}", operation_id="get_image")
async def get_image(
image_type: ImageType = Path(description="The type of image to get"),
@ -69,16 +68,17 @@ async def upload_image(file: UploadFile, request: Request):
)
@images_router.get(
"/uploads/",
operation_id="list_uploads",
"/",
operation_id="list_images",
responses={200: {"model": PaginatedResults[ImageField]}},
)
async def list_uploads(
page: int = Query(default=0, description="The page of uploads to get"),
per_page: int = Query(default=10, description="The number of uploads per page"),
async def list_images(
image_type: ImageType = Query(default=ImageType.RESULT, description="The type of images to get"),
page: int = Query(default=0, description="The page of images to get"),
per_page: int = Query(default=10, description="The number of images per page"),
) -> PaginatedResults[ImageField]:
"""Gets a list of uploads"""
"""Gets a list of images"""
result = ApiDependencies.invoker.services.images.list(
ImageType.UPLOAD, page, per_page
image_type, page, per_page
)
return result

View File

@ -77,18 +77,19 @@ class DiskImageStorage(ImageStorageBase):
self, image_type: ImageType, page: int = 0, per_page: int = 10
) -> PaginatedResults[ImageField]:
dir_path = os.path.join(self.__output_folder, image_type)
image_paths = glob(f"{dir_path}/*.png")
# just want the filenames
image_filenames = list(map(lambda i: os.path.basename(i), image_paths))
# we want to sort the images by timestamp, but we don't trust the filesystem
# we do have a timestamp in the filename: `{uuid}_{timestamp}.png`
image_paths = glob(f"{dir_path}/*.png")
sorted_paths = sorted(
# extract the timestamp as int and multiply -1 to reverse sorting
image_paths, key=lambda i: int(os.path.splitext(i)[0].split("_")[1]) * -1
image_filenames, key=lambda i: int(os.path.splitext(i)[0].split("_")[-1]) * -1
)
all_images = list(
# build ImageFields for every image path
map(lambda i: ImageField(image_type=image_type, image_name=i), sorted_paths)
)