mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(api): set max-age for images
Image files are immutable and we expect deletion to result in no further requests for a given image, so we can set the max-age to something thicc. Resolves #3426
This commit is contained in:
parent
344d87c9f1
commit
c5539b442c
@ -1,25 +1,26 @@
|
|||||||
import io
|
import io
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from fastapi import Body, HTTPException, Path, Query, Request, Response, UploadFile
|
|
||||||
from fastapi.routing import APIRouter
|
from fastapi import (Body, HTTPException, Path, Query, Request, Response,
|
||||||
|
UploadFile)
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
|
from fastapi.routing import APIRouter
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from invokeai.app.models.image import (
|
|
||||||
ImageCategory,
|
from invokeai.app.models.image import ImageCategory, ResourceOrigin
|
||||||
ResourceOrigin,
|
|
||||||
)
|
|
||||||
from invokeai.app.services.image_record_storage import OffsetPaginatedResults
|
from invokeai.app.services.image_record_storage import OffsetPaginatedResults
|
||||||
from invokeai.app.services.models.image_record import (
|
|
||||||
ImageDTO,
|
|
||||||
ImageRecordChanges,
|
|
||||||
ImageUrlsDTO,
|
|
||||||
)
|
|
||||||
from invokeai.app.services.item_storage import PaginatedResults
|
from invokeai.app.services.item_storage import PaginatedResults
|
||||||
|
from invokeai.app.services.models.image_record import (ImageDTO,
|
||||||
|
ImageRecordChanges,
|
||||||
|
ImageUrlsDTO)
|
||||||
|
|
||||||
from ..dependencies import ApiDependencies
|
from ..dependencies import ApiDependencies
|
||||||
|
|
||||||
images_router = APIRouter(prefix="/v1/images", tags=["images"])
|
images_router = APIRouter(prefix="/v1/images", tags=["images"])
|
||||||
|
|
||||||
|
# images are immutable; set a high max-age
|
||||||
|
IMAGE_MAX_AGE = 31536000
|
||||||
|
|
||||||
|
|
||||||
@images_router.post(
|
@images_router.post(
|
||||||
"/",
|
"/",
|
||||||
@ -141,12 +142,14 @@ async def get_image_full(
|
|||||||
if not ApiDependencies.invoker.services.images.validate_path(path):
|
if not ApiDependencies.invoker.services.images.validate_path(path):
|
||||||
raise HTTPException(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
return FileResponse(
|
response = FileResponse(
|
||||||
path,
|
path,
|
||||||
media_type="image/png",
|
media_type="image/png",
|
||||||
filename=image_name,
|
filename=image_name,
|
||||||
content_disposition_type="inline",
|
content_disposition_type="inline",
|
||||||
)
|
)
|
||||||
|
response.headers["Cache-Control"] = f"max-age={IMAGE_MAX_AGE}"
|
||||||
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
@ -175,9 +178,11 @@ async def get_image_thumbnail(
|
|||||||
if not ApiDependencies.invoker.services.images.validate_path(path):
|
if not ApiDependencies.invoker.services.images.validate_path(path):
|
||||||
raise HTTPException(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
return FileResponse(
|
response = FileResponse(
|
||||||
path, media_type="image/webp", content_disposition_type="inline"
|
path, media_type="image/webp", content_disposition_type="inline"
|
||||||
)
|
)
|
||||||
|
response.headers["Cache-Control"] = f"max-age={IMAGE_MAX_AGE}"
|
||||||
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=404)
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user