Commit 625249b0 authored by jan.koester's avatar jan.koester
Browse files

test

parent 3110dfda
Loading
Loading
Loading
Loading
+216 −51
Changes for plugins/media/media.cpp: 216 added lines, 51 removed lines.
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ namespace blogi {
        json_object* newAlbum(const int tid,libhttppp::HttpRequest &req,const std::string &sessionid){
            std::string _lang = getLang(*Args->database[tid]);
            std::string albumname;
            std::string selectedStore;

            libhttppp::HttpForm form;
            form.parse(req);
@@ -83,12 +84,14 @@ namespace blogi {
            for(const auto &curdat : form.urlData()){
                if(curdat.key=="albumname")
                    albumname=curdat.value;
                else if(curdat.key=="store_name")
                    selectedStore=curdat.value;
            }

            if(!albumname.empty()){
            if(!albumname.empty() && !selectedStore.empty()){
                std::string token = _getSessionToken(tid, sessionid);
                _ensureMdbReady(tid, token);
                std::string storeId = _getStoreId(tid);
                std::string storeId = _getStoreId(tid, selectedStore);
                _mdb[tid]->create_album(storeId, albumname);
            }

@@ -100,6 +103,21 @@ namespace blogi {

            json_object *jfields = json_object_new_array();

            // Store selection dropdown
            json_object *jfStore = json_object_new_object();
            json_object_object_add(jfStore, "name", json_object_new_string("store_name"));
            json_object_object_add(jfStore, "label", json_object_new_string(tr(_lang,"Store").c_str()));
            json_object_object_add(jfStore, "type", json_object_new_string("select"));
            json_object *jStoreOptions = json_object_new_array();
            for(const auto &sn : _mdbStores){
                json_object *jopt = json_object_new_object();
                json_object_object_add(jopt, "value", json_object_new_string(sn.c_str()));
                json_object_object_add(jopt, "label", json_object_new_string(sn.c_str()));
                json_object_array_add(jStoreOptions, jopt);
            }
            json_object_object_add(jfStore, "options", jStoreOptions);
            json_object_array_add(jfields, jfStore);

            json_object *jfAlbum = json_object_new_object();
            json_object_object_add(jfAlbum, "name", json_object_new_string("albumname"));
            json_object_object_add(jfAlbum, "label", json_object_new_string(tr(_lang,"Album name").c_str()));
@@ -239,6 +257,7 @@ namespace blogi {
        json_object* settingsConfig(const int tid, libhttppp::HttpRequest &req){
            std::string _lang = getLang(*Args->database[tid]);
            bool saved = false;
            std::string action, newStoreName;

            libhttppp::HttpForm form;
            form.parse(req);
@@ -247,23 +266,28 @@ namespace blogi {
                if(cur.key=="mediadb_url"){
                    setConfig(*Args->database[tid],"MEDIADB_URL",cur.value);
                    saved = true;
                }else if(cur.key=="mediadb_store"){
                    setConfig(*Args->database[tid],"MEDIADB_STORE",cur.value);
                }else if(cur.key=="action"){
                    action = cur.value;
                }else if(cur.key=="store_name"){
                    newStoreName = cur.value;
                }
            }

            if(action == "add_store" && !newStoreName.empty()){
                _addStore(tid, newStoreName);
                saved = true;
            } else if(action == "remove_store" && !newStoreName.empty()){
                _removeStore(tid, newStoreName);
                saved = true;
            }

            if(saved){
                // Reload settings and reset clients
                try { getConfig(*Args->database[tid],"MEDIADB_URL",_mdbUrl); } catch(...) {}
                try { getConfig(*Args->database[tid],"MEDIADB_STORE",_mdbStoreName); } catch(...) {}
                _mdbStoreId.clear();
                _mdbStoreIds.clear();
                _mdbHttpUrl = std::make_unique<libhttppp::HttpUrl>(_mdbUrl);
                for(auto &c : _mdb) c.reset();
            }

            // Read current values
            std::string url = _mdbUrl, store = _mdbStoreName;

            json_object *jroot = json_object_new_object();
            json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"MediaDB Configuration").c_str()));

@@ -279,6 +303,7 @@ namespace blogi {
            json_object_array_add(jnav, jback);
            json_object_object_add(jroot, "nav", jnav);

            // URL config form
            json_object *jform = json_object_new_object();
            json_object_object_add(jform, "method", json_object_new_string("POST"));
            json_object_object_add(jform, "action", json_object_new_string(
@@ -286,22 +311,70 @@ namespace blogi {

            json_object *jfields = json_object_new_array();

            auto addField = [&](const char *name, const char *label, const char *type, const std::string &value){
                json_object *jf = json_object_new_object();
                json_object_object_add(jf, "name", json_object_new_string(name));
                json_object_object_add(jf, "label", json_object_new_string(label));
                json_object_object_add(jf, "type", json_object_new_string(type));
                json_object_object_add(jf, "value", json_object_new_string(value.c_str()));
                json_object_array_add(jfields, jf);
            };

            addField("mediadb_url",   tr(_lang,"MediaDB URL").c_str(),   "text",     url);
            addField("mediadb_store", tr(_lang,"Store Name").c_str(),    "text",     store);
            json_object *jfUrl = json_object_new_object();
            json_object_object_add(jfUrl, "name", json_object_new_string("mediadb_url"));
            json_object_object_add(jfUrl, "label", json_object_new_string(tr(_lang,"MediaDB URL").c_str()));
            json_object_object_add(jfUrl, "type", json_object_new_string("text"));
            json_object_object_add(jfUrl, "value", json_object_new_string(_mdbUrl.c_str()));
            json_object_array_add(jfields, jfUrl);

            json_object_object_add(jform, "fields", jfields);
            json_object_object_add(jform, "submit", json_object_new_string(tr(_lang,"save").c_str()));
            json_object_object_add(jroot, "form", jform);

            // Stores table
            json_object *jtable = json_object_new_object();
            json_object *jheaders = json_object_new_array();
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Store Name").c_str()));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Actions").c_str()));
            json_object_object_add(jtable, "headers", jheaders);

            json_object *jrows = json_object_new_array();
            for(const auto &storeName : _mdbStores){
                json_object *jrow = json_object_new_object();
                json_object *jcells = json_object_new_array();
                json_object_array_add(jcells, json_object_new_string(storeName.c_str()));
                json_object_object_add(jrow, "cells", jcells);

                json_object *jactions = json_object_new_array();
                json_object *jremove = json_object_new_object();
                json_object_object_add(jremove, "label", json_object_new_string(tr(_lang,"Remove").c_str()));
                std::string removeUrl = Args->config->buildurl("settings/media/config?action=remove_store&store_name=")
                                      + storeName;
                json_object_object_add(jremove, "url", json_object_new_string(removeUrl.c_str()));
                json_object_array_add(jactions, jremove);
                json_object_object_add(jrow, "actions", jactions);

                json_object_array_add(jrows, jrow);
            }
            json_object_object_add(jtable, "rows", jrows);
            json_object_object_add(jroot, "table", jtable);

            // Add store form
            json_object *jform2 = json_object_new_object();
            json_object_object_add(jform2, "method", json_object_new_string("POST"));
            json_object_object_add(jform2, "action", json_object_new_string(
                Args->config->buildurl("settings/media/config").c_str()));

            json_object *jfields2 = json_object_new_array();

            json_object *jfAction = json_object_new_object();
            json_object_object_add(jfAction, "name", json_object_new_string("action"));
            json_object_object_add(jfAction, "type", json_object_new_string("hidden"));
            json_object_object_add(jfAction, "value", json_object_new_string("add_store"));
            json_object_array_add(jfields2, jfAction);

            json_object *jfStore = json_object_new_object();
            json_object_object_add(jfStore, "name", json_object_new_string("store_name"));
            json_object_object_add(jfStore, "label", json_object_new_string(tr(_lang,"New Store Name").c_str()));
            json_object_object_add(jfStore, "type", json_object_new_string("text"));
            json_object_object_add(jfStore, "value", json_object_new_string(""));
            json_object_array_add(jfields2, jfStore);

            json_object_object_add(jform2, "fields", jfields2);
            json_object_object_add(jform2, "submit", json_object_new_string(tr(_lang,"Add Store").c_str()));
            json_object_object_add(jroot, "form2", jform2);

            return jroot;
        }

@@ -487,20 +560,10 @@ namespace blogi {

            json_object_object_add(jroot, "nav", jnav);

            // Albums table — list all albums from mediadb
            std::string storeId;
            std::vector<mediadb::AlbumInfo> albums;
            try {
                storeId = _getStoreId(tid);
                albums = _mdb[tid]->list_albums(storeId);
            } catch(std::exception &e) {
                std::cerr << "media plugin: list_albums failed: " << e.what() << std::endl;
            } catch(...) {
                std::cerr << "media plugin: list_albums failed (unknown exception)" << std::endl;
            }

            // Albums table — list albums from all stores
            json_object *jtable = json_object_new_object();
            json_object *jheaders = json_object_new_array();
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Store").c_str()));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Album name").c_str()));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Media count").c_str()));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Created").c_str()));
@@ -508,9 +571,24 @@ namespace blogi {
            json_object_object_add(jtable, "headers", jheaders);

            json_object *jrows = json_object_new_array();
            for(const auto &storeName : _mdbStores){
                std::string storeId;
                std::vector<mediadb::AlbumInfo> albums;
                try {
                    storeId = _getStoreId(tid, storeName);
                    albums = _mdb[tid]->list_albums(storeId);
                } catch(std::exception &e) {
                    std::cerr << "media plugin: list_albums failed for store '" << storeName << "': " << e.what() << std::endl;
                    continue;
                } catch(...) {
                    std::cerr << "media plugin: list_albums failed for store '" << storeName << "' (unknown exception)" << std::endl;
                    continue;
                }

                for(const auto &album : albums){
                    json_object *jrow = json_object_new_object();
                    json_object *jcells = json_object_new_array();
                    json_object_array_add(jcells, json_object_new_string(storeName.c_str()));
                    json_object_array_add(jcells, json_object_new_string(album.name.c_str()));
                    json_object_array_add(jcells, json_object_new_string(std::to_string(album.media_count).c_str()));
                    json_object_array_add(jcells, json_object_new_string(album.created_at.c_str()));
@@ -533,6 +611,7 @@ namespace blogi {
                    json_object_object_add(jrow, "actions", jactions);
                    json_object_array_add(jrows, jrow);
                }
            }
            json_object_object_add(jtable, "rows", jrows);
            json_object_object_add(jroot, "table", jtable);

@@ -555,9 +634,7 @@ namespace blogi {
                mdbUrl = "http://127.0.0.1:8080";
            _mdbUrl = mdbUrl;

            try { getConfig(*Args->database[0],"MEDIADB_STORE",_mdbStoreName); } catch(...) {}
            if(_mdbStoreName.empty())
                _mdbStoreName = "blogi";
            _loadStores(0);

            _mdb.resize(Args->maxthreads);
            _mdbHttpUrl = std::make_unique<libhttppp::HttpUrl>(_mdbUrl);
@@ -844,16 +921,30 @@ namespace blogi {
            if(strcmp(command, "media_list_albums") == 0){
                try {
                    _ensureMdbReady(tid, token);
                    std::string storeId = _getStoreId(tid);
                    auto albums = _mdb[tid]->list_albums(storeId);
                    json_object *jarr = json_object_new_array();

                    // Optional store_name filter; if absent, list from all stores
                    json_object *jsn = nullptr;
                    std::vector<std::string> storesToQuery;
                    if(json_object_object_get_ex(request, "store_name", &jsn)){
                        const char *sn = json_object_get_string(jsn);
                        if(sn && sn[0] != '\0') storesToQuery.push_back(sn);
                    }
                    if(storesToQuery.empty())
                        storesToQuery = _mdbStores;

                    for(const auto &storeName : storesToQuery){
                        std::string storeId = _getStoreId(tid, storeName);
                        auto albums = _mdb[tid]->list_albums(storeId);
                        for(const auto &a : albums){
                            json_object *jal = json_object_new_object();
                            json_object_object_add(jal, "id", json_object_new_string(a.id.c_str()));
                            json_object_object_add(jal, "name", json_object_new_string(a.name.c_str()));
                            json_object_object_add(jal, "store", json_object_new_string(storeName.c_str()));
                            json_object_object_add(jal, "media_count", json_object_new_int(a.media_count));
                            json_object_array_add(jarr, jal);
                        }
                    }
                    json_object_object_add(response, "status", json_object_new_string("success"));
                    json_object_object_add(response, "albums", jarr);
                } catch(std::exception &e) {
@@ -963,6 +1054,13 @@ namespace blogi {
                    json_object_object_add(response, "status", json_object_new_string("error"));
                    json_object_object_add(response, "message", json_object_new_string("Size not found"));
                }
            } else if(strcmp(command, "media_list_stores") == 0){
                json_object *jarr = json_object_new_array();
                for(const auto &s : _mdbStores){
                    json_object_array_add(jarr, json_object_new_string(s.c_str()));
                }
                json_object_object_add(response, "status", json_object_new_string("success"));
                json_object_object_add(response, "stores", jarr);
            }
        }

@@ -989,24 +1087,91 @@ namespace blogi {
            }
        }

        std::string _getStoreId(int tid){
            if(!_mdbStoreId.empty())
                return _mdbStoreId;

        std::string _getStoreId(int tid, const std::string &storeName){
            {
                std::lock_guard<std::mutex> guard(_mdbSetupMutex);
            if(!_mdbStoreId.empty())
                return _mdbStoreId;
                auto it = _mdbStoreIds.find(storeName);
                if(it != _mdbStoreIds.end())
                    return it->second;
            }

            auto stores = _mdb[tid]->list_stores();

            std::lock_guard<std::mutex> guard(_mdbSetupMutex);
            // Double-check after network call
            auto it = _mdbStoreIds.find(storeName);
            if(it != _mdbStoreIds.end())
                return it->second;

            for(const auto &s : stores){
                if(s.name == _mdbStoreName){
                    _mdbStoreId = s.id;
                    return _mdbStoreId;
                if(s.name == storeName){
                    _mdbStoreIds[storeName] = s.id;
                    return s.id;
                }
            }
            // Store not found, create one
            _mdbStoreId = _mdb[tid]->create_store(_mdbStoreName).id;
            return _mdbStoreId;
            std::string newId = _mdb[tid]->create_store(storeName).id;
            _mdbStoreIds[storeName] = newId;
            return newId;
        }

        const std::vector<std::string>& _getStoreNames() const {
            return _mdbStores;
        }

        void _loadStores(int tid){
            std::string json;
            try { getConfig(*Args->database[tid], "MEDIADB_STORES", json); } catch(...) {}
            _mdbStores.clear();
            if(!json.empty()){
                json_object *arr = json_tokener_parse(json.c_str());
                if(arr && json_object_is_type(arr, json_type_array)){
                    size_t len = json_object_array_length(arr);
                    for(size_t i = 0; i < len; ++i){
                        json_object *item = json_object_array_get_idx(arr, i);
                        const char *s = json_object_get_string(item);
                        if(s && s[0] != '\0')
                            _mdbStores.push_back(s);
                    }
                }
                if(arr) json_object_put(arr);
            }
            // Backward compat: if MEDIADB_STORES is empty, try single MEDIADB_STORE
            if(_mdbStores.empty()){
                std::string singleStore;
                try { getConfig(*Args->database[tid], "MEDIADB_STORE", singleStore); } catch(...) {}
                if(singleStore.empty())
                    singleStore = "blogi";
                _mdbStores.push_back(singleStore);
            }
        }

        void _saveStores(int tid){
            json_object *arr = json_object_new_array();
            for(const auto &s : _mdbStores){
                json_object_array_add(arr, json_object_new_string(s.c_str()));
            }
            setConfig(*Args->database[tid], "MEDIADB_STORES",
                      json_object_to_json_string_ext(arr, JSON_C_TO_STRING_NOSLASHESCAPE));
            json_object_put(arr);
        }

        bool _addStore(int tid, const std::string &storeName){
            for(const auto &s : _mdbStores){
                if(s == storeName) return false;
            }
            _mdbStores.push_back(storeName);
            _saveStores(tid);
            return true;
        }

        bool _removeStore(int tid, const std::string &storeName){
            auto it = std::find(_mdbStores.begin(), _mdbStores.end(), storeName);
            if(it == _mdbStores.end()) return false;
            _mdbStores.erase(it);
            _mdbStoreIds.erase(storeName);
            _saveStores(tid);
            return true;
        }

        // ---- streaming state per-request ----
@@ -1056,8 +1221,8 @@ namespace blogi {
        // ---- member state ----

        std::string _mdbUrl;
        std::string _mdbStoreName;
        std::string _mdbStoreId;
        std::vector<std::string> _mdbStores;
        std::map<std::string, std::string> _mdbStoreIds;
        std::vector<std::unique_ptr<mediadb::Client>> _mdb;
        std::unique_ptr<libhttppp::HttpUrl> _mdbHttpUrl;
        std::mutex _mdbSetupMutex;