helpers: rework new random-functions to get the code more reuseable

* introduceand use LOGODIR_TMP (storage dir for webchannel logos)


Origin commit data
------------------
Branch: ni/coolstream
Commit: 2d3f66aa39
Author: vanhofen <vanhofen@gmx.de>
Date: 2018-09-18 (Tue, 18 Sep 2018)

Origin message was:
------------------
- helpers: rework new random-functions to get the code more reuseable

* introduceand use LOGODIR_TMP (storage dir for webchannel logos)


------------------
This commit was generated by Migit
This commit is contained in:
vanhofen
2018-09-18 21:18:40 +02:00
parent 3f7838b248
commit c6319e8a8c
7 changed files with 33 additions and 29 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -52,6 +52,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;

View File

@@ -1714,32 +1714,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;
}

View File

@@ -163,8 +163,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 {

View File

@@ -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)

View File

@@ -21,6 +21,8 @@
*/
#include <cstdio>
#include <global.h>
#include <system/helpers.h>
#include <zapit/zapit.h>
#include <zapit/channel.h>
@@ -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;
}