mirror of
https://github.com/RGBCube/serenity
synced 2025-05-27 23:55:09 +00:00
LibWeb: Make process_blob_parts() publicly accessible
This pulls process_blob_parts() out of the Blob class and makes it publicly accessible.
This commit is contained in:
parent
622a4f29a7
commit
c038a8c9c9
2 changed files with 36 additions and 34 deletions
|
@ -12,6 +12,40 @@
|
||||||
|
|
||||||
namespace Web::FileAPI {
|
namespace Web::FileAPI {
|
||||||
|
|
||||||
|
// https://w3c.github.io/FileAPI/#process-blob-parts
|
||||||
|
ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts)
|
||||||
|
{
|
||||||
|
// 1. Let bytes be an empty sequence of bytes.
|
||||||
|
ByteBuffer bytes {};
|
||||||
|
|
||||||
|
// 2. For each element in parts:
|
||||||
|
for (auto const& blob_part : blob_parts) {
|
||||||
|
TRY(blob_part.visit(
|
||||||
|
// 1. If element is a USVString, run the following sub-steps:
|
||||||
|
[&](String const& string) -> ErrorOr<void> {
|
||||||
|
// NOTE: This step is handled by the lambda expression.
|
||||||
|
// 1. Let s be element.
|
||||||
|
|
||||||
|
// FIXME: 2. If the endings member of options is "native", set s to the result of converting line endings to native of element.
|
||||||
|
|
||||||
|
// NOTE: The AK::String is always UTF-8.
|
||||||
|
// 3. Append the result of UTF-8 encoding s to bytes.
|
||||||
|
return bytes.try_append(string.to_byte_buffer());
|
||||||
|
},
|
||||||
|
// 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those bytes to bytes.
|
||||||
|
[&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
|
||||||
|
auto data_buffer = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
|
||||||
|
return bytes.try_append(data_buffer.bytes());
|
||||||
|
},
|
||||||
|
// 3. If element is a Blob, append the bytes it represents to bytes.
|
||||||
|
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
|
||||||
|
return bytes.try_append(blob->bytes());
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
// 3. Return bytes.
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
Blob::Blob(ByteBuffer byte_buffer, String type)
|
Blob::Blob(ByteBuffer byte_buffer, String type)
|
||||||
: m_byte_buffer(move(byte_buffer))
|
: m_byte_buffer(move(byte_buffer))
|
||||||
, m_type(move(type))
|
, m_type(move(type))
|
||||||
|
@ -49,39 +83,6 @@ DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::create_with_global_object(Bindings::
|
||||||
return Blob::create(blob_parts, options);
|
return Blob::create(blob_parts, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/FileAPI/#process-blob-parts
|
|
||||||
ErrorOr<ByteBuffer> Blob::process_blob_parts(Vector<BlobPart> const& blob_parts)
|
|
||||||
{
|
|
||||||
// 1. Let bytes be an empty sequence of bytes.
|
|
||||||
ByteBuffer bytes {};
|
|
||||||
|
|
||||||
// 2. For each element in parts:
|
|
||||||
for (auto const& blob_part : blob_parts) {
|
|
||||||
TRY(blob_part.visit(
|
|
||||||
// 1. If element is a USVString, run the following sub-steps:
|
|
||||||
[&](String const& string) -> ErrorOr<void> {
|
|
||||||
// NOTE: This step is handled by the lambda expression.
|
|
||||||
// 1. Let s be element.
|
|
||||||
|
|
||||||
// FIXME: 2. If the endings member of options is "native", set s to the result of converting line endings to native of element.
|
|
||||||
|
|
||||||
// NOTE: The AK::String is always UTF-8.
|
|
||||||
// 3. Append the result of UTF-8 encoding s to bytes.
|
|
||||||
return bytes.try_append(string.to_byte_buffer());
|
|
||||||
},
|
|
||||||
// 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those bytes to bytes.
|
|
||||||
[&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
|
|
||||||
auto data_buffer = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
|
|
||||||
return bytes.try_append(data_buffer.bytes());
|
|
||||||
},
|
|
||||||
// 3. If element is a Blob, append the bytes it represents to bytes.
|
|
||||||
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
|
|
||||||
return bytes.try_append(blob->bytes());
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://w3c.github.io/FileAPI/#dfn-slice
|
// https://w3c.github.io/FileAPI/#dfn-slice
|
||||||
DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::slice(Optional<i64> start, Optional<i64> end, Optional<String> const& content_type)
|
DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::slice(Optional<i64> start, Optional<i64> end, Optional<String> const& content_type)
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,6 +26,8 @@ struct BlobPropertyBag {
|
||||||
Bindings::EndingType endings;
|
Bindings::EndingType endings;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
[[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
|
||||||
|
|
||||||
class Blob
|
class Blob
|
||||||
: public RefCounted<Blob>
|
: public RefCounted<Blob>
|
||||||
, public Weakable<Blob>
|
, public Weakable<Blob>
|
||||||
|
@ -52,7 +54,6 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Blob() = default;
|
Blob() = default;
|
||||||
static ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
|
|
||||||
|
|
||||||
ByteBuffer m_byte_buffer {};
|
ByteBuffer m_byte_buffer {};
|
||||||
String m_type {};
|
String m_type {};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue