diff --git a/Libraries/LibWeb/HTML/SelectedFile.cpp b/Libraries/LibWeb/HTML/SelectedFile.cpp index 5b059398a9..bf0a493feb 100644 --- a/Libraries/LibWeb/HTML/SelectedFile.cpp +++ b/Libraries/LibWeb/HTML/SelectedFile.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include @@ -12,17 +11,6 @@ namespace Web::HTML { -ErrorOr SelectedFile::from_file_path(ByteString const& file_path) -{ - // https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file):concept-input-file-path - // Filenames must not contain path components, even in the case that a user has selected an entire directory - // hierarchy or multiple files with the same name from different directories. - auto name = LexicalPath::basename(file_path); - - auto file = TRY(Core::File::open(file_path, Core::File::OpenMode::Read)); - return SelectedFile { move(name), IPC::File::adopt_file(move(file)) }; -} - SelectedFile::SelectedFile(ByteString name, ByteBuffer contents) : m_name(move(name)) , m_file_or_contents(move(contents)) diff --git a/Libraries/LibWeb/HTML/SelectedFile.h b/Libraries/LibWeb/HTML/SelectedFile.h index ede21b24bc..95eecac395 100644 --- a/Libraries/LibWeb/HTML/SelectedFile.h +++ b/Libraries/LibWeb/HTML/SelectedFile.h @@ -22,8 +22,6 @@ enum class AllowMultipleFiles { class WEB_API SelectedFile { public: - static ErrorOr from_file_path(ByteString const& file_path); - SelectedFile(ByteString name, ByteBuffer contents); SelectedFile(ByteString name, IPC::File file); diff --git a/Libraries/LibWebView/Utilities.cpp b/Libraries/LibWebView/Utilities.cpp index 4e041d0020..59b1b46b14 100644 --- a/Libraries/LibWebView/Utilities.cpp +++ b/Libraries/LibWebView/Utilities.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #define TOKENCAT(x, y) x##y @@ -145,6 +146,17 @@ ErrorOr handle_attached_debugger() return {}; } +ErrorOr create_selected_file(ByteString const& file_path) +{ + // https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file):concept-input-file-path + // Filenames must not contain path components, even in the case that a user has selected an entire directory + // hierarchy or multiple files with the same name from different directories. + auto name = LexicalPath::basename(file_path); + + auto file = TRY(Core::File::open(file_path, Core::File::OpenMode::Read)); + return Web::HTML::SelectedFile { move(name), IPC::File::adopt_file(move(file)) }; +} + ErrorOr read_json_file(ByteString const& path) { auto file = Core::File::open(path, Core::File::OpenMode::Read); diff --git a/Libraries/LibWebView/Utilities.h b/Libraries/LibWebView/Utilities.h index dc07cb4e3a..089ef91a32 100644 --- a/Libraries/LibWebView/Utilities.h +++ b/Libraries/LibWebView/Utilities.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace WebView { @@ -27,6 +28,8 @@ WEBVIEW_API ByteString mach_server_name_for_process(StringView process_name, pid WEBVIEW_API ErrorOr handle_attached_debugger(); +WEBVIEW_API ErrorOr create_selected_file(ByteString const&); + ErrorOr read_json_file(ByteString const& path); ErrorOr write_json_file(ByteString const& path, JsonValue const& value); diff --git a/UI/AppKit/Interface/Event.mm b/UI/AppKit/Interface/Event.mm index 7cfc9a18f0..6218d2f815 100644 --- a/UI/AppKit/Interface/Event.mm +++ b/UI/AppKit/Interface/Event.mm @@ -9,6 +9,7 @@ #include #include #include +#include #import #import @@ -103,7 +104,7 @@ Web::DragEvent ns_event_to_drag_event(Web::DragEvent::Type type, id #include #include +#include #import #import @@ -741,7 +742,7 @@ struct HideCursor { auto create_selected_file = [&](NSString* ns_file_path) { auto file_path = Ladybird::ns_string_to_byte_string(ns_file_path); - if (auto file = Web::HTML::SelectedFile::from_file_path(file_path); file.is_error()) + if (auto file = WebView::create_selected_file(file_path); file.is_error()) warnln("Unable to open file {}: {}", file_path, file.error()); else selected_files.append(file.release_value()); diff --git a/UI/Gtk/Dialogs.cpp b/UI/Gtk/Dialogs.cpp index e102adae78..f909a16ef7 100644 --- a/UI/Gtk/Dialogs.cpp +++ b/UI/Gtk/Dialogs.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -175,8 +176,7 @@ void show_file_picker(GtkWindow* parent, WebContentView* view, Web::HTML::FileFi GObjectPtr file { g_list_model_get_item(G_LIST_MODEL(file_list), i) }; g_autofree char* path = g_file_get_path(G_FILE(file.ptr())); if (path) { - auto selected_file = Web::HTML::SelectedFile::from_file_path(ByteString(path)); - if (!selected_file.is_error()) + if (auto selected_file = WebView::create_selected_file(ByteString(path)); !selected_file.is_error()) selected.append(selected_file.release_value()); } } @@ -195,8 +195,7 @@ void show_file_picker(GtkWindow* parent, WebContentView* view, Web::HTML::FileFi Vector selected; g_autofree char* path = g_file_get_path(file); if (path) { - auto selected_file = Web::HTML::SelectedFile::from_file_path(ByteString(path)); - if (!selected_file.is_error()) + if (auto selected_file = WebView::create_selected_file(ByteString(path)); !selected_file.is_error()) selected.append(selected_file.release_value()); } view->file_picker_closed(move(selected)); }, view); diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp index be8e949f54..f338ab94fd 100644 --- a/UI/Qt/Tab.cpp +++ b/UI/Qt/Tab.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -317,7 +318,7 @@ Tab::Tab(BrowserWindow* window, RefPtr parent_client, auto create_selected_file = [&](auto const& qfile_path) { auto file_path = ak_byte_string_from_qstring(qfile_path); - if (auto file = Web::HTML::SelectedFile::from_file_path(file_path); file.is_error()) + if (auto file = WebView::create_selected_file(file_path); file.is_error()) warnln("Unable to open file {}: {}", file_path, file.error()); else selected_files.append(file.release_value()); diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp index 4bae0c1435..ac417c2c4e 100644 --- a/UI/Qt/WebContentView.cpp +++ b/UI/Qt/WebContentView.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -863,7 +864,7 @@ void WebContentView::enqueue_native_event(Web::DragEvent::Type type, QDropEvent for (auto const& url : event.mimeData()->urls()) { auto file_path = ak_byte_string_from_qstring(url.toLocalFile()); - if (auto file = Web::HTML::SelectedFile::from_file_path(file_path); file.is_error()) + if (auto file = WebView::create_selected_file(file_path); file.is_error()) warnln("Unable to open file {}: {}", file_path, file.error()); else files.append(file.release_value());