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]:
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user