From f5754b74f2646c7a183a3ccdcac1499d7512ad38 Mon Sep 17 00:00:00 2001 From: svenhoefer Date: Tue, 18 Sep 2018 21:18:40 +0200 Subject: [PATCH] - helpers: rework new random-functions to get the code more reuseable * introduceand use LOGODIR_TMP (storage dir for webchannel logos) Signed-off-by: Thilo Graf --- src/driver/pictureviewer/pictureviewer.cpp | 9 ++++--- src/eitd/xmlutil.cpp | 5 ++-- src/global.h | 1 + src/system/helpers.cpp | 30 +++++++++++----------- src/system/helpers.h | 5 ++-- src/zapit/src/bouquets.cpp | 2 +- src/zapit/src/channel.cpp | 10 ++++---- 7 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/driver/pictureviewer/pictureviewer.cpp b/src/driver/pictureviewer/pictureviewer.cpp index bd9e738b2..1002dbd26 100644 --- a/src/driver/pictureviewer/pictureviewer.cpp +++ b/src/driver/pictureviewer/pictureviewer.cpp @@ -659,13 +659,14 @@ bool CPictureViewer::GetLogoName(const uint64_t& channel_id, const std::string& } } - if (cc) { + if (cc) + { if (!cc->getAlternateLogo().empty()) { - std::string lname = dlTmpName(cc->getAlternateLogo()); - name = lname; - if(width && height) + std::string lname = downloadUrlToRandomFile(cc->getAlternateLogo(), LOGODIR_TMP); + if (width && height) getSize(lname.c_str(), width, height); + name = lname; cc->setAlternateLogo(lname); return true; } diff --git a/src/eitd/xmlutil.cpp b/src/eitd/xmlutil.cpp index 35f38e6b1..ebaa25e27 100644 --- a/src/eitd/xmlutil.cpp +++ b/src/eitd/xmlutil.cpp @@ -644,13 +644,14 @@ void *insertEventsfromXMLTV(void * data) pthread_exit(NULL); } std::string url = (char *) data; - std::string url_ext = getFileExt(url); - std::string tmp_name = genTmpName(url_ext,8); + std::string tmp_name = randomFile(getFileExt(url), "/tmp", 8); int64_t now = time_monotonic_ms(); if (url.compare(0, 1, "/") == 0) + { readEventsFromXMLTV(url, ev_count); + } else if (::downloadUrl(url, tmp_name)) { readEventsFromXMLTV(tmp_name, ev_count); diff --git a/src/global.h b/src/global.h index a113639cf..d609a3cf2 100644 --- a/src/global.h +++ b/src/global.h @@ -51,6 +51,7 @@ #define LOGODIR ICONSDIR "/logo" #define LOGODIR_VAR ICONSDIR_VAR "/logo" +#define LOGODIR_TMP "/tmp/.logo" NEUTRINO_CPP SNeutrinoSettings g_settings; NEUTRINO_CPP SglobalInfo g_info; diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index 5d701f9d6..e3dfd40f2 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -1527,32 +1527,32 @@ bool utf8_check_is_valid(const std::string &str) return true; } -std::string genTmpName(std::string suffix,unsigned int length) +std::string randomString(unsigned int length) { + std::string random = ""; const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; - int stringLength = sizeof(alphanum) - 1; - std::string Str; unsigned int i; - Str.append("/tmp/"); - for( i = 0; i < length; ++i) - { - Str += alphanum[rand() % stringLength]; - } - Str += "."; - Str += suffix; - return Str; + for(i = 0; i < length; ++i) + random += alphanum[rand() % (sizeof(alphanum) - 1)]; + return random; } -std::string dlTmpName(std::string url) +std::string randomFile(std::string suffix, std::string directory, unsigned int length) +{ + mkdir(directory.c_str(), 0755); + return directory + "/" + randomString(length) + "." + suffix; +} + +std::string downloadUrlToRandomFile(std::string url, std::string directory, unsigned int length) { if (strstr(url.c_str(), "://")) { - std::string tmpname = genTmpName(url.substr(url.find_last_of(".")+1),10); - if (downloadUrl(url,tmpname)) - url = tmpname; + std::string file = randomFile(url.substr(url.find_last_of(".") + 1), directory, length); + if (downloadUrl(url, file)) + return file; } return url; } diff --git a/src/system/helpers.h b/src/system/helpers.h index b45f3db83..55d841a4d 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -159,8 +159,9 @@ std::string readFile(std::string file); std::string iso_8859_1_to_utf8(std::string &str); bool utf8_check_is_valid(const std::string &str); -std::string genTmpName(std::string suffix,unsigned int length); -std::string dlTmpName(std::string url); +std::string randomString(unsigned int length = 10); +std::string randomFile(std::string suffix = "tmp", std::string directory = "/tmp", unsigned int length = 10); +std::string downloadUrlToRandomFile(std::string url, std::string directory = "/tmp", unsigned int length = 10); // curl struct MemoryStruct { diff --git a/src/zapit/src/bouquets.cpp b/src/zapit/src/bouquets.cpp index 3c350ad6b..f03691b2a 100644 --- a/src/zapit/src/bouquets.cpp +++ b/src/zapit/src/bouquets.cpp @@ -853,7 +853,7 @@ void CBouquetManager::loadWebchannels(int mode) { std::string filename = (*it); std::string extension = getFileExt(filename); - std::string tmp_name = genTmpName(extension,8); + std::string tmp_name = randomFile(extension, LOGODIR_TMP); bool remove_tmp = false; if (filename.compare(0, 1, "/") == 0) diff --git a/src/zapit/src/channel.cpp b/src/zapit/src/channel.cpp index 3489ec6b5..44af7deb3 100644 --- a/src/zapit/src/channel.cpp +++ b/src/zapit/src/channel.cpp @@ -21,6 +21,8 @@ */ #include + +#include #include #include #include @@ -110,7 +112,7 @@ void CZapitChannel::Init() bLockCount = 0; bLocked = DEFAULT_CH_LOCKED; bUseCI = false; - thrLogo = NULL; + thrLogo = 0; altlogo = ""; } @@ -442,11 +444,9 @@ void CZapitChannel::setThrAlternateLogo(const std::string &pLogo) void* CZapitChannel::LogoThread(void* channel) { CZapitChannel *cc = (CZapitChannel *)channel; - std::string lname = cc->getAlternateLogo(); - cc->setAlternateLogo(dlTmpName(lname)); + if (cc) + cc->setAlternateLogo(downloadUrlToRandomFile(cc->getAlternateLogo(), LOGODIR_TMP)); pthread_exit(0); - return NULL; - }