Skip to content

Commit acf4815

Browse files
authored
Merge pull request #6 from mofa-org/feat/06-cdn-cache
feat: CDN caching for avatar images
2 parents 7244cb4 + 93bb459 commit acf4815

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

app.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,21 @@ async def list_avatars():
150150
})
151151
return result
152152

153+
_CDN_CACHE_HEADERS = {"Cache-Control": "public, max-age=86400"}
154+
155+
153156
@app.get("/api/avatars/{avatar_id}/image")
154157
async def get_avatar_image(avatar_id: str):
155158
p = _find_preset(avatar_id)
156159
if p:
157160
path = AVATARS_DIR / p["file"]
158161
if path.exists():
159-
return FileResponse(path, media_type="image/png")
162+
return FileResponse(path, media_type="image/png", headers=_CDN_CACHE_HEADERS)
160163
meta = _load_meta()
161164
if avatar_id in meta:
162165
path = UPLOADS_DIR / meta[avatar_id]["file"]
163166
if path.exists():
164-
return FileResponse(path)
167+
return FileResponse(path, headers=_CDN_CACHE_HEADERS)
165168
raise HTTPException(404, "角色不存在")
166169

167170

@@ -190,7 +193,7 @@ async def get_avatar_thumbnail(avatar_id: str):
190193
if not source_path or not source_path.exists():
191194
raise HTTPException(404, "角色不存在")
192195
thumb_path = _make_thumbnail(source_path, avatar_id)
193-
return FileResponse(thumb_path, media_type="image/webp")
196+
return FileResponse(thumb_path, media_type="image/webp", headers=_CDN_CACHE_HEADERS)
194197

195198

196199
@app.post("/api/avatars/upload")

0 commit comments

Comments
 (0)