feat(nodes): list_uploads route wip

This commit is contained in:
psychedelicious 2023-04-04 11:05:15 +10:00
parent ed00afc64d
commit 77bf3c780f
2 changed files with 39 additions and 2 deletions

View File

@ -2,10 +2,12 @@
import io
from datetime import datetime, timezone
from fastapi import Path, Request, UploadFile
from fastapi import Path, Query, Request, UploadFile
from fastapi.responses import FileResponse, Response
from fastapi.routing import APIRouter
from PIL import Image
from invokeai.app.invocations.image import ImageField
from invokeai.app.services.item_storage import PaginatedResults
from ...services.image_storage import ImageType
from ..dependencies import ApiDependencies
@ -64,3 +66,18 @@ async def upload_image(file: UploadFile, request: Request):
)
},
)
@images_router.get(
"/uploads/",
operation_id="list_uploads",
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"),
) -> PaginatedResults[ImageField]:
"""Gets a list of uploads"""
result = ApiDependencies.invoker.services.images.list(
ImageType.UPLOAD, page, per_page
)
return result

View File

@ -2,13 +2,16 @@
import datetime
import os
from glob import glob
from abc import ABC, abstractmethod
from enum import Enum
from pathlib import Path
from queue import Queue
from typing import Dict
from typing import Callable, Dict
from PIL.Image import Image
from invokeai.app.invocations.image import ImageField
from invokeai.app.services.item_storage import PaginatedResults
from invokeai.app.util.save_thumbnail import save_thumbnail
from invokeai.backend.image_util import PngWriter
@ -27,6 +30,10 @@ class ImageStorageBase(ABC):
def get(self, image_type: ImageType, image_name: str) -> Image:
pass
@abstractmethod
def list(self, image_type: ImageType, page: int = 0, per_page: int = 10) -> PaginatedResults[ImageField]:
pass
# TODO: make this a bit more flexible for e.g. cloud storage
@abstractmethod
def get_path(self, image_type: ImageType, image_name: str) -> str:
@ -71,6 +78,19 @@ class DiskImageStorage(ImageStorageBase):
parents=True, exist_ok=True
)
def list(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")
image_list = list(map(lambda i: ImageField(image_type=image_type, image_name=i), image_paths))
count = len(image_list)
image_page = image_list[page * per_page : (page + 1) * per_page]
page_count = int(count / per_page) + 1
return PaginatedResults[ImageField](
items=image_page, page=page, pages=page_count, per_page=per_page, total=count
)
def get(self, image_type: ImageType, image_name: str) -> Image:
image_path = self.get_path(image_type, image_name)
cache_item = self.__get_cache(image_path)