Skip to content

Commit

Permalink
Merge remote-tracking branch 'QSyncUi/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaidenShogunShadow committed Jul 9, 2024
2 parents d003747 + 872593e commit 489e2fd
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
historycard.h historycard.cpp
historyview.h historyview.cpp
historyviewcard.h historyviewcard.cpp
cosclient.h cosclient.cpp
setexcludeditems_view.h setexcludeditems_view.cpp
xmlprocesser.h xmlprocesser.cpp
bucket.h bucket.cpp
Expand Down
199 changes: 199 additions & 0 deletions cosclient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include "cosclient.h"
#include "qeventloop.h"
#include <QUrlQuery>
#include <QNetworkReply>
COSClient::COSClient(QObject *parent)
{
return ;
}

COSClient::COSClient(QString bucketName, QString appId, QString region, QString secretId, QString secretKey, QString token, QDateTime expiredTime, QObject *parent)
{
this->bucketName = bucketName;
this->appId = appId;
this->region = region;
this->secretId = secretId;
this->secretKey = secretKey;
this->token = token;
this->expiredTime = expiredTime;
this->endpoint = bucketName + "-" + appId + ".cos." + region + ".myqcloud.com";
this->generalApiUrl = "https://" + endpoint;
this->signHelper = new SignHelper(appId, secretId, secretKey);
manager = new QNetworkAccessManager(this);
}

QString COSClient::listObjects(const QString &prefix, const QString &delimiter)
{
QMap<QString, QString> queryParams;
queryParams.insert("prefix", prefix);
queryParams.insert("delimiter", delimiter);
return invokeGetRequest("/", queryParams);
}

QString COSClient::invokeGetRequest(const QString &path, const QMap<QString, QString> queryParams)
{
QNetworkRequest request = buildGetRequest(path, queryParams);
qDebug()<<request.url();
QNetworkReply *reply = manager->get(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
return reply->readAll();
}

bool COSClient::invokePutRequest(const QString &path, const QMap<QString, QString> queryParams, const QByteArray &data)
{
QNetworkRequest request = buildPutRequest(path, queryParams,data);
QNetworkReply *reply = manager->put(request, data);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
return reply->error()==QNetworkReply::NoError;
}

bool COSClient::invokeHeadRequest(const QString &path, const QMap<QString, QString> queryParams)
{
QNetworkRequest request = buildHeadRequest(path, queryParams);
QNetworkReply *reply = manager->head(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
return reply->error()==QNetworkReply::NoError;
}

bool COSClient::invokeDeleteRequest(const QString &path, const QMap<QString, QString> queryParams)
{
QNetworkRequest request = buildDeleteRequest(path, queryParams);
QNetworkReply *reply = manager->deleteResource(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
return reply->error()==QNetworkReply::NoError;
}

bool COSClient::preCheckSession()
{
if (token == "")
{
return true;
}
if (expiredTime < QDateTime::currentDateTime())
{
qDebug() << expiredTime;
qDebug() << QDateTime::currentDateTime();
return false;
}
return true;
}

QNetworkRequest COSClient::buildGetRequest(const QString &path, const QMap<QString, QString> queryParams)
{
if (!preCheckSession())
{
return QNetworkRequest();
}
QUrl url;
QUrlQuery query;
url=QUrl(generalApiUrl+path);
for(auto it=queryParams.begin();it!=queryParams.end();it++)
{
query.addQueryItem(it.key(),it.value());
}
qDebug()<<url;
url.setQuery(query);
qDebug()<<url;
QNetworkRequest request(url);
qDebug()<<request.url();
request.setRawHeader("Host",endpoint.toUtf8());
//设置请求方法
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"GET");
QString sig=this->signHelper->generateSignature(request,300);
request.setRawHeader("Authorization",sig.toUtf8());
if(token!="")
{
request.setRawHeader("x-cos-security-token",token.toUtf8());
}
return request;

}

QNetworkRequest COSClient::buildPutRequest(const QString &path, const QMap<QString, QString> queryParams, const QByteArray &data)
{
if (!preCheckSession())
{
return QNetworkRequest();
}
QUrl url;
QUrlQuery query;
url=QUrl(generalApiUrl+path);
for(auto it=queryParams.begin();it!=queryParams.end();it++)
{
query.addQueryItem(it.key(),it.value());
}
url.setQuery(query);
QNetworkRequest request(url);
request.setRawHeader("Host",endpoint.toUtf8());
//设置请求方法
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"PUT");
QString sig=this->signHelper->generateSignature(request,300);
request.setRawHeader("Authorization",sig.toUtf8());
if(token!="")
{
request.setRawHeader("x-cos-security-token",token.toUtf8());
}
return request;
}

QNetworkRequest COSClient::buildHeadRequest(const QString &path, const QMap<QString, QString> queryParams)
{
if (!preCheckSession())
{
return QNetworkRequest();
}
QUrl url;
QUrlQuery query;
url=QUrl(generalApiUrl+path);
for(auto it=queryParams.begin();it!=queryParams.end();it++)
{
query.addQueryItem(it.key(),it.value());
}
url.setQuery(query);
QNetworkRequest request(url);
request.setRawHeader("Host",endpoint.toUtf8());
//设置请求方法
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"HEAD");
QString sig=this->signHelper->generateSignature(request,300);
request.setRawHeader("Authorization",sig.toUtf8());
if(token!="")
{
request.setRawHeader("x-cos-security-token",token.toUtf8());
}
return request;
}

