Resource.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <LibCore/Resource.h>
  8. #include <LibCore/ResourceImplementation.h>
  9. #include <LibCore/System.h>
  10. namespace Core {
  11. Resource::Resource(String path, Scheme scheme, NonnullOwnPtr<Core::MappedFile> file, time_t modified_time)
  12. : m_path(move(path))
  13. , m_scheme(scheme)
  14. , m_data(move(file))
  15. , m_modified_time(modified_time)
  16. {
  17. }
  18. Resource::Resource(String path, Scheme scheme, ByteBuffer buffer, time_t modified_time)
  19. : m_path(move(path))
  20. , m_scheme(scheme)
  21. , m_data(move(buffer))
  22. , m_modified_time(modified_time)
  23. {
  24. }
  25. Resource::Resource(String path, Scheme scheme, DirectoryTag, time_t modified_time)
  26. : m_path(move(path))
  27. , m_scheme(scheme)
  28. , m_data(DirectoryTag {})
  29. , m_modified_time(modified_time)
  30. {
  31. }
  32. ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_filesystem(StringView path)
  33. {
  34. auto filepath = LexicalPath(path);
  35. if (filepath.is_absolute())
  36. return load_from_uri(TRY(String::formatted("file://{}", path)));
  37. auto cwd = TRY(Core::System::getcwd());
  38. return load_from_uri(TRY(String::formatted("file://{}", filepath.prepend(cwd).string())));
  39. }
  40. ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
  41. {
  42. return ResourceImplementation::the().load_from_uri(uri);
  43. }
  44. String Resource::uri() const
  45. {
  46. return MUST(String::formatted("{}://{}", m_scheme == Scheme::Resource ? "resource"sv : "file"sv, m_path));
  47. }
  48. String Resource::filesystem_path() const
  49. {
  50. return ResourceImplementation::the().filesystem_path(*this);
  51. }
  52. String Resource::file_url() const
  53. {
  54. if (m_scheme == Scheme::File)
  55. return uri();
  56. return MUST(String::formatted("file://{}", filesystem_path()));
  57. }
  58. Optional<time_t> Resource::modified_time() const
  59. {
  60. return m_modified_time;
  61. }
  62. String Resource::filename() const
  63. {
  64. return MUST(String::from_utf8(LexicalPath(m_path.bytes_as_string_view()).basename()));
  65. }
  66. Vector<String> Resource::children() const
  67. {
  68. return ResourceImplementation::the().child_names(*this);
  69. }
  70. ByteBuffer Resource::clone_data() const
  71. {
  72. return m_data.visit(
  73. [](NonnullOwnPtr<Core::MappedFile> const& file) { return MUST(ByteBuffer::copy(file->bytes())); },
  74. [](ByteBuffer const& buffer) { return buffer; },
  75. [](DirectoryTag) -> ByteBuffer { VERIFY_NOT_REACHED(); });
  76. }
  77. ByteBuffer Resource::release_data() &&
  78. {
  79. VERIFY(!m_data.has<DirectoryTag>());
  80. if (m_data.has<NonnullOwnPtr<Core::MappedFile>>())
  81. return MUST(ByteBuffer::copy(m_data.get<NonnullOwnPtr<Core::MappedFile>>()->bytes()));
  82. return move(m_data).get<ByteBuffer>();
  83. }
  84. ReadonlyBytes Resource::data() const
  85. {
  86. return m_data.visit(
  87. [](NonnullOwnPtr<Core::MappedFile> const& file) { return file->bytes(); },
  88. [](ByteBuffer const& buffer) { return buffer.bytes(); },
  89. [](DirectoryTag) -> ReadonlyBytes { VERIFY_NOT_REACHED(); });
  90. }
  91. FixedMemoryStream Resource::stream() const
  92. {
  93. return FixedMemoryStream(data());
  94. }
  95. }