diff --git a/src/steamgriddb.cpp b/src/steamgriddb.cpp index 4281401..c93e0d6 100644 --- a/src/steamgriddb.cpp +++ b/src/steamgriddb.cpp @@ -8,11 +8,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -297,16 +297,28 @@ void SteamGridDB::onImageDownloaded() QString coversPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/covers"); QDir dir(coversPath); if (!dir.exists()) { - dir.mkpath(coversPath); + if (!dir.mkpath(coversPath)) { + Q_EMIT fetchError(game, QStringLiteral("Failed to create covers directory")); + m_processedGames++; + Q_EMIT fetchProgress(m_processedGames, m_totalGames); + processNextGame(); + return; + } } QString fileName = game->id() + QStringLiteral(".jpg"); QString filePath = coversPath + QStringLiteral("/") + fileName; - QFile file(filePath); - if (file.open(QIODevice::WriteOnly)) { - file.write(reply->readAll()); - file.close(); + const QByteArray payload = reply->readAll(); + QSaveFile file(filePath); + if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { + if (file.write(payload) != payload.size() || !file.commit()) { + Q_EMIT fetchError(game, QStringLiteral("Failed to save cover image")); + m_processedGames++; + Q_EMIT fetchProgress(m_processedGames, m_totalGames); + processNextGame(); + return; + } QUrl localUrl = QUrl::fromLocalFile(filePath); game->setCoverUrl(localUrl);