Files
moloapp/src/vendors/media/common/downloader.cpp
T
retoorandClaude Sonnet 5.5 5a0c18d0e0 feat(clients): media: image and video generation across 12 providers behind one job seam
Stability, BFL, Runway, Luma, Replicate, fal, Ideogram, Recraft, Leonardo,
MiniMax, Kling, Higgsfield: call tables checked against vendor specs/SDKs,
ImageGenerator/VideoGenerator seam with capped polling, /ai/media demo.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 15:19:43 +01:00

75 lines
2.8 KiB
C++

#include "vendors/media/common/downloader.h"
#include "clients/transports/http_transport.h"
#include "llm/error.h"
#include "security/crypto/Base64.h"
#include "vendors/media/common/caps.h"
namespace molo::media {
namespace {
clients::ClientConfig plain_config() {
clients::ClientConfig c;
c.persist_cookies = false;
return c;
}
std::unique_ptr<clients::Transport> real_or(std::unique_ptr<clients::Transport> t) {
if (t) return t;
return std::make_unique<clients::transports::HttpTransport>();
}
} // namespace
Downloader::Downloader(std::unique_ptr<clients::Transport> transport)
: client_(real_or(std::move(transport)), plain_config()) {}
void Downloader::validate_url(const std::string& url) {
auto bad = [](const std::string& why) { throw llm::ApiError::invalid("media", why); };
if (url.empty() || url.size() > MediaCaps::kMaxUrlChars) bad("download url length");
if (url.rfind("https://", 0) != 0) bad("download url must be https");
for (unsigned char c : url)
if (c <= 0x20 || c == 0x7f) bad("download url has whitespace or control characters");
std::size_t host_end = url.find_first_of("/?#", 8);
std::string authority = url.substr(8, host_end == std::string::npos ? std::string::npos : host_end - 8);
if (authority.empty()) bad("download url has no host");
if (authority.find('@') != std::string::npos) bad("download url must not carry credentials");
}
std::string Downloader::fetch_url(const std::string& url, std::string* content_type) {
validate_url(url);
try {
clients::ClientRequest req(clients::Method::kGet, clients::Url::parse(url));
clients::ClientResponse res = client_.send_blocking(std::move(req));
if (!res.is_success())
throw llm::ApiError("media", res.status(), "DOWNLOAD_FAILED", "download answered HTTP " + std::to_string(res.status()));
if (res.body().size() > MediaCaps::kMaxDownloadBytes)
throw llm::ApiError("media", 502, "BAD_RESPONSE", "download over the byte cap");
if (content_type) *content_type = res.content_type();
return res.body();
} catch (const llm::ApiError&) {
throw;
} catch (const std::exception& e) {
throw llm::ApiError("media", 502, "UNAVAILABLE", e.what());
}
}
FetchedAsset Downloader::fetch(const MediaAsset& asset) {
FetchedAsset out;
out.asset = asset;
if (!asset.data_base64.empty()) {
if (!security::crypto::Base64::decode(asset.data_base64, out.bytes))
throw llm::ApiError::invalid("media", "inline asset is not valid base64");
if (out.bytes.size() > MediaCaps::kMaxDownloadBytes)
throw llm::ApiError("media", 502, "BAD_RESPONSE", "inline asset over the byte cap");
out.content_type = asset.mime;
return out;
}
if (asset.url.empty()) throw llm::ApiError::invalid("media", "asset has neither url nor data");
out.bytes = fetch_url(asset.url, &out.content_type);
return out;
}
} // namespace molo::media