GeneratedPagesLoader.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/GeneratedPagesLoader.h>
  13. namespace Web {
  14. static String s_resource_directory_url = "file:///res"_string;
  15. String resource_directory_url()
  16. {
  17. return s_resource_directory_url;
  18. }
  19. void set_resource_directory_url(String resource_directory_url)
  20. {
  21. s_resource_directory_url = resource_directory_url;
  22. }
  23. static String s_error_page_url = "file:///res/html/error.html"_string;
  24. String error_page_url()
  25. {
  26. return s_error_page_url;
  27. }
  28. void set_error_page_url(String error_page_url)
  29. {
  30. s_error_page_url = error_page_url;
  31. }
  32. static String s_directory_page_url = "file:///res/html/directory.html"_string;
  33. String directory_page_url()
  34. {
  35. return s_directory_page_url;
  36. }
  37. void set_directory_page_url(String directory_page_url)
  38. {
  39. s_directory_page_url = directory_page_url;
  40. }
  41. ErrorOr<String> load_error_page(AK::URL const& url)
  42. {
  43. // Generate HTML error page from error template file
  44. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  45. auto template_path = AK::URL::create_with_url_or_path(error_page_url().to_deprecated_string()).serialize_path();
  46. auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
  47. auto template_contents = TRY(template_file->read_until_eof());
  48. StringBuilder builder;
  49. SourceGenerator generator { builder };
  50. generator.set("resource_directory_url", resource_directory_url());
  51. generator.set("failed_url", url.to_deprecated_string());
  52. generator.append(template_contents);
  53. return TRY(String::from_utf8(generator.as_string_view()));
  54. }
  55. ErrorOr<String> load_file_directory_page(LoadRequest const& request)
  56. {
  57. // Generate HTML contents entries table
  58. auto lexical_path = LexicalPath(request.url().serialize_path());
  59. Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir);
  60. Vector<DeprecatedString> names;
  61. while (dt.has_next())
  62. names.append(dt.next_path());
  63. quick_sort(names);
  64. StringBuilder contents;
  65. contents.append("<table>"sv);
  66. for (auto& name : names) {
  67. auto path = lexical_path.append(name);
  68. auto maybe_st = Core::System::stat(path.string());
  69. if (!maybe_st.is_error()) {
  70. auto st = maybe_st.release_value();
  71. auto is_directory = S_ISDIR(st.st_mode);
  72. contents.append("<tr>"sv);
  73. contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file");
  74. contents.appendff("<td><a href=\"file://{}\">{}</a></td><td>&nbsp;</td>"sv, path, name);
  75. contents.appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-" : human_readable_size(st.st_size));
  76. contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_deprecated_string());
  77. contents.append("</tr>\n"sv);
  78. }
  79. }
  80. contents.append("</table>"sv);
  81. // Generate HTML directory page from directory template file
  82. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  83. auto template_path = AK::URL::create_with_url_or_path(directory_page_url().to_deprecated_string()).serialize_path();
  84. auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
  85. auto template_contents = TRY(template_file->read_until_eof());
  86. StringBuilder builder;
  87. SourceGenerator generator { builder };
  88. generator.set("resource_directory_url", resource_directory_url());
  89. generator.set("path", escape_html_entities(lexical_path.string()));
  90. generator.set("parent_path", escape_html_entities(lexical_path.parent().string()));
  91. generator.set("contents", contents.to_deprecated_string());
  92. generator.append(template_contents);
  93. return TRY(String::from_utf8(generator.as_string_view()));
  94. }
  95. }