Skip to content

Commit

Permalink
Merge branch 'main' of https://gitee.com/opguess/QSyncUi
Browse files Browse the repository at this point in the history
  • Loading branch information
Firefly-26710 committed Jul 9, 2024
2 parents d5c9e91 + b9d3630 commit 348c28c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
113 changes: 113 additions & 0 deletions cosclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,55 @@ bool COSClient::putLocalObject(const QString &path, const QString &localpath)
return putObject(path, data, contentType);
}

QString COSClient::initMultiUpload(const QString &path, QMap<QString, QString> metaDatas,const QString &contentType)
{
preRequest request;
//build metadata
QMap<QString, QString> headers;
for(auto it = metaDatas.begin(); it != metaDatas.end(); ++it)
{
headers.insert("x-cos-meta-" + it.key(), it.value());
}
request.customHeaders = headers;
request.contentType = contentType;
request.queryParams.insert("uploads", "");
preResponse response = invokePostRequest(path, request);
//解析xml:InitiateMultipartUploadResult-》UploadId
QDomDocument doc;
doc.setContent(response.data);
QDomElement root = doc.documentElement();
QDomElement uploadIdElement = root.firstChildElement("UploadId");
return uploadIdElement.text();
}

QString COSClient::initLocalMultiUpload(const QString &path, const QString &localpath, QMap<QString, QString> metaDatas)
{
QString contentType = _getContentTypeByPath(localpath); // Assuming this function exists and returns a content type based on the file extension
return initMultiUpload(path, metaDatas, contentType);
}

QString COSClient::uploadPart(const QString &path, const QString &uploadId, int partNumber, const QByteArray &data)
{
preRequest request;
request.data = data;
request.contentType = "application/octet-stream";
request.queryParams.insert("partNumber", QString::number(partNumber));
request.queryParams.insert("uploadId", uploadId);
preResponse response = invokePutRequest(path, request);
return response.headers.value("ETag");
}

QString COSClient::completeMultipartUpload(QString path, QString uploadId, QMap<int, QString> partEtagMap)
{
preRequest request;
request.data = buildCompleteUploadXml(partEtagMap).toUtf8();
request.contentType = "application/xml";
request.queryParams.insert("uploadId", uploadId);
preResponse response = invokePostRequest(path, request);
//xml, wait for processing
return QString::fromUtf8(response.data);
}

