| 464 | } |
| 465 | |
| 466 | void updateHandler::downloadFinished(QNetworkReply *reply) |
| 467 | { |
| 468 | if (!UPDATE_FEATURE_ENABLE) |
| 469 | return; |
| 470 | |
| 471 | bool error = (reply->error() != QNetworkReply::NoError); |
| 472 | auto err = reply->error(); |
| 473 | bool downloadEncrypted = reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute).toBool(); |
| 474 | DEBUG_UPDATE("updateHandler::downloadFinished " << (error ? "error " : "") << (downloadEncrypted ? "encrypted " : "not encrypted ") << reply->error()); |
| 475 | if (error) |
| 476 | return abortUpdate(QString("An error occurred while downloading file %1. Error code %2 (%3).").arg(currentDownloadFile.first).arg(err).arg(reply->errorString())); |
| 477 | else if (!downloadEncrypted) |
| 478 | return abortUpdate(QString("File %1 could not be downloaded through a secure connection.").arg(currentDownloadFile.first)); |
| 479 | else |
| 480 | { |
| 481 | // A file was downloaded successfully. Get the data. |
| 482 | QByteArray data = reply->readAll(); |
| 483 | |
| 484 | // Here, some check could go that checks the MD5 sum. |
| 485 | // However, we got the file from a secure connection from our github server. So I don't know if |
| 486 | // this check would really add any more security. |
| 487 | |
| 488 | // Save the file locally |
| 489 | QString fullPath = updatePath + currentDownloadFile.first; |
| 490 | |
| 491 | // First, check if the file exists. If it does, remove the local file first. |
| 492 | // This should work, even for existing files because we have elevated rights. |
| 493 | QFileInfo fileInfo(fullPath); |
| 494 | if (fileInfo.exists()) |
| 495 | { |
| 496 | QFile oldFile(fullPath); |
| 497 | if (!oldFile.remove()) |
| 498 | { |
| 499 | // Deleting the file failed. Let's just rename it to "something_old.ext" |
| 500 | QString newName = fileInfo.baseName() + "_old." + fileInfo.completeSuffix(); |
| 501 | QString renamedFilePath = updatePath + newName; |
| 502 | // First, check if this _old file already exists. If yes, delete it first. |
| 503 | QFileInfo newFileInfo(renamedFilePath); |
| 504 | if (newFileInfo.isFile() && newFileInfo.exists()) |
| 505 | if (!QFile(renamedFilePath).remove()) |
| 506 | return abortUpdate(QString("YUView was unable to remove the file %1.").arg(renamedFilePath)); |
| 507 | if (!oldFile.rename(newName)) |
| 508 | return abortUpdate(QString("YUView was unable to remove or rename the file %1.").arg(fileInfo.fileName())); |
| 509 | DEBUG_UPDATE("updateHandler::downloadFinished The old file could not be deleted but was renamed to " << newName); |
| 510 | } |
| 511 | else |
| 512 | DEBUG_UPDATE("updateHandler::downloadFinished Successfully deleted old file " << fileInfo.fileName()); |
| 513 | } |
| 514 | |
| 515 | // Second check: Is the file located in a subirectory that does not exist? |
| 516 | // If so, create that subdirectory. |
| 517 | if (currentDownloadFile.first.contains("/")) |
| 518 | { |
| 519 | int lastIdx = currentDownloadFile.first.lastIndexOf("/"); |
| 520 | QString fullDir = updatePath + currentDownloadFile.first.left(lastIdx); |
| 521 | if (!QDir().mkpath(fullDir)) |
| 522 | return abortUpdate(QString("Could not create the subdirectory %1").arg(fullDir)); |
| 523 | } |