diff --git a/app/routes.py b/app/routes.py index 7fd068d..3471562 100644 --- a/app/routes.py +++ b/app/routes.py @@ -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, diff --git a/docs/draft-api.md b/docs/draft-api.md index c1047b4..8ce765a 100644 --- a/docs/draft-api.md +++ b/docs/draft-api.md @@ -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 +
Описание изображения
Описание изображения
+``` + ## Создание черновика ```http