#include "mologram/ui/media/MediaFormat.hpp" #include #include namespace mologram::ui::media { MediaFormat sniffFormat(const std::string& b) { auto at = [&](size_t i) -> unsigned char { return i < b.size() ? (unsigned char)b[i] : 0; }; auto has = [&](size_t off, const char* s) { for (size_t i = 0; s[i]; i++) if (at(off + i) != (unsigned char)s[i]) return false; return true; }; if (b.size() >= 8 && at(0) == 0x89 && has(1, "PNG")) return MediaFormat::Png; if (at(0) == 0xFF && at(1) == 0xD8 && at(2) == 0xFF) return MediaFormat::Jpeg; if (has(0, "GIF8")) return MediaFormat::Gif; if (has(0, "RIFF") && has(8, "WEBP")) return MediaFormat::Webp; if (has(0, "RIFF") && has(8, "WAVE")) return MediaFormat::Wav; if (at(0) == 0x1f && at(1) == 0x8b) return MediaFormat::Tgs; if (has(4, "ftyp")) return MediaFormat::Mp4; if (at(0) == 0x1A && at(1) == 0x45 && at(2) == 0xDF && at(3) == 0xA3) return MediaFormat::Webm; if (has(0, "OggS")) return MediaFormat::Ogg; if (has(0, "ID3") || (at(0) == 0xFF && (at(1) & 0xE0) == 0xE0)) return MediaFormat::Mp3; if (has(0, "%PDF")) return MediaFormat::Pdf; return MediaFormat::Unknown; } const char* formatName(MediaFormat f) { switch (f) { case MediaFormat::Png: return "png"; case MediaFormat::Jpeg: return "jpeg"; case MediaFormat::Gif: return "gif"; case MediaFormat::Webp: return "webp"; case MediaFormat::Tgs: return "tgs"; case MediaFormat::Mp4: return "mp4"; case MediaFormat::Webm: return "webm"; case MediaFormat::Ogg: return "ogg"; case MediaFormat::Mp3: return "mp3"; case MediaFormat::Wav: return "wav"; case MediaFormat::Pdf: return "pdf"; default: return "unknown"; } } const char* formatExtension(MediaFormat f) { switch (f) { case MediaFormat::Png: return "png"; case MediaFormat::Jpeg: return "jpg"; case MediaFormat::Gif: return "gif"; case MediaFormat::Webp: return "webp"; case MediaFormat::Tgs: return "tgs"; case MediaFormat::Mp4: return "mp4"; case MediaFormat::Webm: return "webm"; case MediaFormat::Ogg: return "ogg"; case MediaFormat::Mp3: return "mp3"; case MediaFormat::Wav: return "wav"; case MediaFormat::Pdf: return "pdf"; default: return ""; } } bool isVideo(MediaFormat f) { return f == MediaFormat::Mp4 || f == MediaFormat::Webm; } bool isAudio(MediaFormat f) { return f == MediaFormat::Ogg || f == MediaFormat::Mp3 || f == MediaFormat::Wav; } std::string sendKindForPath(const std::string& path, bool asDocument) { if (asDocument) return "document"; size_t dot = path.rfind('.'); std::string ext = dot == std::string::npos ? "" : path.substr(dot + 1); std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return (char)std::tolower(c); }); for (const char* e : {"jpg", "jpeg", "png", "webp", "bmp"}) if (ext == e) return "photo"; for (const char* e : {"mp4", "mov", "mkv", "webm", "avi", "m4v"}) if (ext == e) return "video"; for (const char* e : {"ogg", "opus", "oga"}) if (ext == e) return "voice"; for (const char* e : {"mp3", "m4a", "flac", "wav", "aac"}) if (ext == e) return "audio"; return "document"; } } // namespace mologram::ui::media