Change paths where images and enclosures are saved

Images are now stored in the cache directory in a dedicated subdir
called "images".
Enclosures are stored in the data directory in a dedicated subdir
"enclosures".
This commit is contained in:
Bart De Vries 2021-04-07 12:47:46 +02:00
parent f061b9a00e
commit 75f8f93da7
5 changed files with 36 additions and 20 deletions

View File

@ -48,7 +48,7 @@ Enclosure::Enclosure(Entry *entry)
void Enclosure::download() void Enclosure::download()
{ {
EnclosureDownloadJob *downloadJob = new EnclosureDownloadJob(m_url, m_entry->title()); EnclosureDownloadJob *downloadJob = new EnclosureDownloadJob(m_url, path(), m_entry->title());
downloadJob->start(); downloadJob->start();
m_downloadProgress = 0; m_downloadProgress = 0;
@ -101,5 +101,5 @@ void Enclosure::deleteFile()
QString Enclosure::path() const QString Enclosure::path() const
{ {
return Fetcher::instance().filePath(m_url); return Fetcher::instance().enclosurePath(m_url);
} }

View File

@ -12,9 +12,10 @@
#include "enclosuredownloadjob.h" #include "enclosuredownloadjob.h"
#include "fetcher.h" #include "fetcher.h"
EnclosureDownloadJob::EnclosureDownloadJob(const QString& url, const QString& title, QObject *parent) EnclosureDownloadJob::EnclosureDownloadJob(const QString& url, const QString& filename, const QString& title, QObject *parent)
: KJob(parent) : KJob(parent)
, m_url(url) , m_url(url)
, m_filename(filename)
, m_title(title) , m_title(title)
{ {
setCapabilities(Killable); setCapabilities(Killable);
@ -27,7 +28,7 @@ void EnclosureDownloadJob::start()
void EnclosureDownloadJob::startDownload() void EnclosureDownloadJob::startDownload()
{ {
m_reply = Fetcher::instance().download(m_url); m_reply = Fetcher::instance().download(m_url, m_filename);
Q_EMIT description(this, i18n("Downloading %1", m_title)); Q_EMIT description(this, i18n("Downloading %1", m_title));

View File

@ -13,13 +13,14 @@ class EnclosureDownloadJob : public KJob
{ {
public: public:
explicit EnclosureDownloadJob(const QString &url, const QString &title, QObject *parent = nullptr); explicit EnclosureDownloadJob(const QString &url, const QString &filename, const QString &title, QObject *parent = nullptr);
void start() override; void start() override;
bool doKill() override; bool doKill() override;
private: private:
QString m_url; QString m_url;
QString m_filename;
QString m_title; QString m_title;
QNetworkReply *m_reply = nullptr; QNetworkReply *m_reply = nullptr;

View File

@ -9,6 +9,7 @@
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QDir>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QStandardPaths> #include <QStandardPaths>
@ -213,27 +214,27 @@ void Fetcher::processEnclosure(Syndication::EnclosurePtr enclosure, Syndication:
Database::instance().execute(query); Database::instance().execute(query);
} }
QString Fetcher::image(const QString &url) QString Fetcher::image(const QString& url) const
{ {
QString path = filePath(url); QString path = imagePath(url);
if (QFileInfo::exists(path)) { if (QFileInfo::exists(path)) {
if (QFileInfo(path).size() != 0 ) if (QFileInfo(path).size() != 0 )
return path; return path;
} }
download(url); download(url, path);
return QLatin1String(""); return QLatin1String("");
} }
QNetworkReply *Fetcher::download(QString url) QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) const
{ {
QNetworkRequest request((QUrl(url))); QNetworkRequest request((QUrl(url)));
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::finished, this, [=]() { connect(reply, &QNetworkReply::finished, this, [=]() {
if (reply->isOpen()) { if (reply->isOpen()) {
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
QFile file(filePath(url)); QFile file(filePath);
file.open(QIODevice::WriteOnly); file.open(QIODevice::WriteOnly);
file.write(data); file.write(data);
file.close(); file.close();
@ -248,16 +249,28 @@ QNetworkReply *Fetcher::download(QString url)
void Fetcher::removeImage(const QString &url) void Fetcher::removeImage(const QString &url)
{ {
qDebug() << filePath(url); qDebug() << imagePath(url);
QFile(filePath(url)).remove(); QFile(imagePath(url)).remove();
} }
QString Fetcher::filePath(const QString &url) QString Fetcher::imagePath(const QString &url) const
{ {
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/") + QString::fromStdString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex().toStdString()); QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/images/");
// Create path in cache if it doesn't exist yet
QFileInfo().absoluteDir().mkpath(path);
return path + QString::fromStdString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex().toStdString());
} }
QNetworkReply *Fetcher::get(QNetworkRequest &request)
QString Fetcher::enclosurePath(const QString &url) const
{
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QStringLiteral("/enclosures/");
// Create path in cache if it doesn't exist yet
QFileInfo().absoluteDir().mkpath(path);
return path + QString::fromStdString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex().toStdString());
}
QNetworkReply *Fetcher::get(QNetworkRequest &request) const
{ {
request.setRawHeader("User-Agent", "Alligator/0.1; Syndication"); request.setRawHeader("User-Agent", "Alligator/0.1; Syndication");
return manager->get(request); return manager->get(request);

View File

@ -22,11 +22,12 @@ public:
} }
Q_INVOKABLE void fetch(const QString &url); Q_INVOKABLE void fetch(const QString &url);
Q_INVOKABLE void fetchAll(); Q_INVOKABLE void fetchAll();
Q_INVOKABLE QString image(const QString &url); Q_INVOKABLE QString image(const QString &url) const;
void removeImage(const QString &url); void removeImage(const QString &url);
Q_INVOKABLE QNetworkReply *download(QString url); Q_INVOKABLE QNetworkReply *download(const QString &url, const QString &fileName) const;
QNetworkReply *get(QNetworkRequest &request); QNetworkReply *get(QNetworkRequest &request) const;
QString filePath(const QString &url); QString imagePath(const QString &url) const;
QString enclosurePath(const QString &url) const;
private: private:
Fetcher(); Fetcher();
@ -45,5 +46,5 @@ Q_SIGNALS:
void feedDetailsUpdated(const QString &url, const QString &name, const QString &image, const QString &link, const QString &description, const QDateTime &lastUpdated); void feedDetailsUpdated(const QString &url, const QString &name, const QString &image, const QString &link, const QString &description, const QDateTime &lastUpdated);
void error(const QString &url, int errorId, const QString &errorString); void error(const QString &url, int errorId, const QString &errorString);
void entryAdded(const QString &feedurl, const QString &id); void entryAdded(const QString &feedurl, const QString &id);
void downloadFinished(QString url); void downloadFinished(QString url) const;
}; };