GeneratedPagesLoader.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/Resource.h>
  12. #include <LibCore/System.h>
  13. #include <LibWeb/Loader/GeneratedPagesLoader.h>
  14. namespace Web {
  15. ErrorOr<String> load_error_page(AK::URL const& url)
  16. {
  17. // Generate HTML error page from error template file
  18. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  19. auto template_path = TRY(Core::Resource::load_from_uri("resource://ladybird/error.html"sv))->filesystem_path();
  20. auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
  21. auto template_contents = TRY(template_file->read_until_eof());
  22. StringBuilder builder;
  23. SourceGenerator generator { builder };
  24. generator.set("failed_url", url.to_byte_string());
  25. generator.append(template_contents);
  26. return TRY(String::from_utf8(generator.as_string_view()));
  27. }
  28. ErrorOr<String> load_file_directory_page(AK::URL const& url)
  29. {
  30. // Generate HTML contents entries table
  31. auto lexical_path = LexicalPath(url.serialize_path());
  32. Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir);
  33. Vector<ByteString> names;
  34. while (dt.has_next())
  35. names.append(dt.next_path());
  36. quick_sort(names);
  37. StringBuilder contents;
  38. contents.append("<table>"sv);
  39. for (auto& name : names) {
  40. auto path = lexical_path.append(name);
  41. auto maybe_st = Core::System::stat(path.string());
  42. if (!maybe_st.is_error()) {
  43. auto st = maybe_st.release_value();
  44. auto is_directory = S_ISDIR(st.st_mode);
  45. contents.append("<tr>"sv);
  46. contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file");
  47. contents.appendff("<td><a href=\"file://{}\">{}</a></td><td>&nbsp;</td>"sv, path, name);
  48. contents.appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-" : human_readable_size(st.st_size));
  49. contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_byte_string());
  50. contents.append("</tr>\n"sv);
  51. }
  52. }
  53. contents.append("</table>"sv);
  54. // Generate HTML directory page from directory template file
  55. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  56. auto template_path = TRY(Core::Resource::load_from_uri("resource://ladybird/directory.html"sv))->filesystem_path();
  57. auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
  58. auto template_contents = TRY(template_file->read_until_eof());
  59. StringBuilder builder;
  60. SourceGenerator generator { builder };
  61. generator.set("path", escape_html_entities(lexical_path.string()));
  62. generator.set("parent_url", TRY(String::formatted("file://{}", escape_html_entities(lexical_path.parent().string()))));
  63. generator.set("contents", contents.to_byte_string());
  64. generator.append(template_contents);
  65. return TRY(String::from_utf8(generator.as_string_view()));
  66. }
  67. }