QNetworkRequest COSClient::buildDeleteRequest(const QString &path, const QMap<QString, QString> queryParams)
{
if (!preCheckSession())
{
return QNetworkRequest();
}
QUrl url;
QUrlQuery query;
url=QUrl(generalApiUrl+path);
for(auto it=queryParams.begin();it!=queryParams.end();it++)
{
query.addQueryItem(it.key(),it.value());
}
url.setQuery(query);
QNetworkRequest request(url);
request.setRawHeader("Host",endpoint.toUtf8());
//设置请求方法
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"DELETE");
QString sig=this->signHelper->generateSignature(request,300);
request.setRawHeader("Authorization",sig.toUtf8());
if(token!="")
{
request.setRawHeader("x-cos-security-token",token.toUtf8());
}
return request;
}
60 changes: 60 additions & 0 deletions cosclient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef COSCLIENT_H
#define COSCLIENT_H
#include "qdatetime.h"
#include "signhelper.h"
#include <QString>
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QUrl>
class COSClient : public QObject
{
Q_OBJECT
public:
COSClient(QObject *parent = nullptr);
COSClient(QString bucketName, QString appId, QString region, QString secretId, QString secretKey, QString token, QDateTime expiredTime,QObject *parent = nullptr);
QString getBucketName() const { return bucketName; }
QString getAppId() const { return appId; }
QString getRegion() const { return region; }
QString getSecretId() const { return secretId; }
QString getSecretKey() const { return secretKey; }
QString getEndpoint() const { return endpoint; }
QString getGeneralApiUrl() const { return generalApiUrl; }
QString getToken() const { return token; }
QDateTime getExpiredTime() const { return expiredTime; }
void setBucketName(const QString &value) { bucketName = value; }
void setAppId(const QString &value) { appId = value; }
void setRegion(const QString &value) { region = value; }
void setNewSession(QString secretId, QString secretKey, QString token, QDateTime expiredTime){
this->signHelper = new SignHelper(appId, secretId, secretKey);
this->secretId = secretId;
this->secretKey = secretKey;
this->token = token;
this->expiredTime = expiredTime;
}
QString listObjects(const QString &prefix, const QString &delimiter);

private:
QString bucketName;
QString appId;
QString region;
QString secretId;
QString secretKey;
QString endpoint;
QString generalApiUrl;
QString token;
QDateTime expiredTime;
SignHelper* signHelper;
QNetworkAccessManager* manager;
bool preCheckSession();
QNetworkRequest buildGetRequest(const QString& path,const QMap<QString, QString> queryParams);
QNetworkRequest buildPutRequest(const QString& path,const QMap<QString, QString> queryParams, const QByteArray& data);
QNetworkRequest buildHeadRequest(const QString& path,const QMap<QString, QString> queryParams);
QNetworkRequest buildDeleteRequest(const QString& path,const QMap<QString, QString> queryParams);
QString invokeGetRequest(const QString& path,const QMap<QString, QString> queryParams);
bool invokePutRequest(const QString& path,const QMap<QString, QString> queryParams, const QByteArray& data);
bool invokeHeadRequest(const QString& path,const QMap<QString, QString> queryParams);
bool invokeDeleteRequest(const QString& path,const QMap<QString, QString> queryParams);
};
#endif // COSCLIENT_H
14 changes: 11 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "synccore.h"
#include "signhelper.h"
#include "tasktoken.h"

#include "cosclient.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Expand All @@ -15,11 +15,19 @@ int main(int argc, char *argv[])
User loginuser("enrolluser@example.com","123456");
loginuser.login();
TaskToken tt=loginuser.getTaskTokenByRemote("home/");
SignHelper sh("100037680555",tt.tmpSecretId,tt.tmpSecretKey);
//request: https://qsync-1320107701.cos.ap-nanjing.myqcloud.com/?prefix=home
QNetworkRequest request(QUrl("https://qsync-1320107701.cos.ap-nanjing.myqcloud.com/?prefix=home"));
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"GET");
sh.generateSignature(request,60);
QString bucketName="qsync";
QString appId="1320107701";
QString region="ap-nanjing";
QString secretId=tt.tmpSecretId;
QString secretKey=tt.tmpSecretKey;
QString token=tt.sessionToken;
QDateTime expiredTime=tt.expiredTime;
COSClient cosclient(bucketName,appId,region,secretId,secretKey,token,expiredTime);
qDebug()<<cosclient.listObjects("home/","");
//sh.generateSignature(request,60);
//qDebug() <<"s3 location:"<< loginuser.getS3Location();
SyncTaskDatabaseManager stm(&loginuser);
SyncTask mytask("E:/","/home/",1);
Expand Down

0 comments on commit 489e2fd

Please sign in to comment.