GeneratedPagesLoader.cpp 3.7 KB

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