#include "./media_meta_info_loader.hpp" #include "./image_loader_webp.hpp" #include "./image_loader_sdl_bmp.hpp" #include "./image_loader_qoi.hpp" #include "./image_loader_stb.hpp" #include #include #include void MediaMetaInfoLoader::handleMessage(const Message3Handle& m) { if (m.any_of()) { return; } if (!m.all_of()) { return; } const auto& fil = m.get(); if (fil.file_list.size() != 1) { return; } std::ifstream file(fil.file_list.front(), std::ios::binary); if (file.is_open()) { // figure out size file.seekg(0, file.end); if (file.tellg() > 50*1024*1024) { // TODO: conf // dont try load files larger 50mb return; } file.seekg(0, file.beg); std::vector tmp_buffer; while (file.good()) { auto ch = file.get(); if (ch == EOF) { break; } else { tmp_buffer.push_back(ch); } } bool could_load {false}; // try all loaders after another for (auto& il : _image_loaders) { // TODO: impl callback based load auto res = il->loadInfoFromMemory(tmp_buffer.data(), tmp_buffer.size()); if (res.height == 0 || res.width == 0) { continue; } m.emplace(res.width, res.height); could_load = true; std::cout << "MMIL loaded image info " << fil.file_list.front() << "\n"; _rmm.throwEventUpdate(m); break; } if (!could_load) { m.emplace(); std::cout << "MMIL loading failed image info " << fil.file_list.front() << "\n"; _rmm.throwEventUpdate(m); } } } MediaMetaInfoLoader::MediaMetaInfoLoader(RegistryMessageModel& rmm) : _rmm(rmm) { // HACK: make them be added externally? _image_loaders.push_back(std::make_unique()); _image_loaders.push_back(std::make_unique()); _image_loaders.push_back(std::make_unique()); _image_loaders.push_back(std::make_unique()); _rmm.subscribe(this, RegistryMessageModel_Event::message_construct); _rmm.subscribe(this, RegistryMessageModel_Event::message_updated); } MediaMetaInfoLoader::~MediaMetaInfoLoader(void) { } bool MediaMetaInfoLoader::onEvent(const Message::Events::MessageConstruct& e) { handleMessage(e.e); return false; } bool MediaMetaInfoLoader::onEvent(const Message::Events::MessageUpdated& e) { handleMessage(e.e); return false; }