mirror of
https://invent.kde.org/marcoa/a-la-karte.git
synced 2026-03-26 17:03:08 +00:00
steamgriddb: save covers atomically
This commit is contained in:
parent
a032bec8c4
commit
144076675c
1 changed files with 18 additions and 6 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue