File.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2022-2024, 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/FilePrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/FileAPI/File.h>
  11. #include <LibWeb/Infra/Strings.h>
  12. #include <LibWeb/MimeSniff/MimeType.h>
  13. namespace Web::FileAPI {
  14. JS_DEFINE_ALLOCATOR(File);
  15. File::File(JS::Realm& realm, ByteBuffer byte_buffer, String file_name, String type, i64 last_modified)
  16. : Blob(realm, move(byte_buffer), move(type))
  17. , m_name(move(file_name))
  18. , m_last_modified(last_modified)
  19. {
  20. }
  21. File::File(JS::Realm& realm)
  22. : Blob(realm, {})
  23. {
  24. }
  25. void File::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(File);
  29. }
  30. File::~File() = default;
  31. JS::NonnullGCPtr<File> File::create(JS::Realm& realm)
  32. {
  33. return realm.heap().allocate<File>(realm, realm);
  34. }
  35. // https://w3c.github.io/FileAPI/#ref-for-dom-file-file
  36. WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
  37. {
  38. auto& vm = realm.vm();
  39. // 1. Let bytes be the result of processing blob parts given fileBits and options.
  40. auto bytes = TRY_OR_THROW_OOM(vm, process_blob_parts(file_bits, options.has_value() ? static_cast<BlobPropertyBag const&>(*options) : Optional<BlobPropertyBag> {}));
  41. // 2. Let n be the fileName argument to the constructor.
  42. // 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.
  43. auto name = file_name;
  44. auto type = String {};
  45. i64 last_modified = 0;
  46. // 3. Process FilePropertyBag dictionary argument by running the following substeps:
  47. if (options.has_value()) {
  48. // FIXME: 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
  49. // 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.
  50. // FIXME: 2. Convert every character in t to ASCII lowercase.
  51. // NOTE: The spec is out of date, and we are supposed to call into the MimeType parser here.
  52. auto maybe_parsed_type = Web::MimeSniff::MimeType::parse(options->type);
  53. if (maybe_parsed_type.has_value())
  54. type = maybe_parsed_type->serialized();
  55. // 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]).
  56. // 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].
  57. last_modified = options->last_modified.has_value() ? options->last_modified.value() : UnixDateTime::now().milliseconds_since_epoch();
  58. }
  59. // 4. Return a new File object F such that:
  60. // 2. F refers to the bytes byte sequence.
  61. // NOTE: Spec started at 2 therefore keeping the same number sequence here.
  62. // 3. F.size is set to the number of total bytes in bytes.
  63. // 4. F.name is set to n.
  64. // 5. F.type is set to t.
  65. // 6. F.lastModified is set to d.
  66. return realm.heap().allocate<File>(realm, realm, move(bytes), move(name), move(type), last_modified);
  67. }
  68. WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::construct_impl(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
  69. {
  70. return create(realm, file_bits, file_name, options);
  71. }
  72. WebIDL::ExceptionOr<void> File::serialization_steps(HTML::SerializationRecord& record, bool, HTML::SerializationMemory&)
  73. {
  74. auto& vm = this->vm();
  75. // FIXME: 1. Set serialized.[[SnapshotState]] to value’s snapshot state.
  76. // NON-STANDARD: FileAPI spec doesn't specify that type should be serialized, although
  77. // to be conformant with other browsers this needs to be serialized.
  78. TRY(HTML::serialize_string(vm, record, m_type));
  79. // 2. Set serialized.[[ByteSequence]] to value’s underlying byte sequence.
  80. TRY(HTML::serialize_bytes(vm, record, m_byte_buffer.bytes()));
  81. // 3. Set serialized.[[Name]] to the value of value’s name attribute.
  82. TRY(HTML::serialize_string(vm, record, m_name));
  83. // 4. Set serialized.[[LastModified]] to the value of value’s lastModified attribute.
  84. HTML::serialize_primitive_type(record, m_last_modified);
  85. return {};
  86. }
  87. WebIDL::ExceptionOr<void> File::deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&)
  88. {
  89. auto& vm = this->vm();
  90. // FIXME: 1. Set value’s snapshot state to serialized.[[SnapshotState]].
  91. // NON-STANDARD: FileAPI spec doesn't specify that type should be deserialized, although
  92. // to be conformant with other browsers this needs to be deserialized.
  93. m_type = TRY(HTML::deserialize_string(vm, record, position));
  94. // 2. Set value’s underlying byte sequence to serialized.[[ByteSequence]].
  95. m_byte_buffer = TRY(HTML::deserialize_bytes(vm, record, position));
  96. // 3. Initialize the value of value’s name attribute to serialized.[[Name]].
  97. m_name = TRY(HTML::deserialize_string(vm, record, position));
  98. // 4. Initialize the value of value’s lastModified attribute to serialized.[[LastModified]].
  99. m_last_modified = HTML::deserialize_primitive_type<i64>(record, position);
  100. return {};
  101. }
  102. }