File.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Time.h>
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/FileAPI/File.h>
  10. #include <LibWeb/Infra/Strings.h>
  11. namespace Web::FileAPI {
  12. File::File(JS::Realm& realm, ByteBuffer byte_buffer, String file_name, String type, i64 last_modified)
  13. : Blob(realm, move(byte_buffer), move(type))
  14. , m_name(move(file_name))
  15. , m_last_modified(last_modified)
  16. {
  17. }
  18. JS::ThrowCompletionOr<void> File::initialize(JS::Realm& realm)
  19. {
  20. MUST_OR_THROW_OOM(Base::initialize(realm));
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::FilePrototype>(realm, "File"));
  22. return {};
  23. }
  24. File::~File() = default;
  25. // https://w3c.github.io/FileAPI/#ref-for-dom-file-file
  26. WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
  27. {
  28. auto& vm = realm.vm();
  29. // 1. Let bytes be the result of processing blob parts given fileBits and options.
  30. auto bytes = TRY_OR_THROW_OOM(realm.vm(), process_blob_parts(file_bits, options.has_value() ? static_cast<BlobPropertyBag const&>(*options) : Optional<BlobPropertyBag> {}));
  31. // 2. Let n be the fileName argument to the constructor.
  32. // 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.
  33. auto name = file_name;
  34. auto type = String {};
  35. i64 last_modified = 0;
  36. // 3. Process FilePropertyBag dictionary argument by running the following substeps:
  37. if (options.has_value()) {
  38. // 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
  39. // 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.
  40. // NOTE: t is set to empty string at declaration.
  41. if (!options->type.is_empty()) {
  42. if (is_basic_latin(options->type))
  43. type = options->type;
  44. }
  45. // 2. Convert every character in t to ASCII lowercase.
  46. if (!type.is_empty())
  47. type = TRY_OR_THROW_OOM(vm, Infra::to_ascii_lowercase(type));
  48. // 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]).
  49. // 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].
  50. last_modified = options->last_modified.has_value() ? options->last_modified.value() : UnixDateTime::now().milliseconds_since_epoch();
  51. }
  52. // 4. Return a new File object F such that:
  53. // 2. F refers to the bytes byte sequence.
  54. // NOTE: Spec started at 2 therefore keeping the same number sequence here.
  55. // 3. F.size is set to the number of total bytes in bytes.
  56. // 4. F.name is set to n.
  57. // 5. F.type is set to t.
  58. // 6. F.lastModified is set to d.
  59. return MUST_OR_THROW_OOM(realm.heap().allocate<File>(realm, realm, move(bytes), move(name), move(type), last_modified));
  60. }
  61. WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::construct_impl(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
  62. {
  63. return create(realm, file_bits, file_name, options);
  64. }
  65. }