From 577fc0845718ed8ad5bebf02a277c0579a817f77 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 17 May 2024 17:58:38 +0200 Subject: wanda: restructure source layout --- source/lib/include/wanda/system/magic.hpp | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 source/lib/include/wanda/system/magic.hpp (limited to 'source/lib/include/wanda/system/magic.hpp') diff --git a/source/lib/include/wanda/system/magic.hpp b/source/lib/include/wanda/system/magic.hpp new file mode 100644 index 0000000..a8ea1ba --- /dev/null +++ b/source/lib/include/wanda/system/magic.hpp @@ -0,0 +1,58 @@ +#ifndef WANDA_SYSTEM_MAGIC_HPP +#define WANDA_SYSTEM_MAGIC_HPP + +#include + +#include +#include +#include +#include + +namespace wanda::system +{ + struct magic + { + struct closer + { + auto operator()(magic_t handle) const noexcept -> void + { + magic_close(handle); + } + }; + + enum struct mime_type + { + unknown, + image_jpeg, + image_png, + }; + + magic() + : m_handle{magic_open(MAGIC_MIME_TYPE)} + { + magic_load(m_handle.get(), nullptr); + } + + auto type(std::filesystem::path path) -> mime_type + { + auto magic_type = std::string{magic_file(m_handle.get(), path.native().c_str())}; + + if (magic_type == "image/jpeg") + { + return mime_type::image_jpeg; + } + else if (magic_type == "image/png") + { + return mime_type::image_png; + } + + return mime_type::unknown; + } + + private: + std::unique_ptr, closer> m_handle; + }; + +} // namespace wanda::system + +#endif \ No newline at end of file -- cgit v1.2.3