From 7232238d7b18cf76acb662b67d5efe69865eda1f Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Fri, 9 Apr 2021 22:32:13 +0200 Subject: [PATCH] Add additional checks after enclosure has been downloaded The file size in the database will be corrected if it doesn't match the real file size. The "new" status will be unset once the file has been downloaded. --- src/enclosure.cpp | 25 ++++++++++++++++++++++++- src/enclosure.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/enclosure.cpp b/src/enclosure.cpp index a9b8d0b2..f84a3713 100644 --- a/src/enclosure.cpp +++ b/src/enclosure.cpp @@ -35,7 +35,6 @@ Enclosure::Enclosure(Entry *entry) m_url = query.value(QStringLiteral("url")).toString(); m_playposition = query.value(QStringLiteral("playposition")).toInt(); - // TODO: what to do if the rss-reported filesize doesn't match the real file size??? QFile file(path()); if(file.size() == m_size) { m_status = Downloaded; @@ -61,6 +60,8 @@ void Enclosure::download() connect(downloadJob, &KJob::result, this, [this, downloadJob]() { if(downloadJob->error() == 0) { m_status = Downloaded; + processDownloadedFile(); + } else { m_status = Downloadable; if(downloadJob->error() != QNetworkReply::OperationCanceledError) { @@ -88,6 +89,28 @@ void Enclosure::download() Q_EMIT statusChanged(); } +void Enclosure::processDownloadedFile() { + // This will be run if the enclosure has been downloaded successfully + + // Unset "new" status of item + if (m_entry->getNew()) m_entry->setNew(false); + + // Check if reported filesize in rss feed corresponds to real file size + // if not, correct the filesize in the database + // otherwise the file will get deleted because of mismatch in signature + QFile file(path()); + if(file.size() != m_size) { + qDebug() << "enclosure file size mismatch" << m_entry->title(); + m_size = file.size(); + QSqlQuery query; + query.prepare(QStringLiteral("UPDATE Enclosures SET size=:size WHERE id=:id AND feed=:feed")); + query.bindValue(QStringLiteral(":id"), m_entry->id()); + query.bindValue(QStringLiteral(":feed"), m_entry->feed()->url()); + query.bindValue(QStringLiteral(":size"), m_size); + Database::instance().execute(query); + } +} + void Enclosure::deleteFile() { qDebug() << "Trying to delete enclosure file" << path(); diff --git a/src/enclosure.h b/src/enclosure.h index 2f7f7295..db7b0969 100644 --- a/src/enclosure.h +++ b/src/enclosure.h @@ -50,6 +50,8 @@ Q_SIGNALS: private: + void processDownloadedFile(); + Entry *m_entry; int m_duration; int m_size;