1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:27:44 +00:00

LibWeb: Introduce the File interface from the FileAPI spec

This commit is contained in:
Kenneth Myhra 2022-07-24 21:32:18 +02:00 committed by Linus Groh
parent df91dbfbb6
commit 890514a057
9 changed files with 139 additions and 0 deletions

View file

@ -87,6 +87,8 @@
#include <LibWeb/Bindings/EventPrototype.h> #include <LibWeb/Bindings/EventPrototype.h>
#include <LibWeb/Bindings/EventTargetConstructor.h> #include <LibWeb/Bindings/EventTargetConstructor.h>
#include <LibWeb/Bindings/EventTargetPrototype.h> #include <LibWeb/Bindings/EventTargetPrototype.h>
#include <LibWeb/Bindings/FileConstructor.h>
#include <LibWeb/Bindings/FilePrototype.h>
#include <LibWeb/Bindings/FocusEventConstructor.h> #include <LibWeb/Bindings/FocusEventConstructor.h>
#include <LibWeb/Bindings/FocusEventPrototype.h> #include <LibWeb/Bindings/FocusEventPrototype.h>
#include <LibWeb/Bindings/HTMLAnchorElementConstructor.h> #include <LibWeb/Bindings/HTMLAnchorElementConstructor.h>
@ -422,6 +424,7 @@
ADD_WINDOW_OBJECT_INTERFACE(ErrorEvent) \ ADD_WINDOW_OBJECT_INTERFACE(ErrorEvent) \
ADD_WINDOW_OBJECT_INTERFACE(Event) \ ADD_WINDOW_OBJECT_INTERFACE(Event) \
ADD_WINDOW_OBJECT_INTERFACE(EventTarget) \ ADD_WINDOW_OBJECT_INTERFACE(EventTarget) \
ADD_WINDOW_OBJECT_INTERFACE(File) \
ADD_WINDOW_OBJECT_INTERFACE(Headers) \ ADD_WINDOW_OBJECT_INTERFACE(Headers) \
ADD_WINDOW_OBJECT_INTERFACE(History) \ ADD_WINDOW_OBJECT_INTERFACE(History) \
ADD_WINDOW_OBJECT_INTERFACE(HTMLAnchorElement) \ ADD_WINDOW_OBJECT_INTERFACE(HTMLAnchorElement) \

View file

