🐞 fix(文件传输): 优化文件上传逻辑

This commit is contained in:
huheng@kylinos.cn 2023-04-04 10:26:23 +08:00
parent 514350ae81
commit 463f04feda
2 changed files with 17 additions and 13 deletions

View File

@ -274,18 +274,23 @@ void FtpManager::ftpCommandFinished(int commandId, bool error)
}
if (m_ftp->currentCommand() == QFtp::Put) {
QString fileName = m_uploadFiles.take(commandId);
if (fileName.isEmpty()) {
QFile *file = m_uploadFiles.take(commandId);
if (file == nullptr) {
return;
}
if (error) {
QString fileName = file->fileName();
qWarning() << "CommandFinished: Cancle the file success: " << fileName;
Q_EMIT sigDownFileFail(fileName.mid(fileName.lastIndexOf("/") + 1, fileName.size()));
abort();
} else {
QString fileName = file->fileName();
qInfo() << "CommandFinished: Upload the file success: " << fileName;
}
// 将该文件从上传文件中删除
file->close();
delete file;
file = nullptr;
m_uploadFiles.remove(commandId);
return;
}
@ -528,6 +533,7 @@ void FtpManager::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes)
m_isFree = true;
// this->cdToDir(m_downBeforePath);
}
Q_EMIT sigUpdateTransferProgress(m_readProgress, m_updateAllFileSize);
}
}
@ -674,13 +680,9 @@ void FtpManager::upload(const QStringList &fileList, QString uploadPath)
file = nullptr;
continue;
}
QByteArray data = file->readAll();
int id = 0;
id = m_ftp->put(data, uploadPath + "/" + fileName);
m_uploadFiles.insert(id, uploadPath + "/" + fileName);
file->close();
delete file;
file = nullptr;
id = m_ftp->put(file, uploadPath + "/" + fileName);
m_uploadFiles.insert(id, file);
}
}
@ -766,13 +768,13 @@ void FtpManager::clearErrorFiles()
}
}
// 清理未完成上传的文件
QMap<int, QString>::iterator uploadIt = m_uploadFiles.begin();
QMap<int, QFile *>::iterator uploadIt = m_uploadFiles.begin();
while (uploadIt != m_uploadFiles.end()) {
if (uploadIt.value().isEmpty()) {
if (uploadIt.value() == nullptr) {
uploadIt++;
continue;
} else {
QString fileName = uploadIt.value();
QString fileName = uploadIt.value()->fileName();
QString filePath = fileName.mid(0, fileName.lastIndexOf('/') + 1);
fileName = fileName.mid(fileName.lastIndexOf('/') + 1, fileName.size());
if (filePath != m_currentPath) {
@ -782,7 +784,9 @@ void FtpManager::clearErrorFiles()
} else {
m_ftp->remove(fileName);
}
uploadIt.value().clear();
uploadIt.value()->close();
uploadIt.value()->deleteLater();
uploadIt.value() = nullptr;
}
}
}

View File

@ -93,7 +93,7 @@ private:
QString m_downBeforePath = ""; // 进行下载之前的路径
QMap<QString, FileInfo> m_currentAllFiles; // 当前路径下文件列表
QMap<int, QFile *> m_downFiles; // 所有下载文件
QMap<int, QString> m_uploadFiles; // 所有上传文件
QMap<int, QFile *> m_uploadFiles; // 所有上传文件
QMap<int, QStringList> m_taskLocalMap; // 用于记录上传文件夹中文件的本地路径
QMap<int, QString> m_taskPathMap; // 用于记录每一次任务的上传|下载路径
QMap<QString, DownloadTask> m_taskDownloadMap; // 记录当前下载信息