Compare commits

...

2 Commits

Author SHA1 Message Date
wuyize f65f9f3dbb Merge remote-tracking branch 'origin/main' into main 2023-07-05 21:14:55 +08:00
wuyize d5d707f891 初步实现上传 2023-07-05 21:14:33 +08:00
10 changed files with 379 additions and 123 deletions

View File

@ -108,8 +108,8 @@ AppFluWindow {
}, function (p1, p2) { }, function (p1, p2) {
console.log(p1) console.log(p1)
console.log(p2) console.log(p2)
FluApp.navigate("/") //FluApp.navigate("/")
window.close() //window.close()
btn_login.disabled = false btn_login.disabled = false
btn_login.text = "登录" btn_login.text = "登录"
}) })

View File

@ -89,20 +89,6 @@ FluWindow {
title_bar.maximizeButton.visible = true title_bar.maximizeButton.visible = true
// window.backgroundVisible = false // window.backgroundVisible = false
window.show() window.show()
Request.post(
"knowledge/file", "{\n"
+ " \"name\": \"特东局却热\",\n" + " \"brief\": \"fugiat occaecat\",\n"
+ " \"size\": 88,\n" + " \"sha256\": \"ea incididunt pariatur\",\n"
+ " \"tags\": [\n" + " 43,\n"
+ " 45\n" + " ],\n" + " \"parentId\": \"4\"\n" + "}",
function (p1, p2) {
console.log(p1)
console.log(p2)
}, function (p1, p2) {
console.log(p1)
console.log(p2)
})
} }
} }
} }

View File

@ -2,7 +2,9 @@ import QtQuick
import QtQuick.Controls import QtQuick.Controls
import QtQuick.Window import QtQuick.Window
import QtQuick.Layouts import QtQuick.Layouts
import Qt5Compat.GraphicalEffects
import FluentUI import FluentUI
import AicsKB.FileTransferManager
Popup { Popup {
id: transfer_popup id: transfer_popup
@ -21,21 +23,99 @@ Popup {
anchors.topMargin: 10 anchors.topMargin: 10
ColumnLayout { ColumnLayout {
Layout.fillWidth: true Layout.fillWidth: true
Layout.bottomMargin: -10 spacing: 0
Repeater { Repeater {
model: FileTransferListModel model: FileTransferListModel
delegate: ColumnLayout { delegate: Item {
Layout.bottomMargin: 10 Layout.fillWidth: true
spacing: 10 height: 50
width: parent.width
Text { ColumnLayout {
text: name anchors.fill: parent
anchors.margins: 5
spacing: 2
Text {
text: name
}
FluProgressBar {
Layout.fillWidth: true
progress: completedSize / totalSize
indeterminate: false
}
Text {
color: FluColors.Grey130
text: formatSize(speed) + "/s - " + formatSize(
completedSize) + "/" + formatSize(
totalSize)
/**
* ,
* @param {Number} size
* @param {Number} [pointLength=1]
* @param {Array} [units=[ 'B', 'K', 'M', 'G', 'TB' ]]
* K()M, K.
*/
function formatSize(size, pointLength, units) {
var unit
units = units || ['B', 'KB', 'MB', 'GB', 'TB']
while ((unit = units.shift()) && size > 1024) {
size = size / 1024
}
return (unit === 'B' ? size : size.toFixed(
pointLength === undefined ? 1 : pointLength)) + ' ' + unit
}
}
} }
FluProgressBar { MouseArea {
Layout.fillWidth: true anchors.fill: parent
progress: completedSize / totalSize hoverEnabled: true
indeterminate: false onEntered: file_buttons.visible = true
onExited: file_buttons.visible = false
LinearGradient {
anchors.fill: file_buttons
visible: file_buttons.visible
start: Qt.point(0, 0)
end: Qt.point(20, 0)
gradient: Gradient {
GradientStop {
position: 0.0
color: "transparent"
}
GradientStop {
position: 1.0
color: "#f8f8f8"
}
}
}
RowLayout {
id: file_buttons
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
layoutDirection: Qt.RightToLeft
visible: false
FluIconButton {
iconSource: FluentIcons.Cancel
}
FluIconButton {
Layout.leftMargin: 20
iconSource: paused ? FluentIcons.Play : FluentIcons.Pause
onClicked: {
if (!paused) {
console.log("pause")
FileTransferManager.pause(fileId)
}
}
}
}
} }
} }
} }

View File

@ -5,7 +5,7 @@ import QtQuick
QtObject { QtObject {
id: request id: request
property string baseUrl: "http://127.0.0.1:4523/m1/2914957-0-5604d062/" property string baseUrl: "https://api.hammer-hfut.tk:233/aics/main/"
//property string baseUrl: "http://192.168.156.74:8080/" //property string baseUrl: "http://192.168.156.74:8080/"
// GET // GET

View File

@ -145,7 +145,9 @@ FluArea {
} }
onClicked: { onClicked: {
emit: content_area.download(content_area.knowledgeFileId) emit: content_area.download(content_area.knowledgeFileId)
FileTransferManager.download(content_area.knowledgeFileId) // FileTransferManager.download(content_area.knowledgeFileId)
FileTransferManager.download(
"4973c59e-3ba1-41f2-a57e-3b53c2b5e2a4")
} }
} }
} }
@ -216,7 +218,6 @@ FluArea {
Layout.fillWidth: true Layout.fillWidth: true
implicitHeight: 400 implicitHeight: 400
} }
} }
Component { Component {
id: other_view id: other_view
@ -231,28 +232,28 @@ FluArea {
} }
} }
// Item { // Item {
// Layout.fillWidth: true // Layout.fillWidth: true
// implicitHeight: 50 // implicitHeight: 50
// FluText { // FluText {
// id: text_note // id: text_note
// text: "" // text: ""
// padding: 10 // padding: 10
// font { // font {
// pointSize: 15 // pointSize: 15
// bold: true // bold: true
// } // }
// } // }
// FluTextButton { // FluTextButton {
// id: button_publish // id: button_publish
// text: "" // text: ""
// hoverColor: "blue" // hoverColor: "blue"
// normalColor: "black" // normalColor: "black"
// anchors { // anchors {
// verticalCenter: text_note.verticalCenter // verticalCenter: text_note.verticalCenter
// right: parent.right // right: parent.right
// } // }
// } // }
// } // }
} }
} }

