ResourceImplementationFile.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <AK/StringView.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibCore/Resource.h>
  10. #include <LibCore/ResourceImplementationFile.h>
  11. namespace Core {
  12. ResourceImplementationFile::ResourceImplementationFile(String base_directory)
  13. : m_base_directory(move(base_directory))
  14. {
  15. }
  16. ErrorOr<NonnullRefPtr<Resource>> ResourceImplementationFile::load_from_resource_scheme_uri(StringView uri)
  17. {
  18. StringView const resource_scheme = "resource://"sv;
  19. VERIFY(uri.starts_with(resource_scheme));
  20. auto path = TRY(String::from_utf8(uri.substring_view(resource_scheme.length())));
  21. auto full_path = TRY(String::from_byte_string(LexicalPath::join(m_base_directory, path).string()));
  22. if (is_directory(full_path))
  23. return make_directory_resource(move(path));
  24. return make_resource(path, TRY(MappedFile::map(full_path)));
  25. }
  26. Vector<String> ResourceImplementationFile::child_names_for_resource_scheme(Resource const& resource)
  27. {
  28. Vector<String> children;
  29. Core::DirIterator it(resource.filesystem_path().to_byte_string(), Core::DirIterator::SkipParentAndBaseDir);
  30. while (it.has_next())
  31. children.append(MUST(String::from_byte_string(it.next_path())));
  32. return children;
  33. }
  34. String ResourceImplementationFile::filesystem_path_for_resource_scheme(String const& relative_path)
  35. {
  36. return MUST(String::from_byte_string(LexicalPath::join(m_base_directory, relative_path).string()));
  37. }
  38. }