2022-07-10 13:31:17 -03:00
|
|
|
/*
|
2023-02-25 06:27:38 -03:00
|
|
|
* Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2022-07-10 13:31:17 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
|
#include <AK/Vector.h>
|
2022-08-28 08:42:07 -03:00
|
|
|
#include <LibWeb/Bindings/BlobPrototype.h>
|
2022-09-04 06:44:38 -03:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2022-07-10 13:31:17 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
2022-09-25 13:03:42 -03:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2022-07-10 13:31:17 -03:00
|
|
|
|
|
|
|
|
namespace Web::FileAPI {
|
|
|
|
|
|
2023-02-25 06:27:38 -03:00
|
|
|
using BlobPart = Variant<JS::Handle<JS::Object>, JS::Handle<Blob>, String>;
|
2022-07-10 13:31:17 -03:00
|
|
|
|
|
|
|
|
struct BlobPropertyBag {
|
2023-02-25 06:27:38 -03:00
|
|
|
String type = String {};
|
2022-07-10 13:31:17 -03:00
|
|
|
Bindings::EndingType endings;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-25 06:27:38 -03:00
|
|
|
[[nodiscard]] ErrorOr<String> convert_line_endings_to_native(StringView string);
|
2022-07-30 05:36:18 -03:00
|
|
|
[[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
|
2022-08-01 14:33:27 -03:00
|
|
|
[[nodiscard]] bool is_basic_latin(StringView view);
|
2022-07-24 11:05:25 -03:00
|
|
|
|
2022-09-04 06:44:38 -03:00
|
|
|
class Blob : public Bindings::PlatformObject {
|
|
|
|
|
WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject);
|
2022-07-10 13:31:17 -03:00
|
|
|
|
|
|
|
|
public:
|
2022-08-28 08:42:07 -03:00
|
|
|
virtual ~Blob() override;
|
|
|
|
|
|
2023-08-13 08:05:26 -03:00
|
|
|
[[nodiscard]] static JS::NonnullGCPtr<Blob> create(JS::Realm&, ByteBuffer, String type);
|
|
|
|
|
[[nodiscard]] static JS::NonnullGCPtr<Blob> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
|
2022-09-25 21:08:29 -03:00
|
|
|
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
|
2022-07-10 13:31:17 -03:00
|
|
|
|
2022-07-25 18:08:55 -03:00
|
|
|
// https://w3c.github.io/FileAPI/#dfn-size
|
2022-07-10 13:31:17 -03:00
|
|
|
u64 size() const { return m_byte_buffer.size(); }
|
2022-07-25 18:08:55 -03:00
|
|
|
// https://w3c.github.io/FileAPI/#dfn-type
|
2023-02-25 06:27:38 -03:00
|
|
|
String const& type() const { return m_type; }
|
2022-07-10 13:31:17 -03:00
|
|
|
|
2023-02-25 06:27:38 -03:00
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {});
|
2022-07-10 13:31:17 -03:00
|
|
|
|
2023-06-12 16:36:00 -03:00
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<Streams::ReadableStream>> stream();
|
2023-02-25 06:27:38 -03:00
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> text();
|
2023-06-15 05:29:54 -03:00
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> array_buffer();
|
2022-07-10 13:31:17 -03:00
|
|
|
|
2022-07-24 10:43:33 -03:00
|
|
|
ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
|
|
|
|
|
|
2023-09-08 22:27:22 -03:00
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<Streams::ReadableStream>> get_stream();
|
|
|
|
|
|
2022-07-24 16:32:18 -03:00
|
|
|
protected:
|
2023-02-25 06:27:38 -03:00
|
|
|
Blob(JS::Realm&, ByteBuffer, String type);
|
2022-09-25 21:08:29 -03:00
|
|
|
Blob(JS::Realm&, ByteBuffer);
|
2022-07-24 16:32:18 -03:00
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2023-01-10 08:28:20 -03:00
|
|
|
|
2022-07-10 13:31:17 -03:00
|
|
|
private:
|
2022-09-25 21:08:29 -03:00
|
|
|
explicit Blob(JS::Realm&);
|
2022-07-10 13:31:17 -03:00
|
|
|
|
|
|
|
|
ByteBuffer m_byte_buffer {};
|
2023-02-25 06:27:38 -03:00
|
|
|
String m_type {};
|
2022-07-10 13:31:17 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|