File.cpp 3.2 KB

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