QByteArray COSClient::getObject(const QString &path, const QString &versionId, QMap<QString,QString> &respHeaders)
{
preRequest request;
Expand Down Expand Up @@ -95,6 +144,32 @@ bool COSClient::save2Local(const QString &path, const QString &localpath, const
return true;
}

QString COSClient::multiUpload(const QString &path, const QString &localpath, QMap<QString, QString> metaDatas)
{
//判断文件存在且可读
QFile file(localpath);
if (!file.open(QIODevice::ReadOnly))
{
return "";
}
//初始化分片上传
QString uploadId = initLocalMultiUpload(path, localpath, metaDatas);
//分片上传
int partNumber = 1;
QMap<int, QString> partEtagMap;
while (!file.atEnd())
{
QByteArray data = file.read(5 * 1024 * 1024); // Assuming 5MB parts
QString etag = uploadPart(path, uploadId, partNumber, data);
partEtagMap.insert(partNumber, etag);
partNumber++;
}
//完成分片上传
QString result = completeMultipartUpload(path, uploadId, partEtagMap);
file.close();
return result;
}

// 修改后的函数实现

preResponse COSClient::invokeGetFileRequest(const QString& path, const preRequest& request) {
Expand Down Expand Up @@ -249,6 +324,42 @@ QMap<QString, QString> COSClient::parseTagXmlToMap(const QString &xmlString) {
return map;
}

QString COSClient::buildCompleteUploadXml(QMap<int, QString>partEtagMap)
{
/*
<CompleteMultipartUpload>
<Part>
<PartNumber>integer</PartNumber>
<ETag>string</ETag>
</Part>
<Part>
<PartNumber>integer</PartNumber>
<ETag>string</ETag>
</Part>
</CompleteMultipartUpload>
*/
QDomDocument doc;
QDomElement root = doc.createElement("CompleteMultipartUpload");
doc.appendChild(root);

QMapIterator<int, QString> i(partEtagMap);
while (i.hasNext()) {
i.next();
QDomElement part = doc.createElement("Part");
QDomElement partNumber = doc.createElement("PartNumber");
QDomElement eTag = doc.createElement("ETag");
QDomText partNumberText = doc.createTextNode(QString::number(i.key()));
QDomText eTagText = doc.createTextNode(i.value());
partNumber.appendChild(partNumberText);
eTag.appendChild(eTagText);
part.appendChild(partNumber);
part.appendChild(eTag);
root.appendChild(part);
}

return doc.toString();
}

bool COSClient::preCheckSession()
{
if (token == "")
Expand Down Expand Up @@ -492,6 +603,7 @@ QNetworkRequest COSClient::buildPostRequest(const QString &path, const QMap<QStr
return QNetworkRequest();
}
QUrl url;
qDebug()<<queryParams;
QUrlQuery query;
url=QUrl(generalApiUrl+path);
for(auto it=queryParams.begin();it!=queryParams.end();it++)
Expand All @@ -500,6 +612,7 @@ QNetworkRequest COSClient::buildPostRequest(const QString &path, const QMap<QStr
}
url.setQuery(query);
QNetworkRequest request(url);
qDebug()<<request.url();
request.setRawHeader("Host",endpoint.toUtf8());
//设置请求方法
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"POST");
Expand Down
8 changes: 6 additions & 2 deletions cosclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <QUrl>
#include <QDomDocument>
struct preRequest{
QString path;
QMap<QString, QString> queryParams;
QByteArray data;
QString contentType;
Expand Down Expand Up @@ -59,9 +58,13 @@ class COSClient : public QObject
QString listObjects(const QString &prefix, const QString &delimiter);
bool putObject(const QString &path, const QByteArray &data,const QString &contentType="application/octet-stream");
bool putLocalObject(const QString &path, const QString &localpath);
QString initMultiUpload(const QString &path,const QMap<QString,QString> metaDatas=QMap<QString,QString>(),const QString &contentType="application/octet-stream");
QString initLocalMultiUpload(const QString &path, const QString &localpath,QMap<QString,QString> metaDatas=QMap<QString,QString>());
QString uploadPart(const QString &path, const QString &uploadId, int partNumber, const QByteArray &data);
QString completeMultipartUpload(QString path, QString uploadId, QMap<int, QString> partEtagMap);
QByteArray getObject(const QString &path,const QString &versionId, QMap<QString,QString> &respHeaders);
bool save2Local(const QString &path, const QString &localpath,const QString &versionId, QMap<QString,QString> &respMetaDatas);

QString multiUpload(const QString &path, const QString &localpath, QMap<QString,QString> metaDatas=QMap<QString,QString>());

private:
QString bucketName;
Expand Down Expand Up @@ -93,5 +96,6 @@ class COSClient : public QObject

// 从XML字符串解析为QMap
QMap<QString, QString> parseTagXmlToMap(const QString &xmlString);
QString buildCompleteUploadXml(QMap<int, QString>);
};
#endif // COSCLIENT_H
7 changes: 7 additions & 0 deletions linknewfolder_window.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "linknewfolder_window.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include "ElaCheckBox.h"
#include "ElaLineEdit.h"
#include "ElaPushButton.h"
Expand Down Expand Up @@ -36,6 +37,12 @@ linkNewFolder_window::linkNewFolder_window(QWidget *parent)
lineEditLayout1->addWidget(folderName1); // 将输入框添加到布局中
lineEditLayout1->addStretch();
lineEditLayout1->addWidget(_pushButton1); // 将按钮添加到布局中
connect(_pushButton1, &ElaPushButton::clicked, [=]() {
QString folderPath = QFileDialog::getExistingDirectory(this, "选择文件夹", QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!folderPath.isEmpty()) {
folderName1->setText(folderPath);
}
});

QWidget* lineEditArea2 = new QWidget();
lineEditArea2->setWindowFlags(Qt::FramelessWindowHint); // 去除窗口边框
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int main(int argc, char *argv[])
COSClient cosclient(bucketName,appId,region,secretId,secretKey,token,expiredTime);
QString xmlstr=cosclient.listObjects("home/","");
qDebug()<<cosclient.putLocalObject("/home/1.jpg","C:/Users/33327/Documents/Tencent Files/3332770186/Image/Group2/LN/[B/LN[BMJILV2RA]X6$X49XPGS.jpg");
qDebug()<<cosclient.multiUpload("/home/2.mp4","E:\\Game Recording\\Yuan Shen 原神\\3.mp4");
XmlProcesser xp;
Bucket bucket=xp.processXml(xmlstr);

Expand Down
3 changes: 3 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ void MainWindow::onLoginResponse(const int &code, const QJsonObject &data, const
void MainWindow::onCloseButtonClicked()
{
ElaContentDialog *dialag = new ElaContentDialog(this);
dialag->setLeftButtonText("取消");
dialag->setMiddleButtonText("最小化");
dialag->setRightButtonText("退出");
connect(dialag, &ElaContentDialog::rightButtonClicked, this, &MainWindow::closeWindow);
connect(dialag, &ElaContentDialog::middleButtonClicked, this, &MainWindow::showMinimized);
dialag->show();
Expand Down

0 comments on commit 348c28c

Please sign in to comment.