Compare commits
2 Commits
d7f5670700
...
f65f9f3dbb
Author | SHA1 | Date |
---|---|---|
wuyize | f65f9f3dbb | |
wuyize | d5d707f891 |
|
@ -108,8 +108,8 @@ AppFluWindow {
|
|||
}, function (p1, p2) {
|
||||
console.log(p1)
|
||||
console.log(p2)
|
||||
FluApp.navigate("/")
|
||||
window.close()
|
||||
//FluApp.navigate("/")
|
||||
//window.close()
|
||||
btn_login.disabled = false
|
||||
btn_login.text = "登录"
|
||||
})
|
||||
|
|
|
@ -89,20 +89,6 @@ FluWindow {
|
|||
title_bar.maximizeButton.visible = true
|
||||
// window.backgroundVisible = false
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@ import QtQuick
|
|||
import QtQuick.Controls
|
||||
import QtQuick.Window
|
||||
import QtQuick.Layouts
|
||||
import Qt5Compat.GraphicalEffects
|
||||
import FluentUI
|
||||
import AicsKB.FileTransferManager
|
||||
|
||||
Popup {
|
||||
id: transfer_popup
|
||||
|
@ -21,21 +23,99 @@ Popup {
|
|||
anchors.topMargin: 10
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.bottomMargin: -10
|
||||
spacing: 0
|
||||
Repeater {
|
||||
model: FileTransferListModel
|
||||
delegate: ColumnLayout {
|
||||
Layout.bottomMargin: 10
|
||||
spacing: 10
|
||||
width: parent.width
|
||||
Text {
|
||||
text: name
|
||||
delegate: Item {
|
||||
Layout.fillWidth: true
|
||||
height: 50
|
||||
|
||||
ColumnLayout {
|
||||
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 {
|
||||
Layout.fillWidth: true
|
||||
progress: completedSize / totalSize
|
||||
indeterminate: false
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import QtQuick
|
|||
QtObject {
|
||||
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/"
|
||||
|
||||
// GET
|
||||
|
|
|
@ -145,7 +145,9 @@ FluArea {
|
|||
}
|
||||
onClicked: {
|
||||
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
|
||||
implicitHeight: 400
|
||||
}
|
||||
|
||||
}
|
||||
Component {
|
||||
id: other_view
|
||||
|
@ -231,28 +232,28 @@ FluArea {
|
|||
}
|
||||
}
|
||||
|
||||
// Item {
|
||||
// Layout.fillWidth: true
|
||||
// implicitHeight: 50
|
||||
// FluText {
|
||||
// id: text_note
|
||||
// text: "笔记"
|
||||
// padding: 10
|
||||
// font {
|
||||
// pointSize: 15
|
||||
// bold: true
|
||||
// }
|
||||
// }
|
||||
// FluTextButton {
|
||||
// id: button_publish
|
||||
// text: "发布笔记"
|
||||
// hoverColor: "blue"
|
||||
// normalColor: "black"
|
||||
// anchors {
|
||||
// verticalCenter: text_note.verticalCenter
|
||||
// right: parent.right
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Item {
|
||||
// Layout.fillWidth: true
|
||||
// implicitHeight: 50
|
||||
// FluText {
|
||||
// id: text_note
|
||||
// text: "笔记"
|
||||
// padding: 10
|
||||
// font {
|
||||
// pointSize: 15
|
||||
// bold: true
|
||||
// }
|
||||
// }
|
||||
// FluTextButton {
|
||||
// id: button_publish
|
||||
// text: "发布笔记"
|
||||
// hoverColor: "blue"
|
||||
// normalColor: "black"
|
||||
// anchors {
|
||||
// verticalCenter: text_note.verticalCenter
|
||||
// right: parent.right
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import QtQuick.Dialogs
|
|||
import FluentUI
|
||||
import AicsKB.FileTransferManager
|
||||
import "qrc:///AicsKnowledgeBase/qml/component"
|
||||
import "qrc:///AicsKnowledgeBase/qml/global"
|
||||
|
||||
FluArea {
|
||||
property string url: ''
|
||||
|
@ -32,7 +33,33 @@ FluArea {
|
|||
FileDialog {
|
||||
id: fileDialog
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ FileTransferListModel::FileTransferListModel(QObject *parent)
|
|||
m_roleName.insert(kNameRole, "name");
|
||||
m_roleName.insert(kCompletedSizeRole, "completedSize");
|
||||
m_roleName.insert(kTotalSizeRole, "totalSize");
|
||||
m_roleName.insert(kSpeedRole, "speed");
|
||||
m_roleName.insert(kPausedRole, "paused");
|
||||
|
||||
m_data = {
|
||||
{"v", "vivo", 1200, 2000},
|
||||
|
@ -42,6 +44,10 @@ QVariant FileTransferListModel::data(const QModelIndex &index, int role) const
|
|||
return m_data.value(index.row()).completedSize;
|
||||
case kTotalSizeRole:
|
||||
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:
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ struct FileItem
|
|||
QString name;
|
||||
int64_t completedSize;
|
||||
int64_t totalSize;
|
||||
int64_t speed;
|
||||
bool paused = false;
|
||||
};
|
||||
|
||||
|
||||
|
@ -39,7 +41,9 @@ public:
|
|||
kIdRole = Qt::UserRole + 1,
|
||||
kNameRole,
|
||||
kCompletedSizeRole,
|
||||
kTotalSizeRole
|
||||
kTotalSizeRole,
|
||||
kSpeedRole,
|
||||
kPausedRole
|
||||
};
|
||||
private:
|
||||
explicit FileTransferListModel(QObject *parent = nullptr);
|
||||
|
|
|
@ -9,7 +9,9 @@
|
|||
#include <QtConcurrent/QtConcurrent>
|
||||
#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)
|
||||
{
|
||||
|
@ -28,24 +30,26 @@ static size_t writeDataCallback(void *contents, size_t size, size_t nmemb, void
|
|||
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;
|
||||
|
||||
// 获取easy handle
|
||||
CURL *easy_handle = curl_easy_init();
|
||||
if (nullptr == easy_handle) {
|
||||
curl_global_cleanup();
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
if (!easy_handle) return CURLE_FAILED_INIT;
|
||||
|
||||
// 设置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_WRITEFUNCTION, writeDataCallback);
|
||||
curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, timeout);
|
||||
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, (void *) &response);
|
||||
curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
//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);
|
||||
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)
|
||||
: 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();
|
||||
|
||||
QFile file = fileUrl.toLocalFile();
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return;
|
||||
auto fileSize = file.size();
|
||||
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
curl = curl_easy_init();
|
||||
if (curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
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_DEFAULT_PROTOCOL, "https");
|
||||
struct curl_slist *headers = NULL;
|
||||
headers = curl_slist_append(headers, "User-Agent: Apifox/1.0.0 (https://apifox.com)");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
curl_mime *mime;
|
||||
curl_mimepart *part;
|
||||
mime = curl_mime_init(curl);
|
||||
part = curl_mime_addpart(mime);
|
||||
curl_mime_name(part, "ticket");
|
||||
curl_mime_data(part, "<ticket>", CURL_ZERO_TERMINATED);
|
||||
part = curl_mime_addpart(mime);
|
||||
curl_mime_name(part, "rangeStart");
|
||||
curl_mime_data(part, "<rangeStart>", CURL_ZERO_TERMINATED);
|
||||
part = curl_mime_addpart(mime);
|
||||
curl_mime_name(part, "rangeEnd");
|
||||
curl_mime_data(part, "<rangeEnd>", CURL_ZERO_TERMINATED);
|
||||
part = curl_mime_addpart(mime);
|
||||
curl_mime_name(part, "data");
|
||||
curl_mime_filedata(part, "<data>");
|
||||
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_mime_free(mime);
|
||||
}
|
||||
CURL *curl = curl_easy_init();
|
||||
if (!curl)
|
||||
return;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, (baseUrl + "file/" + fileId.toStdString()).c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
|
||||
curl_httppost *formpost = nullptr;
|
||||
curl_httppost *lastptr = nullptr;
|
||||
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "ticket",
|
||||
CURLFORM_COPYCONTENTS, ticket.toStdString().c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "rangeStart",
|
||||
CURLFORM_COPYCONTENTS, std::to_string(0).c_str(),
|
||||
CURLFORM_END);
|
||||
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "rangeEnd",
|
||||
CURLFORM_COPYCONTENTS, std::to_string(fileSize).c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
curl_formadd(&formpost, &lastptr,
|
||||
CURLFORM_COPYNAME, "data",
|
||||
CURLFORM_STREAM, &file,
|
||||
CURLFORM_CONTENTLEN, fileSize,
|
||||
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_formfree(formpost);
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
struct FileTransfering : FileItem
|
||||
{
|
||||
CURL *curlHandle = nullptr;
|
||||
std::ofstream file;
|
||||
};
|
||||
|
||||
curl_off_t getHttpFileSize(const std::string &url)
|
||||
{
|
||||
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_HEADER, 0); //只需要header头
|
||||
curl_easy_setopt(handle, CURLOPT_NOBODY, 1); //不需要body
|
||||
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
|
||||
if (curl_easy_perform(handle) == CURLE_OK)
|
||||
curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &fileLength);
|
||||
|
@ -123,28 +151,66 @@ curl_off_t getHttpFileSize(const std::string &url)
|
|||
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)
|
||||
{
|
||||
auto buf = (FileTransfering *) obj;
|
||||
auto writeSize = size * nmemb;
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
auto fileSize = getHttpFileSize(url);//获得文件大小。
|
||||
if (fileSize != -1) {
|
||||
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();
|
||||
if (!obj.curlHandle)
|
||||
|
@ -157,19 +223,29 @@ bool httpDownload(const std::string &url, const FileItem &item)
|
|||
curl_easy_cleanup(obj.curlHandle);
|
||||
return false;
|
||||
} else {
|
||||
|
||||
curl_easy_setopt(obj.curlHandle, CURLOPT_VERBOSE, 0L);
|
||||
curl_easy_setopt(obj.curlHandle, CURLOPT_HEADER, 0);
|
||||
curl_easy_setopt(obj.curlHandle, CURLOPT_NOPROGRESS, 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_WRITEDATA, (void *) &obj);
|
||||
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);
|
||||
curl_easy_cleanup(obj.curlHandle);
|
||||
obj.file.close();
|
||||
return res == CURLE_OK && obj.completedSize == obj.totalSize;
|
||||
|
||||
transferMapMutex.lock();
|
||||
transferMap.erase(obj.id);
|
||||
transferMapMutex.unlock();
|
||||
|
||||
return res == CURLE_OK;
|
||||
}
|
||||
} else
|
||||
return false;
|
||||
|
@ -178,18 +254,91 @@ bool httpDownload(const std::string &url, const FileItem &item)
|
|||
void FileTransferManager::download(QString fileId)
|
||||
{
|
||||
static int _fileId = 0;
|
||||
fileId = QString::number(_fileId++);
|
||||
//fileId = QString::number(_fileId++);
|
||||
|
||||
QtConcurrent::run([fileId, this] {
|
||||
qDebug() << "Start Get";
|
||||
|
||||
std::string resData;
|
||||
if (CURLE_OK != httpGet(fileId.toStdString() + "/status", resData))
|
||||
if (CURLE_OK != httpGet("File/" + fileId.toStdString() + "/status", resData))
|
||||
return;
|
||||
|
||||
qDebug() << resData.c_str();
|
||||
auto resJson = QJsonDocument::fromJson(resData.c_str());
|
||||
//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;
|
||||
|
||||
|
||||
|
@ -202,8 +351,14 @@ void FileTransferManager::download(QString fileId)
|
|||
|
||||
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, resJson["md5"].toString(), 0, size};
|
||||
FileItem item{fileId, fileId, 0, size};
|
||||
QTimer::singleShot(0, qApp, [this, item]() {
|
||||
FileTransferListModel::instance().insertItem(item);
|
||||
});
|
||||
|
@ -211,20 +366,8 @@ void FileTransferManager::download(QString fileId)
|
|||
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;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -8,17 +8,26 @@
|
|||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <unordered_map>
|
||||
#include "FileTransferListModel.h"
|
||||
|
||||
|
||||
|
||||
class FileTransferManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
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 pause(const QString& fileId);
|
||||
Q_INVOKABLE void resume(const QString& fileId);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue