李太白3028
2025-07-30 ba557576d960281d774ac1e3c83f2e29786ed682
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#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;
}