| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "cachewebpage.h"
- #include "httpdownload.h"
- #include "sncdatabase.h"
- CacheWebPage::CacheWebPage(QString pageUrl, QString cachePath, QObject *parent):
- QThread(parent)
- {
- this->pageUrl = pageUrl;
- this->cachePath = cachePath;
- this->stop = false;
- createPageCachePath();
- }
- CacheWebPage::~CacheWebPage()
- {
- qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
- <<"CacheWebPage Destroyed.";
- }
- void CacheWebPage::createPageCachePath()
- {
- QString md5;
- QByteArray ba;
- ba = QCryptographicHash::hash ( this->pageUrl.toAscii(), QCryptographicHash::Md5 );
- md5.append(ba.toHex());
- QDir dir(this->cachePath);
- if(!dir.exists(md5))
- {
- dir.mkdir(md5);
- }
- if(this->cachePath.endsWith('/'))
- this->cachePath += md5 + "/";
- else
- this->cachePath += "/" + md5 + "/";
- }
- void CacheWebPage::abort()
- {
- this-> stop = true;
- }
- void CacheWebPage::downloadPage()
- {
- HttpDownload download;
- QString storePath = createLocalDirByUrl(this->pageUrl);
- int retCode = download.downloadFile(this->pageUrl,storePath);
- if(retCode<0)
- return ;
- if(stop)
- return ;
- QUrl url(this->pageUrl);
- QFileInfo fileInfo(url.path());
- QFile file(storePath + fileInfo.fileName());
- if(!file.open(QIODevice::ReadOnly))
- return ;
- QTextStream in(&file);
- QString souceText = in.readAll();
- file.close();
- QStringList elements = getMatchElementFromPage(souceText);
- QFileInfo fileInfo2(this->pageUrl);
- for(int i=0;i<elements.size();i++)
- {
- if(stop)
- break;
- QString link = this->pageUrl.replace(fileInfo2.fileName(),"") + elements.at(i);
- QString path = createLocalDirByUrl(link);
- HttpDownload down;
- down.downloadFile(link,path);
- }
- }
- QString CacheWebPage::createLocalDirByUrl(QString pageUrl)
- {
- QUrl url(pageUrl);
- QString path = this->cachePath;
- QStringList subDirs = url.path().split("/");
- QDir dir(path);
- if(subDirs.size()>2)
- {
- for(int i=1;i<subDirs.size()-1;i++)
- {
- path += subDirs.at(i) + "/";
- if(!dir.exists(path))
- {
- dir.mkdir(path);
- }
- }
- }
- return path;
- }
- QStringList CacheWebPage::getMatchElementFromPage(QString souceText)
- {
- QRegExp rx ("[a-zA-Z0-9]+/?[a-zA-Z0-9]+\\.{1}((jpg)|(gif)|(bmp)|(ico)|(png)|(js)|(css))") ;
- int pos = 0;
- QStringList elements;
- while ((pos = rx.indexIn(souceText, pos)) != -1)
- {
- pos += rx.matchedLength();
- elements<<rx.capturedTexts().at(0);
- }
- return elements;
- }
- void CacheWebPage::run()
- {
- downloadPage();
- }
|