[image-service] update api route, commit image
This commit is contained in:
parent
54ecb2ee8c
commit
fe75c5b9ed
3 changed files with 142 additions and 7 deletions
|
|
@ -5,12 +5,12 @@ Image management enapoint:
|
||||||
## Upload Images
|
## Upload Images
|
||||||
### POST /image/{folder}/upload
|
### POST /image/{folder}/upload
|
||||||
```
|
```
|
||||||
curl -X POST http://localhost/image/page_drink_n/upload \
|
curl -X POST http://localhost/image/page_drink_n/upload/uid/displayname/email \
|
||||||
-F "files=@img1.png" \
|
-F "files=@img1.png" \
|
||||||
-F "files=@img2.png"
|
-F "files=@img2.png"
|
||||||
```
|
```
|
||||||
### POST /inter/{country}/image/{folder}/upload
|
### POST /inter/{country}/image/{folder}/upload
|
||||||
```
|
```
|
||||||
curl -X POST http://localhost/inter/tha/image/page_drink_disable_n2/upload \
|
curl -X POST http://localhost/inter/tha/image/page_drink_disable_n2/upload/uid/displayname/email \
|
||||||
-F "files=@bn_hot_america_no.png"
|
-F "files=@bn_hot_america_no.png"
|
||||||
```
|
```
|
||||||
142
main.py
142
main.py
|
|
@ -4,12 +4,16 @@ from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import shutil
|
import shutil
|
||||||
import uuid
|
import uuid
|
||||||
|
import os
|
||||||
|
import httpx
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
BASE_DIR = Path("/usr/src/app/taobin_project")
|
BASE_DIR = Path("/usr/src/app/taobin_project")
|
||||||
# BASE_DIR = Path("/taobin_project")
|
# BASE_DIR = Path("/taobin_project")
|
||||||
|
|
||||||
|
GIT_REPO_SERVER_URL = os.getenv("GIT_REPO_SERVER_URL")
|
||||||
|
|
||||||
ALLOWED_FOLDERS = {"page_drink",
|
ALLOWED_FOLDERS = {"page_drink",
|
||||||
"page_drink_disable",
|
"page_drink_disable",
|
||||||
"page_drink_disable_n",
|
"page_drink_disable_n",
|
||||||
|
|
@ -44,16 +48,100 @@ def get_image_dir(folder: str, country: Optional[str] = None) -> Path:
|
||||||
path.mkdir(parents=True, exist_ok=True)
|
path.mkdir(parents=True, exist_ok=True)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
async def commit_files_to_git(
|
||||||
|
files: list[UploadFile],
|
||||||
|
folder: str,
|
||||||
|
display_name: str,
|
||||||
|
email: str,
|
||||||
|
country: str,
|
||||||
|
git_server_url: str = GIT_REPO_SERVER_URL
|
||||||
|
) -> dict:
|
||||||
|
"""
|
||||||
|
Commit file(s) to Git repository server via multipart form POST.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
files: List of UploadFile objects (file pointers will be reset)
|
||||||
|
folder: Folder name used in commit message
|
||||||
|
display_name: Git signature username
|
||||||
|
email: Git signature email
|
||||||
|
git_server_url: Target Git repo server endpoint
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response JSON from Git server
|
||||||
|
"""
|
||||||
|
commit_data = {
|
||||||
|
"signature_username": display_name,
|
||||||
|
"signature_email": email,
|
||||||
|
"message": f"commit {folder} {country}"
|
||||||
|
}
|
||||||
|
|
||||||
|
multipart_files = {}
|
||||||
|
|
||||||
|
if len(files) == 1:
|
||||||
|
file_obj = files[0]
|
||||||
|
file_obj.file.seek(0)
|
||||||
|
commit_data["path"] = Path(file_obj.filename).name
|
||||||
|
multipart_files["file"] = (
|
||||||
|
Path(file_obj.filename).name,
|
||||||
|
file_obj.file,
|
||||||
|
"application/octet-stream"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
for idx, file_obj in enumerate(files, start=1):
|
||||||
|
file_obj.file.seek(0)
|
||||||
|
commit_data[f"path{idx}"] = Path(file_obj.filename).name
|
||||||
|
multipart_files[f"file{idx}"] = (
|
||||||
|
Path(file_obj.filename).name,
|
||||||
|
file_obj.file,
|
||||||
|
"application/octet-stream"
|
||||||
|
)
|
||||||
|
|
||||||
|
# return {
|
||||||
|
# "git_url": git_server_url,
|
||||||
|
# "data": commit_data,
|
||||||
|
# "file": multipart_files
|
||||||
|
# }
|
||||||
|
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.post(
|
||||||
|
git_server_url,
|
||||||
|
data=commit_data,
|
||||||
|
files=multipart_files,
|
||||||
|
timeout=30.0
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
# ─────────────────────────────────────────
|
# ─────────────────────────────────────────
|
||||||
# UPLOAD
|
# UPLOAD
|
||||||
# ─────────────────────────────────────────
|
# ─────────────────────────────────────────
|
||||||
|
|
||||||
@app.post("/image/{folder}/upload")
|
@app.post("/image/{folder}/upload/{uid}/{displayname}/{email}")
|
||||||
async def upload_images(
|
async def upload_images(
|
||||||
folder: str,
|
folder: str,
|
||||||
|
uid: str,
|
||||||
|
displayname: str,
|
||||||
|
email: str,
|
||||||
files: list[UploadFile] = File(...)
|
files: list[UploadFile] = File(...)
|
||||||
):
|
):
|
||||||
|
required_user_fields = {
|
||||||
|
"uid": uid,
|
||||||
|
"displayname": displayname,
|
||||||
|
"email": email,
|
||||||
|
"country": "tha"
|
||||||
|
}
|
||||||
|
|
||||||
|
if not (country):
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid country")
|
||||||
|
|
||||||
|
for field, value in required_user_fields.items():
|
||||||
|
if not str(value).strip():
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=f"Missing or empty user_info.{field}"
|
||||||
|
)
|
||||||
|
|
||||||
validate_folder(folder)
|
validate_folder(folder)
|
||||||
saved = []
|
saved = []
|
||||||
for file in files:
|
for file in files:
|
||||||
|
|
@ -66,15 +154,49 @@ async def upload_images(
|
||||||
"filename": filename,
|
"filename": filename,
|
||||||
"url": f"/image/{folder}/{filename}"
|
"url": f"/image/{folder}/{filename}"
|
||||||
})
|
})
|
||||||
return {"uploaded": saved}
|
|
||||||
|
# Commit to Git
|
||||||
|
git_response = await commit_files_to_git(
|
||||||
|
files=files,
|
||||||
|
folder=folder,
|
||||||
|
display_name=required_user_fields.get("displayname"),
|
||||||
|
email=required_user_fields.get("email"),
|
||||||
|
country=required_user_fields.get("country")
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"uploaded": saved,
|
||||||
|
"git_commit": git_response
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.post("/inter/{country}/image/{folder}/upload")
|
@app.post("/inter/{country}/image/{folder}/upload/{uid}/{displayname}/{email}")
|
||||||
async def upload_inter_images(
|
async def upload_inter_images(
|
||||||
country: str,
|
country: str,
|
||||||
folder: str,
|
folder: str,
|
||||||
|
uid: str,
|
||||||
|
displayname: str,
|
||||||
|
email: str,
|
||||||
files: list[UploadFile] = File(...)
|
files: list[UploadFile] = File(...)
|
||||||
):
|
):
|
||||||
|
|
||||||
|
required_user_fields = {
|
||||||
|
"uid": uid,
|
||||||
|
"displayname": displayname,
|
||||||
|
"email": email,
|
||||||
|
"country": country
|
||||||
|
}
|
||||||
|
|
||||||
|
if not (country):
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid country")
|
||||||
|
|
||||||
|
for field, value in required_user_fields.items():
|
||||||
|
if not str(value).strip():
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=f"Missing or empty user_info.{field}"
|
||||||
|
)
|
||||||
|
|
||||||
validate_folder(folder)
|
validate_folder(folder)
|
||||||
saved = []
|
saved = []
|
||||||
for file in files:
|
for file in files:
|
||||||
|
|
@ -87,4 +209,16 @@ async def upload_inter_images(
|
||||||
"filename": filename,
|
"filename": filename,
|
||||||
"url": f"/inter/{country}/image/{folder}/{filename}"
|
"url": f"/inter/{country}/image/{folder}/{filename}"
|
||||||
})
|
})
|
||||||
return {"uploaded": saved}
|
|
||||||
|
git_response = await commit_files_to_git(
|
||||||
|
files=files,
|
||||||
|
folder=folder,
|
||||||
|
display_name=required_user_fields.get("displayname"),
|
||||||
|
email=required_user_fields.get("email"),
|
||||||
|
country=required_user_fields.get("country")
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"uploaded": saved,
|
||||||
|
"git_commit": git_response
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
fastapi
|
fastapi
|
||||||
uvicorn
|
uvicorn
|
||||||
python-multipart
|
python-multipart
|
||||||
|
httpx
|
||||||
Loading…
Add table
Add a link
Reference in a new issue