@ -128,6 +128,7 @@ set(SOURCES
Fetch/Infrastructure/HTTP/Responses.cpp Fetch/Infrastructure/HTTP/Responses.cpp
Fetch/Infrastructure/HTTP/Statuses.cpp Fetch/Infrastructure/HTTP/Statuses.cpp
FileAPI/Blob.cpp FileAPI/Blob.cpp
FileAPI/File.cpp
FontCache.cpp FontCache.cpp
Geometry/DOMRectList.cpp Geometry/DOMRectList.cpp
HTML/AttributeNames.cpp HTML/AttributeNames.cpp

View file

@ -52,6 +52,11 @@ Blob::Blob(ByteBuffer byte_buffer, String type)
{ {
} }
Blob::Blob(ByteBuffer byte_buffer)
: m_byte_buffer(move(byte_buffer))
{
}
// https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob // https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::create(Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options) DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::create(Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
{ {

View file

@ -54,6 +54,9 @@ public:
ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); } ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
protected:
Blob(ByteBuffer byte_buffer);
private: private:
Blob() = default; Blob() = default;

View file

@ -0,0 +1,73 @@
/*
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/FileAPI/File.h>
namespace Web::FileAPI {
static bool is_basic_latin(StringView view)
{
for (auto code_point : view) {
if (code_point < 0x0020 || code_point > 0x007E)
return false;
}
return true;
}
File::File(ByteBuffer byte_buffer, String file_name, String type, i64 last_modified)
: Blob(move(byte_buffer), move(type))
, m_name(move(file_name))
, m_last_modified(last_modified)
{
}
// https://w3c.github.io/FileAPI/#ref-for-dom-file-file
DOM::ExceptionOr<NonnullRefPtr<File>> File::create(Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
{
// 1. Let bytes be the result of processing blob parts given fileBits and options.
auto bytes = TRY_OR_RETURN_OOM(process_blob_parts(file_bits));
// 2. Let n be the fileName argument to the constructor.
// NOTE: Underlying OS filesystems use differing conventions for file name; with constructed files, mandating UTF-16 lessens ambiquity when file names are converted to byte sequences.
auto name = file_name;
auto type = String::empty();
i64 last_modified = 0;
// 3. Process FilePropertyBag dictionary argument by running the following substeps:
if (options.has_value()) {
// 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
// If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps.
// NOTE: t is set to empty string at declaration.
if (!options->type.is_empty()) {
if (is_basic_latin(options->type))
type = options->type;
}
// 2. Convert every character in t to ASCII lowercase.
if (!type.is_empty())
type = options->type.to_lowercase();
// 3. If the lastModified member is provided, let d be set to the lastModified dictionary member. If it is not provided, set d to the current date and time represented as the number of milliseconds since the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]).
// Note: Since ECMA-262 Date objects convert to long long values representing the number of milliseconds since the Unix Epoch, the lastModified member could be a Date object [ECMA-262].
last_modified = options->last_modified.has_value() ? options->last_modified.value() : Time::now_realtime().to_milliseconds();
}
// 4. Return a new File object F such that:
// 2. F refers to the bytes byte sequence.
// NOTE: Spec started at 2 therefore keeping the same number sequence here.
// 3. F.size is set to the number of total bytes in bytes.
// 4. F.name is set to n.
// 5. F.type is set to t.
// 6. F.lastModified is set to d.
return adopt_ref(*new File(move(bytes), move(name), move(type), last_modified));
}
DOM::ExceptionOr<NonnullRefPtr<File>> File::create_with_global_object(Bindings::WindowObject&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
{
return create(file_bits, file_name, options);
}
}

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Time.h>
#include <LibWeb/FileAPI/Blob.h>
namespace Web::FileAPI {
struct FilePropertyBag : BlobPropertyBag {
Optional<i64> last_modified;
};
class File : public Blob {
public:
using WrapperType = Bindings::FileWrapper;
static DOM::ExceptionOr<NonnullRefPtr<File>> create(Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
static DOM::ExceptionOr<NonnullRefPtr<File>> create_with_global_object(Bindings::WindowObject&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
// https://w3c.github.io/FileAPI/#dfn-name
String const& name() const { return m_name; }
// https://w3c.github.io/FileAPI/#dfn-lastModified
i64 last_modified() const { return m_last_modified; }
private:
File(ByteBuffer byte_buffer, String file_name, String type, i64 last_modified);
String m_name;
i64 m_last_modified { 0 };
};
}

View file

@ -0,0 +1,13 @@
#import <FileAPI/Blob.idl>
[Exposed=(Window,Worker), Serializable]
interface File : Blob {
constructor(sequence<BlobPart> fileBits, USVString fileName, optional FilePropertyBag options = {});
readonly attribute DOMString name;
readonly attribute long long lastModified;
};
dictionary FilePropertyBag : BlobPropertyBag {
long long lastModified;
};

View file

@ -185,6 +185,7 @@ class Response;
namespace Web::FileAPI { namespace Web::FileAPI {
class Blob; class Blob;
class File;
} }
namespace Web::Geometry { namespace Web::Geometry {
@ -474,6 +475,7 @@ class ErrorEventWrapper;
class EventListenerWrapper; class EventListenerWrapper;
class EventTargetWrapper; class EventTargetWrapper;
class EventWrapper; class EventWrapper;
class FileWrapper;
class FocusEventWrapper; class FocusEventWrapper;
class HeadersWrapper; class HeadersWrapper;
class HeadersIteratorWrapper; class HeadersIteratorWrapper;

View file

@ -55,6 +55,7 @@ libweb_js_wrapper(Encoding/TextDecoder)
libweb_js_wrapper(Encoding/TextEncoder) libweb_js_wrapper(Encoding/TextEncoder)
libweb_js_wrapper(Fetch/Headers ITERABLE) libweb_js_wrapper(Fetch/Headers ITERABLE)
libweb_js_wrapper(FileAPI/Blob) libweb_js_wrapper(FileAPI/Blob)
libweb_js_wrapper(FileAPI/File)
libweb_js_wrapper(Geometry/DOMPoint) libweb_js_wrapper(Geometry/DOMPoint)
libweb_js_wrapper(Geometry/DOMPointReadOnly) libweb_js_wrapper(Geometry/DOMPointReadOnly)
libweb_js_wrapper(Geometry/DOMRect) libweb_js_wrapper(Geometry/DOMRect)