#include "addfile.h"
|
#include "ui_addfile.h"
|
#include <QFileDialog>
|
#include <QDebug>
|
#include <QDateTime>
|
#include <QString>
|
#include <memory>
|
#define DEFAULT_PATH ("E:\soft")
|
#define DEFAULT_TYPE (tr("Pkg Files (*.zip *.tar.gz *.rar *.7z *.bz2)"))
|
#define UPLOAD 10
|
|
AddFile::AddFile(QWidget *parent) :
|
QMainWindow(parent),
|
ui(new Ui::AddFile)
|
{
|
ui->setupUi(this);
|
}
|
|
AddFile::~AddFile()
|
{
|
delete ui;
|
}
|
void AddFile::on_pushButton_3_clicked()
|
{
|
QString filePath = QFileDialog::getOpenFileName(this,
|
tr("选择文件"),
|
DEFAULT_PATH,
|
DEFAULT_TYPE);
|
if(filePath.isEmpty()){
|
return;
|
}
|
ui->lineEdit->setText(filePath);
|
QFileInfo fileInfo(filePath);
|
ui->lineEdit_2->setText(fileInfo.path());
|
}
|
void AddFile::on_pushButton_clicked()
|
{
|
|
QString file = ui->lineEdit->text();
|
QFileInfo fileInfo(file);
|
QString sourcePath = fileInfo.path();
|
QString targetPath = ui->lineEdit_3->text();
|
QString version = ui->lineEdit_4->text();
|
QString parentVersion = ui->comboBox->currentText();
|
QDateTime createDate = fileInfo.lastModified();
|
QString description = ui->textEdit->document()->toPlainText();
|
|
QString versionMessage;
|
versionMessage = targetPath + "#"
|
+ version + "#"
|
+ parentVersion + "#"
|
+ createDate.toString() + "#"
|
+ description;
|
qDebug()<<versionMessage;
|
// 获取文件基本信息
|
QString fileName = fileInfo.fileName();
|
qint64 size = fileInfo.size();
|
|
|
|
// 计算头消息长度,并分配空间
|
// 头消息(示例)
|
int len = sizeof(UpLoadPkgHead) + fileName.size() + 1;
|
|
std::shared_ptr<char> head(new char[len],[](char *p){delete[] p;});
|
UpLoadPkgHead* pHead = reinterpret_cast<UpLoadPkgHead*>(head.get());
|
|
// 直接操作智能指针管理的内存
|
pHead->head.type = UPLOAD_REQ;
|
pHead->head.len = len;
|
memcpy(pHead->fileName, fileName.toUtf8().constData(), fileName.size());
|
pHead->fileName[fileName.size()] = '\0';
|
|
emit addFileSignal(head, len); // 直接传递智能指针
|
|
|
QFile *pFile = new QFile(file);
|
if(!pFile){
|
return;
|
}
|
|
// 读文件内容
|
|
if (!pFile->open(QIODevice::ReadOnly)) {
|
qDebug() << "Failed to open file for reading:" << pFile->errorString();
|
return;
|
}
|
// const int bufferSize = 4 * 1024;
|
const int bufferSize = 8 * 1024;
|
auto buffer = std::unique_ptr<char>(new char[bufferSize]);
|
qint64 totalBytesRead = 0;
|
int flag = 0;
|
while (!pFile->atEnd()) {
|
qint64 bytesRead = pFile->read(buffer.get(), bufferSize);
|
if (bytesRead == -1) {
|
qDebug() << "Error during read operation:" << pFile->errorString();
|
break;
|
}
|
totalBytesRead += bytesRead;
|
|
// 数据包构造(示例)
|
int dtlen = sizeof(UpLoadPkgData) + fileName.size() + bytesRead + 2;
|
std::shared_ptr<char> dataPkg(new char[dtlen],[](char *p){delete[] p;});
|
UpLoadPkgData* pData = reinterpret_cast<UpLoadPkgData*>(dataPkg.get());
|
|
// 填充数据
|
pData->head.len = len;
|
pData->head.type = UPLOAD_REQ;
|
pData->upLoadType = DATA_UPLOAD_REQ;
|
pData->fileNameLen = fileName.size();
|
pData->dataLen = bytesRead;
|
pData->flag = ++flag;
|
pData->dataLen = bytesRead;
|
memcpy(pData->data, fileName.toUtf8().constData(), fileName.size());
|
memcpy(pData->data + fileName.size() + 1, buffer.get(), bytesRead);
|
|
emit addFileSignal(dataPkg, dtlen);
|
// 无需手动 free,shared_ptr 自动释放
|
|
}
|
if(totalBytesRead != size){
|
qDebug()<<"数据发送不完整";
|
return;
|
}
|
qDebug()<<"数据发送完成,发送数据库包";
|
|
int finLen = sizeof(UpLoadPkgFin)+fileName.size()+versionMessage.size()+2;
|
std::shared_ptr<char> finPkg(new char[finLen],[](char *p){delete[] p;});
|
UpLoadPkgFin* pData = reinterpret_cast<UpLoadPkgFin*>(finPkg.get());
|
|
// 填充数据
|
pData->head.len = finLen;
|
pData->head.type = UPLOAD_REQ;
|
pData->fileNameLen = fileName.size();
|
pData->cnt = flag;
|
memcpy(pData->fileName, fileName.toUtf8().constData(), fileName.size());
|
memcpy(pData->fileName + fileName.size() + 1,versionMessage.data(),versionMessage.size());
|
|
emit addFileSignal(finPkg ,finLen);
|
emit addFileMsg(QString("上传成功!"));
|
|
delete pFile;
|
}
|