FileDirectoryLoader.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NumberFormat.h>
  7. #include <AK/QuickSort.h>
  8. #include <AK/SourceGenerator.h>
  9. #include <LibCore/DateTime.h>
  10. #include <LibCore/Directory.h>
  11. #include <LibCore/System.h>
  12. #include <LibWeb/Loader/FileDirectoryLoader.h>
  13. namespace Web {
  14. static DeprecatedString s_resource_directory_url = "file:///res";
  15. DeprecatedString resource_directory_url()
  16. {
  17. return s_resource_directory_url;
  18. }
  19. void set_resource_directory_url(DeprecatedString resource_directory_url)
  20. {
  21. s_resource_directory_url = resource_directory_url;
  22. }
  23. static DeprecatedString s_directory_page_url = "file:///res/html/directory.html";
  24. DeprecatedString directory_page_url()
  25. {
  26. return s_directory_page_url;
  27. }
  28. void set_directory_page_url(DeprecatedString directory_page_url)
  29. {
  30. s_directory_page_url = directory_page_url;
  31. }
  32. ErrorOr<DeprecatedString> load_file_directory_page(LoadRequest const& request)
  33. {
  34. // Generate HTML contents entries table
  35. auto lexical_path = LexicalPath(request.url().serialize_path());
  36. Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir);
  37. Vector<DeprecatedString> names;
  38. while (dt.has_next())
  39. names.append(dt.next_path());
  40. quick_sort(names);
  41. StringBuilder contents;
  42. contents.append("<table>"sv);
  43. for (auto& name : names) {
  44. auto path = lexical_path.append(name);
  45. auto maybe_st = Core::System::stat(path.string());
  46. if (!maybe_st.is_error()) {
  47. auto st = maybe_st.release_value();
  48. auto is_directory = S_ISDIR(st.st_mode);
  49. contents.append("<tr>"sv);
  50. contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file");
  51. contents.appendff("<td><a href=\"file://{}\">{}</a></td><td>&nbsp;</td>"sv, path, name);
  52. contents.appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-" : human_readable_size(st.st_size));
  53. contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_deprecated_string());
  54. contents.append("</tr>\n"sv);
  55. }
  56. }
  57. contents.append("</table>"sv);
  58. // Generate HTML directory page from directory template file
  59. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  60. auto template_path = AK::URL::create_with_url_or_path(directory_page_url()).serialize_path();
  61. auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
  62. auto template_contents = TRY(template_file->read_until_eof());
  63. StringBuilder builder;
  64. SourceGenerator generator { builder };
  65. generator.set("resource_directory_url", resource_directory_url());
  66. generator.set("path", escape_html_entities(lexical_path.string()));
  67. generator.set("parent_path", escape_html_entities(lexical_path.parent().string()));
  68. generator.set("contents", contents.to_deprecated_string());
  69. generator.append(template_contents);
  70. return generator.as_string_view().to_deprecated_string();
  71. }
  72. }