steamgriddb: save covers atomically

This commit is contained in:
Marco Allegretti 2026-02-13 13:20:45 +01:00
parent a032bec8c4
commit 144076675c

View file

@ -8,11 +8,11 @@
#include <KConfigGroup>
#include <KSharedConfig>
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QSaveFile>
#include <QStandardPaths>
#include <QUrlQuery>
@ -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);