MimeData.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/URL.h>
  11. #include <LibCore/EventReceiver.h>
  12. namespace Core {
  13. class MimeData : public EventReceiver {
  14. C_OBJECT(MimeData);
  15. public:
  16. virtual ~MimeData() = default;
  17. ByteBuffer data(StringView mime_type) const { return m_data.get(mime_type).value_or({}); }
  18. void set_data(String const& mime_type, ByteBuffer&& data) { m_data.set(mime_type, move(data)); }
  19. bool has_format(StringView mime_type) const { return m_data.contains(mime_type); }
  20. Vector<String> formats() const { return m_data.keys(); }
  21. // Convenience helpers for "text/plain"
  22. bool has_text() const { return has_format("text/plain"sv); }
  23. ByteString text() const;
  24. void set_text(ByteString const&);
  25. // Convenience helpers for "text/uri-list"
  26. bool has_urls() const { return has_format("text/uri-list"sv); }
  27. Vector<URL> urls() const;
  28. ErrorOr<void> set_urls(Vector<URL> const&);
  29. HashMap<String, ByteBuffer> const& all_data() const { return m_data; }
  30. private:
  31. MimeData() = default;
  32. explicit MimeData(HashMap<String, ByteBuffer> const& data)
  33. : m_data(data.clone().release_value_but_fixme_should_propagate_errors())
  34. {
  35. }
  36. HashMap<String, ByteBuffer> m_data;
  37. };
  38. StringView guess_mime_type_based_on_filename(StringView);
  39. Optional<StringView> guess_mime_type_based_on_sniffed_bytes(ReadonlyBytes);
  40. Optional<StringView> guess_mime_type_based_on_sniffed_bytes(Core::File&);
  41. struct MimeType {
  42. StringView name {};
  43. Vector<StringView> common_extensions {};
  44. StringView description {};
  45. Optional<Vector<u8>> magic_bytes {};
  46. u64 offset { 0 };
  47. };
  48. Optional<StringView> get_description_from_mime_type(StringView);
  49. }