Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64 Image support #1328

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 25 additions & 24 deletions Polyfills/Canvas/Source/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,36 +124,37 @@ namespace Babylon::Polyfills::Internal
request.Open(UrlLib::UrlMethod::Get, text);
request.ResponseType(UrlLib::UrlResponseType::Buffer);
request.SendAsync().then(m_runtimeScheduler, *m_cancellationSource, [env{info.Env()}, this, cancellationSource{m_cancellationSource}, request{std::move(request)}, text](arcana::expected<void, std::exception_ptr> result) {
if (!cancellationSource->cancelled())
if (cancellationSource->cancelled())
{
if (result.has_error())
{
HandleLoadImageError(Napi::Error::New(env, result.error()));
return;
}
return;
}
if (result.has_error())
{
HandleLoadImageError(Napi::Error::New(env, result.error()));
return;
}

Dispose();
Dispose();

auto buffer{request.ResponseBuffer()};
std::vector<uint8_t> base64Buffer;
if (buffer.data() == nullptr || buffer.size_bytes() == 0)
{
// try with base64
static const std::string base64{"base64,"};
const auto pos = text.find(base64);
if (pos == std::string::npos)
{
HandleLoadImageError(Napi::Error::New(env, "Image with provided source returned empty response or invalid base64."));
return;
}
bn::decode_b64(text.begin() + pos + base64.length(), text.end(), std::back_inserter(base64Buffer));
buffer = {reinterpret_cast<std::byte*>(base64Buffer.data()), base64Buffer.size()};
}
if (!SetBuffer(buffer))
auto buffer{request.ResponseBuffer()};
std::vector<uint8_t> base64Buffer;
if (buffer.data() == nullptr || buffer.size_bytes() == 0)
{
// try with base64
static const std::string base64{"base64,"};
const auto pos = text.find(base64);
CedricGuillemet marked this conversation as resolved.
Show resolved Hide resolved
if (pos == std::string::npos)
{
HandleLoadImageError(Napi::Error::New(env, "Unable to decode image with provided src."));
HandleLoadImageError(Napi::Error::New(env, "Image with provided source returned empty response or invalid base64."));
return;
}
bn::decode_b64(text.begin() + pos + base64.length(), text.end(), std::back_inserter(base64Buffer));
buffer = {reinterpret_cast<std::byte*>(base64Buffer.data()), base64Buffer.size()};
}
if (!SetBuffer(buffer))
{
HandleLoadImageError(Napi::Error::New(env, "Unable to decode image with provided src."));
return;
}
});
}
Expand Down