代码拉取完成,页面将自动刷新
#include "ehome_resimg.h"
#include <QDir>
#include <QFileInfo>
#include <QApplication>
#include <QGuiApplication>
#include <QCryptographicHash>
static size_t downloadCallback(void *buffer, size_t sz, size_t nmemb, void *writer)
{
string* psResponse = (string*) writer;
size_t size = sz * nmemb;
psResponse->append((char*) buffer, size);
return sz * nmemb;
}
ehome_resimg::ehome_resimg(QObject *parent) : QObject(parent)
{
isbuffer = true;
m_AppPath = QGuiApplication::applicationDirPath()+"/";
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
void ehome_resimg::setIsbuffer(bool value)
{
isbuffer = value;
}
ehome_resimg::~ehome_resimg()
{
if (manager != NULL)
{
manager->deleteLater();
manager = NULL;
}
}
void ehome_resimg::setlocalpath(QString path)
{
QString uuid = QUuid::createUuid().toString().remove("-").remove("{").remove("}");
QDir checker(QString(m_AppPath+"appres/").toLocal8Bit());
if (!checker.exists())
{
if (!checker.mkpath(m_AppPath+"appres/"))
{
}
}
m_imagepath =m_AppPath+"appres/"+uuid+".png";
QFile::copy(path,m_imagepath);
if (QFileInfo::exists(m_imagepath))
{
emit sign_flesh();
return;
}
}
void ehome_resimg::getimage(QString qurl)
{
QString pwd=qurl;
if(this->property("id").toInt()!=0)
{
m_imagepath = QString("%1/%2-%3").arg(this->property("imagedir").toString())
.arg(this->property("id").toString()).arg(this->property("imageName").toString());
}
else
{
QString md5;
QByteArray ba,bb;
QCryptographicHash md(QCryptographicHash::Md5);
// ba.append()
ba.append(pwd.toLocal8Bit());
md.addData(ba);
bb = md.result();
md5.append(bb.toHex());
QDir checker(QString(m_AppPath+"appres/").toLocal8Bit());
if (!checker.exists())
{
if (!checker.mkpath(m_AppPath+"appres/"))
{
}
}
if(this->property("albumid").toString().isEmpty())
{
m_imagepath =m_AppPath+"appres/"+md5+".png";
}else
{
m_imagepath =m_AppPath+"appres/"+QString("%1-%2").arg(this->property("albumid").toString()).arg(md5)+".png";
}
}
if (isbuffer)//是否启用缓存
{
if (QFileInfo::exists(m_imagepath))
{
emit sign_flesh();
// qDebug()<<"启用了缓存记忆";
return;
}
}
else
{
// qDebug()<<"未启用了缓存记忆";
}
// auto it = new Worker(m_imagepath,qurl);
// it->setIsdown(false);
// connect(it,SIGNAL(sign_reflesh()),this,SIGNAL(sign_flesh()));
// it->start();
}
void ehome_resimg::downloadimage(QString savepath, QString qurl)
{
// auto it = new Worker(savepath,qurl);
// it->start();
}
void ehome_resimg::getimage(QString url, QString uid)
{
QDir checker(QString(m_AppPath+"appres/").toLocal8Bit());
if (!checker.exists())
{
if (!checker.mkpath(m_AppPath+"appres/"))
{
}
}
m_imagepath =m_AppPath+"appres/"+uid+".jpg";
if (QFileInfo::exists(m_imagepath))
{
emit sign_flesh();
return;
}
QNetworkRequest header;
header.setUrl(url);
// header.setRawHeader("uid",uid.toLocal8Bit());
manager->get(header);
}
QPixmap ehome_resimg::getImage() const
{
return m_image;
}
void ehome_resimg::setImage(const QPixmap &image)
{
m_image = image;
}
#include <QLabel>
void ehome_resimg::replyFinished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
m_image.loadFromData(reply->readAll());
m_image.save(m_imagepath);
emit sign_flesh();
reply->deleteLater();
reply = NULL;
}
}
/*
CURLcode ehome_resimg::curl_get_req(const string &url, string &response)
{
// init curl
CURL *curl = curl_easy_init();
// res code
CURLcode res;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3000); // set transport and time out time
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5000);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
return res;
}
Worker::Worker(QString savepath, QString qurl)
{
m_qurl = qurl;
m_savepath = savepath;
}
void Worker::setIsdown(bool isdown)
{
m_isdown = isdown;
}
CURLcode Worker::curl_get_req(const string &url, string &response)
{
// init curl
CURL *curl = curl_easy_init();
// res code
CURLcode res;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3000); // set transport and time out time
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5000);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
return res;
}
void Worker::run()
{
if(m_isdown)
{
QString fileurl = m_qurl.mid(0,m_qurl.indexOf("?")).remove("http://");
QStringList datas = fileurl.split("/");
QString filename = datas.last();
QString saveurl = m_savepath+"/"+filename;
if (QFileInfo::exists(saveurl))
{
emit sign_finished(filename);
return;
}
string url = m_qurl.toStdString();
string postResponseStr;
auto res = curl_get_req(url,postResponseStr);
QByteArray data;
data.resize(postResponseStr.length());
memcpy(data.data(),postResponseStr.c_str(),postResponseStr.length());
if (res == CURLE_OK)
{
m_image.loadFromData(data);
m_image.save(saveurl);
emit sign_finished(filename);
}
data.clear();
}
else
{
string url = m_qurl.toStdString();
string postResponseStr;
auto res = curl_get_req(url,postResponseStr);
QByteArray data;
data.resize(postResponseStr.length());
memcpy(data.data(),postResponseStr.c_str(),postResponseStr.length());
if (res == CURLE_OK)
{
m_image.loadFromData(data);
m_image.save(m_savepath);
emit sign_reflesh();
}
data.clear();
}
}*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。