File.cpp 3.4 KB

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