Resource.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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)
  12. : m_path(move(path))
  13. , m_scheme(scheme)
  14. , m_data(move(file))
  15. {
  16. }
  17. Resource::Resource(String path, Scheme scheme, ByteBuffer buffer)
  18. : m_path(move(path))
  19. , m_scheme(scheme)
  20. , m_data(move(buffer))
  21. {
  22. }
  23. Resource::Resource(String path, Scheme scheme, DirectoryTag)
  24. : m_path(move(path))
  25. , m_scheme(scheme)
  26. , m_data(DirectoryTag {})
  27. {
  28. }
  29. ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_filesystem(StringView path)
  30. {
  31. auto filepath = LexicalPath(path);
  32. if (filepath.is_absolute())
  33. return load_from_uri(TRY(String::formatted("file://{}", path)));
  34. auto cwd = TRY(Core::System::getcwd());
  35. return load_from_uri(TRY(String::formatted("file://{}", filepath.prepend(cwd).string())));
  36. }
  37. ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
  38. {
  39. return ResourceImplementation::the().load_from_uri(uri);
  40. }
  41. String Resource::uri() const
  42. {
  43. return MUST(String::formatted("{}://{}", m_scheme == Scheme::Resource ? "resource"sv : "file"sv, m_path));
  44. }
  45. String Resource::filesystem_path() const
  46. {
  47. return ResourceImplementation::the().filesystem_path(*this);
  48. }
  49. String Resource::file_url() const
  50. {
  51. if (m_scheme == Scheme::File)
  52. return uri();
  53. return MUST(String::formatted("file://{}", filesystem_path()));
  54. }
  55. String Resource::filename() const
  56. {
  57. return MUST(String::from_utf8(LexicalPath(m_path.bytes_as_string_view()).basename()));
  58. }
  59. Vector<String> Resource::children() const
  60. {
  61. return ResourceImplementation::the().child_names(*this);
  62. }
  63. ByteBuffer Resource::clone_data() const
  64. {
  65. return m_data.visit(
  66. [](NonnullOwnPtr<Core::MappedFile> const& file) { return MUST(ByteBuffer::copy(file->bytes())); },
  67. [](ByteBuffer const& buffer) { return buffer; },
  68. [](DirectoryTag) -> ByteBuffer { VERIFY_NOT_REACHED(); });
  69. }
  70. ByteBuffer Resource::release_data() &&
  71. {
  72. VERIFY(!m_data.has<DirectoryTag>());
  73. if (m_data.has<NonnullOwnPtr<Core::MappedFile>>())
  74. return MUST(ByteBuffer::copy(m_data.get<NonnullOwnPtr<Core::MappedFile>>()->bytes()));
  75. return move(m_data).get<ByteBuffer>();
  76. }
  77. ReadonlyBytes Resource::data() const
  78. {
  79. return m_data.visit(
  80. [](NonnullOwnPtr<Core::MappedFile> const& file) { return file->bytes(); },
  81. [](ByteBuffer const& buffer) { return buffer.bytes(); },
  82. [](DirectoryTag) -> ReadonlyBytes { VERIFY_NOT_REACHED(); });
  83. }
  84. FixedMemoryStream Resource::stream() const
  85. {
  86. return FixedMemoryStream(data());
  87. }
  88. }