Make sure image src attribute matches local file or is absolute url

This commit is contained in:
Nimetu 2015-04-19 22:01:46 +03:00
parent eb83692d34
commit bf89af5460
2 changed files with 19 additions and 20 deletions

View file

@ -713,7 +713,7 @@ namespace NLGUI
struct CDataDownload struct CDataDownload
{ {
public: public:
CDataDownload(CURL *c, const std::string &u, FILE *f, TDataType t, CViewBase *i, const std::string &s, const std::string &m) : curl(c), url(u), luaScript(s), md5sum(m), type(t), fp(f) CDataDownload(CURL *c, const std::string &u, const std::string &d, FILE *f, TDataType t, CViewBase *i, const std::string &s, const std::string &m) : curl(c), url(u), dest(d), luaScript(s), md5sum(m), type(t), fp(f)
{ {
if (t == ImgType) imgs.push_back(i); if (t == ImgType) imgs.push_back(i);
} }
@ -721,6 +721,7 @@ namespace NLGUI
public: public:
CURL *curl; CURL *curl;
std::string url; std::string url;
std::string dest;
std::string luaScript; std::string luaScript;
std::string md5sum; std::string md5sum;
TDataType type; TDataType type;

View file

@ -177,13 +177,15 @@ namespace NLGUI
// Add a image download request in the multi_curl // Add a image download request in the multi_curl
void CGroupHTML::addImageDownload(const string &url, CViewBase *img) void CGroupHTML::addImageDownload(const string &url, CViewBase *img)
{ {
string finalUrl = getAbsoluteUrl(url);
// Search if we are not already downloading this url. // Search if we are not already downloading this url.
for(uint i = 0; i < Curls.size(); i++) for(uint i = 0; i < Curls.size(); i++)
{ {
if(Curls[i].url == url) if(Curls[i].url == finalUrl)
{ {
#ifdef LOG_DL #ifdef LOG_DL
nlwarning("already downloading '%s' img %p", url.c_str(), img); nlwarning("already downloading '%s' img %p", finalUrl.c_str(), img);
#endif #endif
Curls[i].imgs.push_back(img); Curls[i].imgs.push_back(img);
return; return;
@ -192,12 +194,13 @@ namespace NLGUI
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_URL, finalUrl.c_str());
// use requested url for local name
string dest = localImageName(url); string dest = localImageName(url);
string tmpdest = localImageName(url)+".tmp"; string tmpdest = localImageName(url)+".tmp";
#ifdef LOG_DL #ifdef LOG_DL
nlwarning("add to download '%s' dest '%s' img %p", url.c_str(), dest.c_str(), img); nlwarning("add to download '%s' dest '%s' img %p", finalUrl.c_str(), dest.c_str(), img);
#endif #endif
// erase the tmp file if exists // erase the tmp file if exists
@ -216,7 +219,7 @@ namespace NLGUI
curl_easy_setopt(curl, CURLOPT_FILE, fp); curl_easy_setopt(curl, CURLOPT_FILE, fp);
curl_multi_add_handle(MultiCurl, curl); curl_multi_add_handle(MultiCurl, curl);
Curls.push_back(CDataDownload(curl, url, fp, ImgType, img, "", "")); Curls.push_back(CDataDownload(curl, finalUrl, dest, fp, ImgType, img, "", ""));
#ifdef LOG_DL #ifdef LOG_DL
nlwarning("adding handle %x, %d curls", curl, Curls.size()); nlwarning("adding handle %x, %d curls", curl, Curls.size());
#endif #endif
@ -311,7 +314,7 @@ namespace NLGUI
curl_easy_setopt(curl, CURLOPT_FILE, fp); curl_easy_setopt(curl, CURLOPT_FILE, fp);
curl_multi_add_handle(MultiCurl, curl); curl_multi_add_handle(MultiCurl, curl);
Curls.push_back(CDataDownload(curl, url, fp, BnpType, NULL, script, md5sum)); Curls.push_back(CDataDownload(curl, url, dest, fp, BnpType, NULL, script, md5sum));
#ifdef LOG_DL #ifdef LOG_DL
nlwarning("adding handle %x, %d curls", curl, Curls.size()); nlwarning("adding handle %x, %d curls", curl, Curls.size());
#endif #endif
@ -453,34 +456,29 @@ namespace NLGUI
curl_multi_remove_handle(MultiCurl, it->curl); curl_multi_remove_handle(MultiCurl, it->curl);
curl_easy_cleanup(it->curl); curl_easy_cleanup(it->curl);
string file; string tmpfile = it->dest + ".tmp";
if (it->type == ImgType) if(res != CURLE_OK || r < 200 || r >= 300 || ((it->md5sum != "") && (it->md5sum != getMD5(tmpfile).toString())))
file = localImageName(it->url);
else
file = localBnpName(it->url);
if(res != CURLE_OK || r < 200 || r >= 300 || ((it->md5sum != "") && (it->md5sum != getMD5(file+".tmp").toString())))
{ {
NLMISC::CFile::deleteFile((file+".tmp").c_str()); NLMISC::CFile::deleteFile(tmpfile.c_str());
} }
else else
{ {
string finalUrl; string finalUrl;
if (it->type == ImgType) if (it->type == ImgType)
{ {
CFile::moveFile(file.c_str(), (file+".tmp").c_str()); CFile::moveFile(it->dest.c_str(), tmpfile.c_str());
if (lookupLocalFile (finalUrl, file.c_str(), false)) //if (lookupLocalFile (finalUrl, file.c_str(), false))
{ {
for(uint i = 0; i < it->imgs.size(); i++) for(uint i = 0; i < it->imgs.size(); i++)
{ {
setImage(it->imgs[i], file); setImage(it->imgs[i], it->dest);
} }
} }
} }
else else
{ {
CFile::moveFile(file.c_str(), (file+".tmp").c_str()); CFile::moveFile(it->dest.c_str(), tmpfile.c_str());
if (lookupLocalFile (finalUrl, file.c_str(), false)) //if (lookupLocalFile (finalUrl, file.c_str(), false))
{ {
CLuaManager::getInstance().executeLuaScript( it->luaScript, true ); CLuaManager::getInstance().executeLuaScript( it->luaScript, true );
} }