chore: update

This commit is contained in:
TakahashiNg
2026-05-10 01:55:37 +00:00
parent 9bb609c165
commit f10c5970b5
2 changed files with 12 additions and 13 deletions
+8 -9
View File
@@ -1,35 +1,34 @@
import os import os
import shutil
from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from pathlib import Path from pathlib import Path
app = FastAPI() app = FastAPI()
# Thư mục lưu trữ ảnh (nên được mount volume khi chạy docker)
STORAGE_DIR = Path("images") STORAGE_DIR = Path("images")
STORAGE_DIR.mkdir(exist_ok=True) STORAGE_DIR.mkdir(exist_ok=True)
@app.post("/") @app.post("/")
async def upload_image(file: UploadFile = File(...)): async def upload_image(file: UploadFile = File(...)):
# Chỉ chấp nhận định dạng ảnh
if not file.content_type.startswith("image/"): if not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="File không hợp lệ. Chỉ chấp nhận ảnh.") raise HTTPException(status_code=400, detail="Chỉ chấp nhận file ảnh.")
file_path = STORAGE_DIR / file.filename file_path = STORAGE_DIR / file.filename
# Ghi file theo block để tối ưu bộ nhớ # Ghi file theo kiểu stream để tránh tràn RAM
with open(file_path, "wb") as f: try:
content = await file.read() with file_path.open("wb") as buffer:
f.write(content) shutil.copyfileobj(file.file, buffer)
finally:
file.file.close()
return {"status": "success", "filename": file.filename} return {"status": "success", "filename": file.filename}
@app.get("/{name}") @app.get("/{name}")
async def get_image(name: str): async def get_image(name: str):
file_path = STORAGE_DIR / name file_path = STORAGE_DIR / name
if not file_path.exists() or not file_path.is_file(): if not file_path.exists() or not file_path.is_file():
raise HTTPException(status_code=404, detail="Không tìm thấy ảnh") raise HTTPException(status_code=404, detail="Không tìm thấy ảnh")
# Trả về ảnh trực tiếp cho trình duyệt/client
return FileResponse(file_path) return FileResponse(file_path)
+4 -4
View File
@@ -20,11 +20,11 @@ spec:
- containerPort: 8080 - containerPort: 8080
resources: resources:
requests: requests:
memory: "32Mi" memory: "128Mi"
cpu: "50m" cpu: "150m"
limits: limits:
memory: "64Mi" memory: "512Mi"
cpu: "250m" cpu: "450m"
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service