2023-08-13 22:35:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-09-16 21:51:00 +01:00
|
|
|
#include <AK/NumberFormat.h>
|
2023-08-13 22:35:35 +02:00
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/SourceGenerator.h>
|
|
|
|
#include <LibCore/DateTime.h>
|
|
|
|
#include <LibCore/Directory.h>
|
|
|
|
#include <LibCore/System.h>
|
2023-09-21 17:32:31 +02:00
|
|
|
#include <LibWeb/Loader/GeneratedPagesLoader.h>
|
2023-08-13 22:35:35 +02:00
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
2023-12-04 15:21:48 -05:00
|
|
|
static String s_error_page_url = "file:///res/ladybird/error.html"_string;
|
2023-09-21 17:55:14 +02:00
|
|
|
|
|
|
|
String error_page_url()
|
|
|
|
{
|
|
|
|
return s_error_page_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_error_page_url(String error_page_url)
|
|
|
|
{
|
|
|
|
s_error_page_url = error_page_url;
|
|
|
|
}
|
|
|
|
|
2023-12-04 15:21:48 -05:00
|
|
|
static String s_directory_page_url = "file:///res/ladybird/directory.html"_string;
|
2023-09-19 19:16:50 +02:00
|
|
|
|
2023-09-21 17:32:31 +02:00
|
|
|
String directory_page_url()
|
2023-09-19 19:16:50 +02:00
|
|
|
{
|
|
|
|
return s_directory_page_url;
|
|
|
|
}
|
|
|
|
|
2023-09-21 17:32:31 +02:00
|
|
|
void set_directory_page_url(String directory_page_url)
|
2023-09-19 19:16:50 +02:00
|
|
|
{
|
|
|
|
s_directory_page_url = directory_page_url;
|
|
|
|
}
|
|
|
|
|
2023-09-21 17:55:14 +02:00
|
|
|
ErrorOr<String> load_error_page(AK::URL const& url)
|
|
|
|
{
|
|
|
|
// Generate HTML error page from error template file
|
|
|
|
// FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
|
2023-12-16 17:49:34 +03:30
|
|
|
auto template_path = AK::URL::create_with_url_or_path(error_page_url().to_byte_string()).serialize_path();
|
2023-09-21 17:55:14 +02:00
|
|
|
auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
|
|
|
|
auto template_contents = TRY(template_file->read_until_eof());
|
|
|
|
StringBuilder builder;
|
|
|
|
SourceGenerator generator { builder };
|
2023-12-16 17:49:34 +03:30
|
|
|
generator.set("failed_url", url.to_byte_string());
|
2023-09-21 17:55:14 +02:00
|
|
|
generator.append(template_contents);
|
|
|
|
return TRY(String::from_utf8(generator.as_string_view()));
|
|
|
|
}
|
2023-09-21 17:32:31 +02:00
|
|
|
|
2023-12-26 10:12:35 +01:00
|
|
|
ErrorOr<String> load_file_directory_page(AK::URL const& url)
|
2023-08-13 22:35:35 +02:00
|
|
|
{
|
|
|
|
// Generate HTML contents entries table
|
2023-12-26 10:12:35 +01:00
|
|
|
auto lexical_path = LexicalPath(url.serialize_path());
|
2023-08-13 22:35:35 +02:00
|
|
|
Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir);
|
2023-12-16 17:49:34 +03:30
|
|
|
Vector<ByteString> names;
|
2023-08-13 22:35:35 +02:00
|
|
|
while (dt.has_next())
|
|
|
|
names.append(dt.next_path());
|
|
|
|
quick_sort(names);
|
|
|
|
|
|
|
|
StringBuilder contents;
|
|
|
|
contents.append("<table>"sv);
|
|
|
|
for (auto& name : names) {
|
|
|
|
auto path = lexical_path.append(name);
|
|
|
|
auto maybe_st = Core::System::stat(path.string());
|
|
|
|
if (!maybe_st.is_error()) {
|
|
|
|
auto st = maybe_st.release_value();
|
2023-09-16 21:51:00 +01:00
|
|
|
auto is_directory = S_ISDIR(st.st_mode);
|
|
|
|
|
2023-08-13 22:35:35 +02:00
|
|
|
contents.append("<tr>"sv);
|
2023-09-16 21:51:00 +01:00
|
|
|
contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file");
|
2023-08-13 22:35:35 +02:00
|
|
|
contents.appendff("<td><a href=\"file://{}\">{}</a></td><td> </td>"sv, path, name);
|
2023-09-16 21:51:00 +01:00
|
|
|
contents.appendff("<td>{:10}</td><td> </td>", is_directory ? "-" : human_readable_size(st.st_size));
|
2023-12-16 17:49:34 +03:30
|
|
|
contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_byte_string());
|
2023-08-13 22:35:35 +02:00
|
|
|
contents.append("</tr>\n"sv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contents.append("</table>"sv);
|
|
|
|
|
|
|
|
// Generate HTML directory page from directory template file
|
|
|
|
// FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
|
2023-12-16 17:49:34 +03:30
|
|
|
auto template_path = AK::URL::create_with_url_or_path(directory_page_url().to_byte_string()).serialize_path();
|
2023-08-13 22:35:35 +02:00
|
|
|
auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
|
|
|
|
auto template_contents = TRY(template_file->read_until_eof());
|
|
|
|
StringBuilder builder;
|
|
|
|
SourceGenerator generator { builder };
|
|
|
|
generator.set("path", escape_html_entities(lexical_path.string()));
|
2023-12-26 10:12:35 +01:00
|
|
|
generator.set("parent_url", TRY(String::formatted("file://{}", escape_html_entities(lexical_path.parent().string()))));
|
2023-12-16 17:49:34 +03:30
|
|
|
generator.set("contents", contents.to_byte_string());
|
2023-08-13 22:35:35 +02:00
|
|
|
generator.append(template_contents);
|
2023-09-21 17:32:31 +02:00
|
|
|
return TRY(String::from_utf8(generator.as_string_view()));
|
2023-08-13 22:35:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|