加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ehome_resimg.cpp 7.37 KB
一键复制 编辑 原始数据 按行查看 历史
编号001 提交于 2024-11-03 21:21 . init
#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();
}
}*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化