2020-02-14 12:17:26 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-02-26 16:09:45 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-02-14 12:17:26 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-14 12:17:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/HashMap.h>
|
2023-08-06 16:09:39 +00:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2024-03-18 03:22:27 +00:00
|
|
|
#include <LibURL/URL.h>
|
2020-02-14 12:17:26 +00:00
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
class MimeData : public EventReceiver {
|
2020-02-14 12:17:26 +00:00
|
|
|
C_OBJECT(MimeData);
|
|
|
|
|
|
|
|
public:
|
2022-02-26 16:09:45 +00:00
|
|
|
virtual ~MimeData() = default;
|
2020-02-14 12:17:26 +00:00
|
|
|
|
2023-08-21 14:45:46 +00:00
|
|
|
ByteBuffer data(StringView mime_type) const { return m_data.get(mime_type).value_or({}); }
|
2023-08-21 15:06:49 +00:00
|
|
|
void set_data(String const& mime_type, ByteBuffer&& data) { m_data.set(mime_type, move(data)); }
|
2020-02-14 12:17:26 +00:00
|
|
|
|
2023-08-21 14:45:46 +00:00
|
|
|
bool has_format(StringView mime_type) const { return m_data.contains(mime_type); }
|
2023-08-23 12:41:26 +00:00
|
|
|
Vector<String> formats() const { return m_data.keys(); }
|
2020-02-14 12:17:26 +00:00
|
|
|
|
|
|
|
// Convenience helpers for "text/plain"
|
2023-08-21 14:45:46 +00:00
|
|
|
bool has_text() const { return has_format("text/plain"sv); }
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString text() const;
|
|
|
|
void set_text(ByteString const&);
|
2020-02-14 12:17:26 +00:00
|
|
|
|
|
|
|
// Convenience helpers for "text/uri-list"
|
2023-08-21 14:45:46 +00:00
|
|
|
bool has_urls() const { return has_format("text/uri-list"sv); }
|
2024-03-18 03:22:27 +00:00
|
|
|
Vector<URL::URL> urls() const;
|
|
|
|
ErrorOr<void> set_urls(Vector<URL::URL> const&);
|
2020-02-14 12:17:26 +00:00
|
|
|
|
2023-08-21 15:06:49 +00:00
|
|
|
HashMap<String, ByteBuffer> const& all_data() const { return m_data; }
|
2020-11-07 19:44:21 +00:00
|
|
|
|
2020-02-14 12:17:26 +00:00
|
|
|
private:
|
2022-02-26 16:09:45 +00:00
|
|
|
MimeData() = default;
|
2023-08-21 15:06:49 +00:00
|
|
|
explicit MimeData(HashMap<String, ByteBuffer> const& data)
|
2023-05-13 18:44:07 +00:00
|
|
|
: m_data(data.clone().release_value_but_fixme_should_propagate_errors())
|
2020-11-07 19:44:21 +00:00
|
|
|
{
|
|
|
|
}
|
2020-02-14 12:17:26 +00:00
|
|
|
|
2023-08-21 15:06:49 +00:00
|
|
|
HashMap<String, ByteBuffer> m_data;
|
2020-02-14 12:17:26 +00:00
|
|
|
};
|
|
|
|
|
2023-01-19 23:57:33 +00:00
|
|
|
StringView guess_mime_type_based_on_filename(StringView);
|
2020-07-27 17:49:43 +00:00
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
Optional<StringView> guess_mime_type_based_on_sniffed_bytes(ReadonlyBytes);
|
|
|
|
Optional<StringView> guess_mime_type_based_on_sniffed_bytes(Core::File&);
|
2021-05-03 18:48:21 +00:00
|
|
|
|
2023-07-11 23:00:23 +00:00
|
|
|
struct MimeType {
|
|
|
|
StringView name {};
|
|
|
|
Vector<StringView> common_extensions {};
|
|
|
|
StringView description {};
|
|
|
|
Optional<Vector<u8>> magic_bytes {};
|
|
|
|
u64 offset { 0 };
|
|
|
|
};
|
|
|
|
|
2024-03-14 23:15:40 +00:00
|
|
|
Optional<MimeType const&> get_mime_type_data(StringView);
|
2023-07-11 23:03:23 +00:00
|
|
|
|
2020-02-14 12:17:26 +00:00
|
|
|
}
|