From 73f6e9df2a8b5ecffea06ae8d03744432fb265d4 Mon Sep 17 00:00:00 2001 From: Marc Szymkowiak Date: Fri, 19 Feb 2016 21:34:53 +0100 Subject: [PATCH] add getmovies control Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/f323f61785015a33ea130c764967d1c0901223a5 Author: Marc Szymkowiak Date: 2016-02-19 (Fri, 19 Feb 2016) --- src/nhttpd/tuxboxapi/controlapi.cpp | 152 +++++++++++++++++++++++++++- src/nhttpd/tuxboxapi/controlapi.h | 2 + 2 files changed, 153 insertions(+), 1 deletion(-) diff --git a/src/nhttpd/tuxboxapi/controlapi.cpp b/src/nhttpd/tuxboxapi/controlapi.cpp index cde36f552..c9610f635 100644 --- a/src/nhttpd/tuxboxapi/controlapi.cpp +++ b/src/nhttpd/tuxboxapi/controlapi.cpp @@ -225,7 +225,8 @@ const CControlAPI::TyCgiCall CControlAPI::yCgiCallList[]= // filehandling {"file", &CControlAPI::FileCGI, "+xml"}, {"statfs", &CControlAPI::StatfsCGI, "+xml"}, - {"getdir", &CControlAPI::getDirCGI, "+xml"} + {"getdir", &CControlAPI::getDirCGI, "+xml"}, + {"getmovies", &CControlAPI::getMoviesCGI, "+xml"} }; //----------------------------------------------------------------------------- // Main Dispatcher @@ -3550,3 +3551,152 @@ std::string CControlAPI::getSubdirectories(CyhookHandler *hh, std::string path, } return result; } + +//----------------------------------------------------------------------------- +/** Get neutrino movies + * + * @param hh CyhookHandler + * + * @par nhttpd-usage + * @code + * /control/getmovies?ir=allmoviedirs&[&subdirs=true][&format=|xml|json] + * @endcode + * +{"success": "true", "data":{"movies": [{"title": "Sample.mkv", +"path": "/media/sda1/movies/Sample.mkv", +"size": "1136242099" +} +,{"title": "Aufnahme1.ts", +"path": "/media/sda1/recording/Aufnahme1.ts", +"size": "941" +} +,{"title": "Aufnahme2.ts", +"path": "/media/sda1/recording/Aufnahme2.ts", +"size": "941" +} +] +}} + * @endcode + * + */ +//----------------------------------------------------------------------------- +void CControlAPI::getMoviesCGI(CyhookHandler *hh) { + std::string result = ""; + bool isFirstLine = true; + bool subdirs = true; + + if(hh->ParamList["subdirs"] == "false") { + subdirs = false; + } + + TOutType outType = hh->outStart(); + + //Shows all movies with path in moviebrowser.conf + if (hh->ParamList["dir"] == "moviedir" || hh->ParamList["dir"] == "allmoviedirs" ) { + CConfigFile *Config = new CConfigFile(','); + Config->loadConfig(MOVIEBROWSER_CONFIGFILE); + char index[21]; + std::string mb_dir; + + for(int i=0;i<8;i++) { + snprintf(index, sizeof(index), "%d", i); + mb_dir = "mb_dir_"; + mb_dir = mb_dir + index; + mb_dir = Config->getString(mb_dir, ""); + + if(!mb_dir.empty()) { + result = readMovies(hh, mb_dir, result, subdirs); + } + } + } + + //Shows all movies in the recordingdir + if (hh->ParamList["dir"] == "recordingdir" || hh->ParamList["dir"] == "allmoviedirs" ) { + result = readMovies(hh, g_settings.network_nfs_recordingdir, result, subdirs); + } + + //Shows movie from a given path + if (hh->ParamList["dir"][0] == '/') { + result = readMovies(hh, hh->ParamList["dir"], result, subdirs); + } + + result = hh->outArray("movies", result); + // write footer + if (outType == json) { + hh->WriteLn(json_out_success(result)); + } + else { + hh->WriteLn(result); + } +} + +//Helpfunction to get subdirs of a dir +std::string CControlAPI::getSubdirectories(CyhookHandler *hh, std::string path, std::string result) { + std::string item = ""; + std::string dirname; + DIR *dirp; + struct dirent *entry; + + if ((dirp = opendir(path.c_str()))) { + while ((entry = readdir(dirp))) { + if (entry->d_type == DT_DIR && entry->d_name[0] != '.') { + if (path[path.length() - 1] != '/') { + path += "/"; + } + std::string fullname = path + entry->d_name; + item += hh->outPair("dir", hh->outValue(fullname), false); + result += hh->outNext(); + result += hh->outArrayItem("item", item, false); + item = ""; + result = getSubdirectories(hh, fullname, result); + } + } + closedir(dirp); + } + return result; +} + +//Helpfunction to get movies of a dir +std::string CControlAPI::readMovies(CyhookHandler *hh, std::string path, std::string result, bool subdirs) { + std::string item = ""; + std::string fullname; + DIR *dirp; + struct dirent *entry; + + if ((dirp = opendir(path.c_str()))) { + if (path[path.length() - 1] != '/') { + path += "/"; + } + while ((entry = readdir(dirp))) { + if(entry->d_type == 8) { + fullname = path + entry->d_name; + item += hh->outPair("title", hh->outValue(entry->d_name),true); + item += hh->outPair("path", hh->outValue(fullname), true); + struct stat statbuf; + if (stat(fullname.c_str(), &statbuf) != -1) { + /* Print size of file. */ + item += hh->outPair("size", string_printf("%jd", (intmax_t) statbuf.st_size), false); + } + if(!result.empty()) { + result += hh->outNext(); + } + result += hh->outArrayItem("item", item, false); + item = ""; + } + } + closedir(dirp); + if ((dirp = opendir(path.c_str()))) { + if(subdirs) + { + while ((entry = readdir(dirp))) { + if (entry->d_type == DT_DIR && entry->d_name[0] != '.') { + fullname = path + entry->d_name; + result = readMovies(hh, fullname, result, subdirs); + } + } + } + closedir(dirp); + } + } + return result; +} diff --git a/src/nhttpd/tuxboxapi/controlapi.h b/src/nhttpd/tuxboxapi/controlapi.h index 95bdef974..238dc1cf8 100644 --- a/src/nhttpd/tuxboxapi/controlapi.h +++ b/src/nhttpd/tuxboxapi/controlapi.h @@ -130,6 +130,8 @@ private: void StatfsCGI(CyhookHandler *hh); void SignalInfoCGI(CyhookHandler *hh); void getDirCGI(CyhookHandler *hh); + void getMoviesCGI(CyhookHandler *hh); + std::string readMovies(CyhookHandler *hh, std::string path, std::string result, bool subdirs); std::string getSubdirectories(CyhookHandler *hh, std::string path, std::string result);