View File

@ -7,6 +7,7 @@ import QtQuick.Dialogs
import FluentUI import FluentUI
import AicsKB.FileTransferManager import AicsKB.FileTransferManager
import "qrc:///AicsKnowledgeBase/qml/component" import "qrc:///AicsKnowledgeBase/qml/component"
import "qrc:///AicsKnowledgeBase/qml/global"
FluArea { FluArea {
property string url: '' property string url: ''
@ -32,7 +33,33 @@ FluArea {
FileDialog { FileDialog {
id: fileDialog id: fileDialog
onAccepted: function () { onAccepted: function () {
FileTransferManager.upload(selectedFile) const size = FileTransferManager.getFileSize(selectedFile)
const md5 = FileTransferManager.getFileMd5(selectedFile)
if (size <= 0 || md5 === '')
return
var body = {
"name": "test2",
"brief": "brief",
"size": size,
"md5": md5,
"tags": [],
"parentId": null
}
console.log("begin")
console.log(JSON.stringify(body))
Request.post("knowledge/file", JSON.stringify(body),
function (res, data) {
console.log(res)
console.log(data)
FileTransferManager.upload(selectedFile,
data.id,
data.ticket)
}, function (res, data) {
console.log(res)
})
} }
} }
} }

View File

@ -7,6 +7,8 @@ FileTransferListModel::FileTransferListModel(QObject *parent)
m_roleName.insert(kNameRole, "name"); m_roleName.insert(kNameRole, "name");
m_roleName.insert(kCompletedSizeRole, "completedSize"); m_roleName.insert(kCompletedSizeRole, "completedSize");
m_roleName.insert(kTotalSizeRole, "totalSize"); m_roleName.insert(kTotalSizeRole, "totalSize");
m_roleName.insert(kSpeedRole, "speed");
m_roleName.insert(kPausedRole, "paused");
m_data = { m_data = {
{"v", "vivo", 1200, 2000}, {"v", "vivo", 1200, 2000},
@ -42,6 +44,10 @@ QVariant FileTransferListModel::data(const QModelIndex &index, int role) const
return m_data.value(index.row()).completedSize; return m_data.value(index.row()).completedSize;
case kTotalSizeRole: case kTotalSizeRole:
return m_data.value(index.row()).totalSize; return m_data.value(index.row()).totalSize;
case kSpeedRole:
return m_data.value(index.row()).speed;
case kPausedRole:
return m_data.value(index.row()).paused;
default: default:
break; break;
} }

View File

@ -11,6 +11,8 @@ struct FileItem
QString name; QString name;
int64_t completedSize; int64_t completedSize;
int64_t totalSize; int64_t totalSize;
int64_t speed;
bool paused = false;
}; };
@ -39,7 +41,9 @@ public:
kIdRole = Qt::UserRole + 1, kIdRole = Qt::UserRole + 1,
kNameRole, kNameRole,
kCompletedSizeRole, kCompletedSizeRole,
kTotalSizeRole kTotalSizeRole,
kSpeedRole,
kPausedRole
}; };
private: private:
explicit FileTransferListModel(QObject *parent = nullptr); explicit FileTransferListModel(QObject *parent = nullptr);

