feat: support image uploads in draft API
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 17s
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 17s
This commit is contained in:
parent
bbbae29280
commit
dc2184d90b
2 changed files with 49 additions and 2 deletions
|
|
@ -1319,8 +1319,7 @@ def delete_article(article_id: int, session: Session = Depends(get_session)) ->
|
|||
return {"id": article.id, "status": "deleted"}
|
||||
|
||||
|
||||
@router.post("/api/uploads/images")
|
||||
async def upload_image(file: UploadFile = File(...), settings: Settings = Depends(get_settings)) -> dict[str, str]:
|
||||
async def store_uploaded_image(file: UploadFile, settings: Settings) -> dict[str, str]:
|
||||
if not file.content_type or not file.content_type.startswith("image/"):
|
||||
raise HTTPException(status_code=422, detail="Only image files are supported")
|
||||
suffix = Path(file.filename or "").suffix.lower()
|
||||
|
|
@ -1344,6 +1343,22 @@ async def upload_image(file: UploadFile = File(...), settings: Settings = Depend
|
|||
return {"url": f"{settings.public_base_url.rstrip('/')}/media/uploads/{date_dir}/{filename}"}
|
||||
|
||||
|
||||
@router.post("/api/uploads/images")
|
||||
async def upload_image(file: UploadFile = File(...), settings: Settings = Depends(get_settings)) -> dict[str, str]:
|
||||
return await store_uploaded_image(file, settings)
|
||||
|
||||
|
||||
@router.post("/api/v1/media/images")
|
||||
async def upload_api_image(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
session: Session = Depends(get_session),
|
||||
settings: Settings = Depends(get_settings),
|
||||
) -> dict[str, str]:
|
||||
require_draft_api_key(request, session)
|
||||
return await store_uploaded_image(file, settings)
|
||||
|
||||
|
||||
@router.post("/api/articles/{article_id}/publish")
|
||||
def publish_article(
|
||||
article_id: int,
|
||||
|
|
|
|||
|
|
@ -27,6 +27,38 @@ Authorization: Bearer dzen_draft_...
|
|||
|
||||
Не передавайте ключ в URL, query-параметрах, HTML статьи или логах.
|
||||
|
||||
## Загрузка изображения
|
||||
|
||||
Сначала загрузите файл через API, затем используйте возвращённый URL в `featureImageUrl` или внутри `bodyHtml`:
|
||||
|
||||
```http
|
||||
POST /api/v1/media/images
|
||||
Authorization: Bearer dzen_draft_...
|
||||
Content-Type: multipart/form-data
|
||||
```
|
||||
|
||||
Имя multipart-поля — `file`. Поддерживаются JPEG, PNG, WebP и GIF размером до 12 МБ.
|
||||
|
||||
```bash
|
||||
curl -X POST 'https://dzen.nstarikov.ru/api/v1/media/images' \
|
||||
-H 'Authorization: Bearer dzen_draft_ВАШ_КЛЮЧ' \
|
||||
-F 'file=@cover.webp'
|
||||
```
|
||||
|
||||
Ответ:
|
||||
|
||||
```json
|
||||
{
|
||||
"url": "https://dzen.nstarikov.ru/media/uploads/20260824/abcd.webp"
|
||||
}
|
||||
```
|
||||
|
||||
Для inline-изображения добавьте `alt` самостоятельно:
|
||||
|
||||
```html
|
||||
<figure><img src="https://dzen.nstarikov.ru/media/uploads/20260824/abcd.webp" alt="Описание изображения"><figcaption>Описание изображения</figcaption></figure>
|
||||
```
|
||||
|
||||
## Создание черновика
|
||||
|
||||
```http
|
||||
|
|
|
|||
Loading…
Reference in a new issue