fix(upload): target image host API
This commit is contained in:
1 parent
12e9de9ba4
commit
c3358f3f66
7 files changed
+55
-29
No files matched your search
+25
-20
@@ -61,26 +61,31 @@ class ImageHostClient:
|
||||
"""Загружает произвольный файл на image-host и возвращает прямую ссылку."""
|
||||
if not self.api_url or not body:
|
||||
return None
|
||||
initiate = self._request_json(
|
||||
"/api/uploads/initiate",
|
||||
{
|
||||
"retention": self.retention,
|
||||
"files": [{"name": file_name, "size": len(body), "type": content_type}],
|
||||
},
|
||||
)
|
||||
files = initiate.get("files") or []
|
||||
if not files or not files[0].get("uploadUrl"):
|
||||
raise ImageHostError("image host did not return an upload URL")
|
||||
upload = files[0]
|
||||
request = Request(
|
||||
str(upload["uploadUrl"]),
|
||||
data=body,
|
||||
method="PUT",
|
||||
headers={str(key): str(value) for key, value in (upload.get("uploadHeaders") or {}).items()},
|
||||
)
|
||||
with urlopen(request, timeout=self.timeout):
|
||||
pass
|
||||
return str(upload.get("directUrl") or upload.get("cdnUrl") or "") or None
|
||||
try:
|
||||
initiate = self._request_json(
|
||||
"/api/uploads/initiate",
|
||||
{
|
||||
"retention": self.retention,
|
||||
"files": [{"name": file_name, "size": len(body), "type": content_type}],
|
||||
},
|
||||
)
|
||||
files = initiate.get("files") or []
|
||||
if not files or not files[0].get("uploadUrl"):
|
||||
raise ImageHostError("image host did not return an upload URL")
|
||||
upload = files[0]
|
||||
request = Request(
|
||||
str(upload["uploadUrl"]),
|
||||
data=body,
|
||||
method="PUT",
|
||||
headers={str(key): str(value) for key, value in (upload.get("uploadHeaders") or {}).items()},
|
||||
)
|
||||
with urlopen(request, timeout=self.timeout):
|
||||
pass
|
||||
return str(upload.get("directUrl") or upload.get("cdnUrl") or "") or None
|
||||
except ImageHostError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
raise ImageHostError(f"image host upload failed: {exc}") from exc
|
||||
|
||||
def _request_json(self, path: str, payload: dict[str, object]) -> dict[str, object]:
|
||||
"""
|
||||
|
||||
+7
-4
@@ -16,7 +16,7 @@ from .belki.history import CalculationStore
|
||||
from .belki.applications import ApplicationError, ApplicationStore, validate_application
|
||||
from .belki.google_sheets_export import GoogleSheetsExportService
|
||||
from .belki.identity import IdentityDirectory
|
||||
from .belki.image_host import ImageHostClient
|
||||
from .belki.image_host import ImageHostClient, ImageHostError
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
@@ -229,9 +229,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
raise ApiError("Не удалось прочитать изображение") from exc
|
||||
if len(body) > 15 * 1024 * 1024:
|
||||
raise ApiError("Файл слишком большой (максимум 15 МБ)")
|
||||
url = ImageHostClient(api_url=os.getenv("BELKI_IMAGE_HOST_API_URL", "https://img.nyan404.ru")).upload_bytes(
|
||||
body, str(payload.get("name", "screenshot.jpg")), str(payload.get("type", "image/jpeg"))
|
||||
)
|
||||
try:
|
||||
url = ImageHostClient(api_url=os.getenv("BELKI_IMAGE_HOST_API_URL", "https://nyan.nyan404.ru")).upload_bytes(
|
||||
body, str(payload.get("name", "screenshot.jpg")), str(payload.get("type", "image/jpeg"))
|
||||
)
|
||||
except ImageHostError as exc:
|
||||
raise ApiError(f"Не удалось загрузить изображение: {exc}") from exc
|
||||
if not url:
|
||||
raise ApiError("Хранилище изображений не настроено")
|
||||
self._json(200, {"url": url, "name": payload.get("name", "screenshot.jpg")})
|
||||
|
||||
Reference in new issue
Block a user