Files
RRJClient/Core/UpdateController.cpp
semenov 077208afdd feat: change buttonsView
* add buttonsView common(complete)
* add buttonsView instructor(notWorking)
* BUGFIX sendSystem meth update files
* BUGFIX updateController meth checkNeedUpdate
2025-01-20 10:59:03 +03:00

137 lines
3.5 KiB
C++

#include "UpdateController.h"
#include <QDialogButtonBox>
UpdateController::UpdateController(DataParser *parser,SendSystem *sendSystem, QObject *parent) :
QObject(parent)
{
this->dataParser = parser;
this->sendSystem = sendSystem;
applicationFolderPath = QDir::currentPath() + applicationFolderName;
//calculateStreamingHash();
}
void UpdateController::calculateCommonHash()
{
fileDataList.clear();
calculateHash(applicationFolderPath);
dataParser->createFileDataList(fileDataList,hashFilename);
qDebug() << "UpdateController threadID " << QThread::currentThreadId();
}
void UpdateController::calculateStreamingHash()
{
fileDataList.clear();
calculateHash(QDir::currentPath() + streamingAssetsPath);
dataParser->createFileDataList(fileDataList,streamingHashFilename);
}
void UpdateController::calculateHash(QString path)
{
qDebug() << "Try calculate";
QDir dir(path);
QDirIterator iterator(dir,QDirIterator::Subdirectories);
fileDataList.clear();
QList<FileData> *files = new QList<FileData>;
QList<FileData> *folders = new QList<FileData>;
if(!QDir(path).exists()){ //проверка на наличие папки
QDir().mkdir(path);
}
QString hashString;
while (iterator.hasNext())
{
iterator.next();
QFileInfo fileInfo = iterator.fileInfo();
FileData currentFile;
QFile file(fileInfo.absoluteFilePath());
quint64 fileSize = file.size(); //буффер для хэширования крупных файлов
const quint64 bufferSize = 10240;
if(fileInfo.isHidden()) continue;
if(fileInfo.isFile() && file.open(QIODevice::ReadOnly))
{
char buffer[bufferSize];
int bytesRead;
int readSize = qMin(fileSize,bufferSize);
QCryptographicHash hash(QCryptographicHash::Md5);
while(readSize > 0 && (bytesRead = file.read(buffer,readSize)) > 0){
fileSize -= bytesRead;
hash.addData(buffer,bytesRead);
readSize = qMin(fileSize,bufferSize);
}
hashString = QString(hash.result().toHex());
currentFile.path = Tools::createLocalPath(fileInfo.absoluteFilePath());
currentFile.hash = hashString;
files->push_back(currentFile);
file.close();
}
else if (fileInfo.isDir() && !fileInfo.isRoot() && fileInfo.fileName() != "..")
{
currentFile.path = Tools::createLocalPath(fileInfo.path());
currentFile.hash = "FOLDER";
if(!folders->contains(currentFile)){
folders->push_back(currentFile);
}
}
}
fileDataList.append(*folders);
fileDataList.append(*files);
delete folders;
delete files;
}
void UpdateController::updateFilesOnServer(QList<FileData> *fileSendList){
QListIterator<FileData> serverIterator(*fileSendList);
try {
while(serverIterator.hasNext())
{
FileData data = serverIterator.next();
if (data.hash == "FOLDER")
{
sendSystem->sendFolderBlock(data.path);
}
else
{
sendSystem->sendFileBlock(data.path);
}
}
calculateCommonHash();
sendSystem->sendFinish();
emit sigUpdateComplete(true);
}
catch (...)
{
emit sigUpdateComplete(false);
}
}
UpdateController::~UpdateController()
{
}