forked from Green-Sky/tomato
refactor file from messages to objects (#27)
* part 1 move files from messages to objects tomato - did not touch chat_gui yet, but image loaders and other stuff * part 1.1 * part 2, mostly chatgui - also ngcft1 behind the scenes * part 3 - port over rest, except for avatar_manager, which is effectivly disabled * fix surface missused causing bmp loader to crash * fixing small issues and small forward refactor
This commit is contained in:
@ -7,71 +7,117 @@
|
||||
|
||||
#include <solanaceae/message3/components.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include "./os_comps.hpp"
|
||||
|
||||
#include <solanaceae/object_store/object_store.hpp>
|
||||
|
||||
#include <solanaceae/file/file2.hpp>
|
||||
|
||||
#include <limits>
|
||||
#include <iostream>
|
||||
|
||||
void MediaMetaInfoLoader::handleMessage(const Message3Handle& m) {
|
||||
if (m.any_of<Message::Components::TagNotImage, Message::Components::FrameDims>()) {
|
||||
if (!static_cast<bool>(m)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m.all_of<Message::Components::Transfer::FileInfoLocal, Message::Components::Transfer::TagHaveAll>()) {
|
||||
// move to obj
|
||||
if (m.any_of<Message::Components::TagNotImage>()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& fil = m.get<Message::Components::Transfer::FileInfoLocal>();
|
||||
if (fil.file_list.size() != 1) {
|
||||
if (!m.all_of<Message::Components::MessageFileObject>()) {
|
||||
// not a file message
|
||||
return;
|
||||
}
|
||||
const auto& o = m.get<Message::Components::MessageFileObject>().o;
|
||||
|
||||
if (!static_cast<bool>(o)) {
|
||||
std::cerr << "MMIL error: invalid object in file message\n";
|
||||
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<uint8_t> 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<Message::Components::FrameDims>(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<Message::Components::TagNotImage>();
|
||||
|
||||
std::cout << "MMIL loading failed image info " << fil.file_list.front() << "\n";
|
||||
|
||||
_rmm.throwEventUpdate(m);
|
||||
}
|
||||
if (o.any_of<ObjComp::F::FrameDims>()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!o.all_of<ObjComp::F::TagLocalHaveAll>()) {
|
||||
return; // we dont have all data
|
||||
}
|
||||
|
||||
if (!o.all_of<ObjComp::Ephemeral::Backend, ObjComp::F::SingleInfo>()) {
|
||||
std::cerr << "MMIL error: object missing backend/file info (?)\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: handle collections
|
||||
const auto file_size = o.get<ObjComp::F::SingleInfo>().file_size;
|
||||
|
||||
if (file_size > 50*1024*1024) {
|
||||
std::cerr << "MMIL error: image file too large\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (file_size == 0) {
|
||||
std::cerr << "MMIL warning: empty file\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!o.all_of<ObjComp::F::TagLocalHaveAll>()) {
|
||||
// not ready yet
|
||||
return;
|
||||
}
|
||||
|
||||
auto* file_backend = o.get<ObjComp::Ephemeral::Backend>().ptr;
|
||||
if (file_backend == nullptr) {
|
||||
std::cerr << "MMIL error: object backend nullptr\n";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
auto file2 = file_backend->file2(o, StorageBackendI::FILE2_READ);
|
||||
if (!file2 || !file2->isGood() || !file2->can_read) {
|
||||
std::cerr << "MMIL error: creating file2 from object via backendI\n";
|
||||
return;
|
||||
}
|
||||
|
||||
auto read_data = file2->read(file_size, 0);
|
||||
if (read_data.ptr == nullptr) {
|
||||
std::cerr << "MMIL error: reading from file2 returned nullptr\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (read_data.size != file_size) {
|
||||
std::cerr << "MMIL error: reading from file2 size missmatch, should be " << file_size << ", is " << read_data.size << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// try all loaders after another
|
||||
for (auto& il : _image_loaders) {
|
||||
// TODO: impl callback based load
|
||||
auto res = il->loadInfoFromMemory(read_data.ptr, read_data.size);
|
||||
if (res.height == 0 || res.width == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
o.emplace<ObjComp::F::FrameDims>(
|
||||
static_cast<uint16_t>(std::min<uint32_t>(res.width, std::numeric_limits<uint16_t>::max())),
|
||||
static_cast<uint16_t>(std::min<uint32_t>(res.height, std::numeric_limits<uint16_t>::max()))
|
||||
);
|
||||
|
||||
std::cout << "MMIL: loaded image file o:" << /*file_path*/ entt::to_integral(o.entity()) << "\n";
|
||||
|
||||
_rmm.throwEventUpdate(m);
|
||||
return;
|
||||
}
|
||||
|
||||
m.emplace<Message::Components::TagNotImage>();
|
||||
|
||||
std::cout << "MMIL: loading failed image info o:" << /*file_path*/ entt::to_integral(o.entity()) << "\n";
|
||||
|
||||
// TODO: update object too
|
||||
// recursion
|
||||
_rmm.throwEventUpdate(m);
|
||||
}
|
||||
|
||||
MediaMetaInfoLoader::MediaMetaInfoLoader(RegistryMessageModel& rmm) : _rmm(rmm) {
|
||||
|
Reference in New Issue
Block a user