View File

@ -9,7 +9,9 @@
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <fstream> #include <fstream>
static const std::string baseUrl = "http://127.0.0.1:4523/m1/2914957-0-default/"; static const std::string baseUrl = "https://api.hammer-hfut.tk:233/aics/file/";
//static const std::string baseUrl = "http://127.0.0.1:4523/m1/2914957-0-6e5f2db1/";
//static const std::string baseUrl = "https://quic.nginx.org/";
static size_t writeDataCallback(void *contents, size_t size, size_t nmemb, void *userp) static size_t writeDataCallback(void *contents, size_t size, size_t nmemb, void *userp)
{ {
@ -28,24 +30,26 @@ static size_t writeDataCallback(void *contents, size_t size, size_t nmemb, void
return 0; return 0;
} }
CURLcode httpGet(const std::string &url, std::string &response, int timeout = 3000) CURLcode
httpGet(const std::string &url, std::string &response, long httpVersion = CURL_HTTP_VERSION_3ONLY, int timeout = 3000)
{ {
CURLcode res; CURLcode res;
// 获取easy handle // 获取easy handle
CURL *easy_handle = curl_easy_init(); CURL *easy_handle = curl_easy_init();
if (nullptr == easy_handle) { if (!easy_handle) return CURLE_FAILED_INIT;
curl_global_cleanup();
return CURLE_FAILED_INIT;
}
// 设置easy handle属性 // 设置easy handle属性
//curl_easy_setopt(easy_handle, CURLOPT_HTTP_VERSION, (long) CURL_HTTP_VERSION_3ONLY); curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(easy_handle, CURLOPT_HTTP_VERSION, httpVersion);
curl_easy_setopt(easy_handle, CURLOPT_URL, (baseUrl + url).c_str()); curl_easy_setopt(easy_handle, CURLOPT_URL, (baseUrl + url).c_str());
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, writeDataCallback); curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, writeDataCallback);
curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, timeout); curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, (void *) &response); curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, (void *) &response);
curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, 1L);
//curl_easy_setopt(easy_handle, CURLOPT_DEFAULT_PROTOCOL, "https"); //curl_easy_setopt(easy_handle, CURLOPT_DEFAULT_PROTOCOL, "https");
//curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 0L);
//curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYHOST, 0L);//不验证证书和HOST
// 执行数据请求 // 执行数据请求
res = curl_easy_perform(easy_handle); res = curl_easy_perform(easy_handle);
curl_easy_cleanup(easy_handle); curl_easy_cleanup(easy_handle);
@ -55,55 +59,78 @@ CURLcode httpGet(const std::string &url, std::string &response, int timeout = 30
FileTransferManager::FileTransferManager(QObject *parent) FileTransferManager::FileTransferManager(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
std::string resData;
if (CURLE_OK == httpGet("file/hello", resData))
qDebug() << "HTTP3 OK\n" << resData.c_str();
else
qDebug() << "HTTP3 FAILED";
} }
int64_t completedSize = 0;
void FileTransferManager::upload(QUrl fileUrl) size_t readDataFunc(void *ptr, size_t size, size_t nmemb, void *file)
{
auto s = reinterpret_cast<QFile *>(file)->read(reinterpret_cast<char *>(ptr),
size * nmemb);
completedSize += s;
qDebug() << std::format("Uploading: {} / {}", completedSize,
reinterpret_cast<QFile *>(file)->size()).c_str();
return s;
}
void FileTransferManager::upload(const QUrl &fileUrl, const QString &fileId, const QString &ticket)
{ {
qDebug() << fileUrl.fileName(); qDebug() << fileUrl.fileName();
QFile file = fileUrl.toLocalFile();
if (!file.open(QIODevice::ReadOnly))
return;
auto fileSize = file.size();
CURL *curl;
CURLcode res; CURLcode res;
curl = curl_easy_init(); CURL *curl = curl_easy_init();
if (curl) { if (!curl)
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); return;
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:4523/m1/2914957-0-default/");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_URL, (baseUrl + "file/" + fileId.toStdString()).c_str());
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
struct curl_slist *headers = NULL; curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
headers = curl_slist_append(headers, "User-Agent: Apifox/1.0.0 (https://apifox.com)"); curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
curl_mime *mime; curl_httppost *formpost = nullptr;
curl_mimepart *part; curl_httppost *lastptr = nullptr;
mime = curl_mime_init(curl); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "ticket",
part = curl_mime_addpart(mime); CURLFORM_COPYCONTENTS, ticket.toStdString().c_str(),
curl_mime_name(part, "ticket"); CURLFORM_END);
curl_mime_data(part, "<ticket>", CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "rangeStart",
curl_mime_name(part, "rangeStart"); CURLFORM_COPYCONTENTS, std::to_string(0).c_str(),
curl_mime_data(part, "<rangeStart>", CURL_ZERO_TERMINATED); CURLFORM_END);
part = curl_mime_addpart(mime); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "rangeEnd",
curl_mime_name(part, "rangeEnd"); CURLFORM_COPYCONTENTS, std::to_string(fileSize).c_str(),
curl_mime_data(part, "<rangeEnd>", CURL_ZERO_TERMINATED); CURLFORM_END);
part = curl_mime_addpart(mime);
curl_mime_name(part, "data"); curl_formadd(&formpost, &lastptr,
curl_mime_filedata(part, "<data>"); CURLFORM_COPYNAME, "data",
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); CURLFORM_STREAM, &file,
res = curl_easy_perform(curl); CURLFORM_CONTENTLEN, fileSize,
curl_mime_free(mime); CURLFORM_END);
} curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, readDataFunc);
qDebug() << "Begin Upload";
completedSize = 0;
res = curl_easy_perform(curl);
if (res == CURLE_OK)
qDebug() << "Upload Success";
else
qDebug() << "Upload Failed";
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_formfree(formpost);
file.close();
} }
struct FileTransfering : FileItem
{
CURL *curlHandle = nullptr;
std::ofstream file;
};
curl_off_t getHttpFileSize(const std::string &url) curl_off_t getHttpFileSize(const std::string &url)
{ {
curl_off_t fileLength = 0; curl_off_t fileLength = 0;
@ -112,6 +139,7 @@ curl_off_t getHttpFileSize(const std::string &url)
curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(handle, CURLOPT_HEADER, 0); //只需要header头 curl_easy_setopt(handle, CURLOPT_HEADER, 0); //只需要header头
curl_easy_setopt(handle, CURLOPT_NOBODY, 1); //不需要body curl_easy_setopt(handle, CURLOPT_NOBODY, 1); //不需要body
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
if (curl_easy_perform(handle) == CURLE_OK) if (curl_easy_perform(handle) == CURLE_OK)
curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &fileLength); curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &fileLength);
@ -123,28 +151,66 @@ curl_off_t getHttpFileSize(const std::string &url)
return fileLength; return fileLength;
} }
struct FileTransfering : FileItem
{
CURL *curlHandle = nullptr;
std::ofstream file;
};
size_t receiveDataFunc(const void *ptr, size_t size, size_t nmemb, void *obj) size_t receiveDataFunc(const void *ptr, size_t size, size_t nmemb, void *obj)
{ {
auto buf = (FileTransfering *) obj; auto buf = (FileTransfering *) obj;
auto writeSize = size * nmemb; auto writeSize = size * nmemb;
buf->file.write(reinterpret_cast<const char *>(ptr), writeSize); buf->file.write(reinterpret_cast<const char *>(ptr), writeSize);
buf->completedSize += writeSize;
qDebug() << std::format("Downloading: {} / {}", buf->completedSize, buf->totalSize).c_str();
QTimer::singleShot(0, qApp, [buf]() {
FileTransferListModel::instance().setItem(static_cast<FileItem>(*buf));
});
return writeSize; return writeSize;
} }
static int xferinfo(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
auto fileTrans = (FileTransfering *) p;
curl_off_t speed = 0;
curl_easy_getinfo(fileTrans->curlHandle, CURLINFO_SPEED_DOWNLOAD_T, &speed);
fileTrans->completedSize += dlnow;
fileTrans->speed = speed;
qDebug() << std::format("Downloading: {} / {}, Speed: {}", fileTrans->completedSize, fileTrans->totalSize,
speed).c_str();
auto item = static_cast<FileItem>(*fileTrans);
QTimer::singleShot(0, qApp, [item]() {
FileTransferListModel::instance().setItem(item);
});
if (fileTrans->paused) {
qDebug() << "Pause";
return 1;
}
fileTrans->completedSize -= dlnow;
return 0;
}
std::unordered_map<QString, FileTransfering> transferMap;
std::mutex transferMapMutex;
bool httpDownload(const std::string &url, const FileItem &item) bool httpDownload(const std::string &url, const FileItem &item)
{ {
auto fileSize = getHttpFileSize(url);//获得文件大小。 auto fileSize = getHttpFileSize(url);//获得文件大小。
if (fileSize != -1) { if (fileSize != -1) {
qDebug() << "FileSize: " << fileSize; qDebug() << "FileSize: " << fileSize;
FileTransfering obj = (FileTransfering) item; transferMapMutex.lock();
auto [iter, _] = transferMap.emplace(item.id, (FileTransfering) item);
transferMapMutex.unlock();
FileTransfering &obj = iter->second;
obj.curlHandle = curl_easy_init(); obj.curlHandle = curl_easy_init();
if (!obj.curlHandle) if (!obj.curlHandle)
@ -157,19 +223,29 @@ bool httpDownload(const std::string &url, const FileItem &item)
curl_easy_cleanup(obj.curlHandle); curl_easy_cleanup(obj.curlHandle);
return false; return false;
} else { } else {
curl_easy_setopt(obj.curlHandle, CURLOPT_VERBOSE, 0L); curl_easy_setopt(obj.curlHandle, CURLOPT_VERBOSE, 0L);
curl_easy_setopt(obj.curlHandle, CURLOPT_HEADER, 0); curl_easy_setopt(obj.curlHandle, CURLOPT_HEADER, 0);
curl_easy_setopt(obj.curlHandle, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(obj.curlHandle, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(obj.curlHandle, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(obj.curlHandle, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(obj.curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(obj.curlHandle, CURLOPT_URL, url.c_str()); curl_easy_setopt(obj.curlHandle, CURLOPT_URL, url.c_str());
curl_easy_setopt(obj.curlHandle, CURLOPT_WRITEDATA, (void *) &obj); curl_easy_setopt(obj.curlHandle, CURLOPT_WRITEDATA, (void *) &obj);
curl_easy_setopt(obj.curlHandle, CURLOPT_WRITEFUNCTION, receiveDataFunc); curl_easy_setopt(obj.curlHandle, CURLOPT_WRITEFUNCTION, receiveDataFunc);
curl_easy_setopt(obj.curlHandle, CURLOPT_NOPROGRESS, false);//设为false 下面才能设置进度响应函数
curl_easy_setopt(obj.curlHandle, CURLOPT_XFERINFOFUNCTION, xferinfo);
curl_easy_setopt(obj.curlHandle, CURLOPT_XFERINFODATA, &obj);
//下载 //下载
auto res = curl_easy_perform(obj.curlHandle); auto res = curl_easy_perform(obj.curlHandle);
curl_easy_cleanup(obj.curlHandle); curl_easy_cleanup(obj.curlHandle);
obj.file.close(); obj.file.close();
return res == CURLE_OK && obj.completedSize == obj.totalSize;
transferMapMutex.lock();
transferMap.erase(obj.id);
transferMapMutex.unlock();
return res == CURLE_OK;
} }
} else } else
return false; return false;
@ -178,18 +254,91 @@ bool httpDownload(const std::string &url, const FileItem &item)
void FileTransferManager::download(QString fileId) void FileTransferManager::download(QString fileId)
{ {
static int _fileId = 0; static int _fileId = 0;
fileId = QString::number(_fileId++); //fileId = QString::number(_fileId++);
QtConcurrent::run([fileId, this] { QtConcurrent::run([fileId, this] {
qDebug() << "Start Get"; qDebug() << "Start Get";
std::string resData; std::string resData;
if (CURLE_OK != httpGet(fileId.toStdString() + "/status", resData)) if (CURLE_OK != httpGet("File/" + fileId.toStdString() + "/status", resData))
return; return;
qDebug() << resData.c_str(); qDebug() << resData.c_str();
auto resJson = QJsonDocument::fromJson(resData.c_str()); auto resJson = QJsonDocument::fromJson(resData.c_str());
//if(!resJson["isCompleted"].toBool()) //if(!resJson["isCompleted"].toBool())
return;
/* int size = resJson["size"].toInt();
FileItem item{fileId, resJson["md5"].toString(), 0, size};
QTimer::singleShot(0, qApp, [this, item]() {
FileTransferListModel::instance().insertItem(item);
});*/
auto fileUrl = "https://curl.se/download/curl-8.1.2.zip";
/* curl_slist *headers = nullptr;
headers = curl_slist_append(headers, "Range: bytes=0-");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);*/
int64_t size = getHttpFileSize(fileUrl);
FileItem item{fileId, fileId, 0, size};
auto res = httpDownload(fileUrl, item);
qDebug() << "End Get" << res;
});
}
void FileTransferManager::pause(const QString &fileId)
{
transferMapMutex.lock();
transferMap[fileId].paused = true;
transferMapMutex.unlock();
}
QString FileTransferManager::getFileMd5(const QUrl &fileUrl)
{
QFile sourceFile = fileUrl.toLocalFile();
if (sourceFile.open(QIODevice::ReadOnly)) {
QCryptographicHash hash(QCryptographicHash::Md5);
QByteArray buffer;
while ((buffer = sourceFile.read(10240)).size() > 0) {
hash.addData(buffer);
}
sourceFile.close();
return {hash.result().toHex()};
}
return {};
}
int64_t FileTransferManager::getFileSize(const QUrl &fileUrl)
{
return QFile(fileUrl.toLocalFile()).size();
}
void FileTransferManager::resume(const QString &fileId)
{
QtConcurrent::run([fileId, this] {
qDebug() << "Start Get";
std::string resData;
if (CURLE_OK != httpGet("File/" + fileId.toStdString() + "/status", resData, CURL_HTTP_VERSION_1_1))
return;
qDebug() << resData.c_str();
return;
auto resJson = QJsonDocument::fromJson(resData.c_str());
//if(!resJson["isCompleted"].toBool())
// return; // return;
@ -202,8 +351,14 @@ void FileTransferManager::download(QString fileId)
auto fileUrl = "https://curl.se/download/curl-8.1.2.zip"; auto fileUrl = "https://curl.se/download/curl-8.1.2.zip";
/* curl_slist *headers = nullptr;
headers = curl_slist_append(headers, "Range: bytes=0-");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);*/
int64_t size = getHttpFileSize(fileUrl); int64_t size = getHttpFileSize(fileUrl);
FileItem item{fileId, resJson["md5"].toString(), 0, size}; FileItem item{fileId, fileId, 0, size};
QTimer::singleShot(0, qApp, [this, item]() { QTimer::singleShot(0, qApp, [this, item]() {
FileTransferListModel::instance().insertItem(item); FileTransferListModel::instance().insertItem(item);
}); });
@ -211,20 +366,8 @@ void FileTransferManager::download(QString fileId)
auto res = httpDownload(fileUrl, item); auto res = httpDownload(fileUrl, item);
while (item.completedSize < item.totalSize && false) {
item.completedSize += 1;
QTimer::singleShot(0, qApp, [this, item]() {
FileTransferListModel::instance().setItem(item);
});
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);
}
qDebug() << "End Get" << res; qDebug() << "End Get" << res;
}); });
} }

View File

@ -8,17 +8,26 @@
#include <QObject> #include <QObject>
#include <QUrl> #include <QUrl>
#include <unordered_map>
#include "FileTransferListModel.h" #include "FileTransferListModel.h"
class FileTransferManager : public QObject class FileTransferManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit FileTransferManager(QObject *parent = nullptr); explicit FileTransferManager(QObject *parent = nullptr);
Q_INVOKABLE void upload(QUrl fileUrl); Q_INVOKABLE int64_t getFileSize(const QUrl& fileUrl);
Q_INVOKABLE QString getFileMd5(const QUrl& fileUrl);
Q_INVOKABLE void upload(const QUrl& fileUrl, const QString& fileId, const QString& ticket);
Q_INVOKABLE void download(QString fileId); Q_INVOKABLE void download(QString fileId);
Q_INVOKABLE void pause(const QString& fileId);
Q_INVOKABLE void resume(const QString& fileId);
private:
}; };