Get correct enclosure file size as soon as possible when download starts

This commit is contained in:
Bart De Vries 2021-06-04 22:47:40 +02:00
parent d24b7ed7d0
commit a24fb7d731
3 changed files with 34 additions and 2 deletions

View File

@ -26,6 +26,12 @@ Enclosure::Enclosure(Entry *entry)
{
connect(this, &Enclosure::statusChanged, &DownloadProgressModel::instance(), &DownloadProgressModel::monitorDownloadProgress);
connect(this, &Enclosure::downloadError, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages);
connect(&Fetcher::instance(), &Fetcher::downloadFileSizeUpdated, this, [this](QString url, int fileSize) {
if ((url == m_url) && (fileSize != m_size)) {
qDebug() << "Correct filesize for enclosure" << url << "to" << fileSize;
setSize(fileSize);
}
});
QSqlQuery query;
query.prepare(QStringLiteral("SELECT * FROM Enclosures WHERE id=:id"));

View File

@ -373,6 +373,17 @@ QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) co
file->open(QIODevice::WriteOnly);
}
QNetworkReply *headerReply = head(request);
connect(headerReply, &QNetworkReply::finished, this, [=]() {
if (headerReply->isOpen()) {
qDebug() << "size" << headerReply->header(QNetworkRequest::ContentLengthHeader);
int fileSize = headerReply->header(QNetworkRequest::ContentLengthHeader).toInt();
Q_EMIT downloadFileSizeUpdated(url, fileSize);
}
headerReply->deleteLater();
});
QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::readyRead, this, [=]() {
@ -428,6 +439,17 @@ QString Fetcher::enclosurePath(const QString &url) const
QNetworkReply *Fetcher::get(QNetworkRequest &request) const
{
request.setRawHeader("User-Agent", "Kasts/0.1; Syndication");
setHeader(request);
return manager->get(request);
}
QNetworkReply *Fetcher::head(QNetworkRequest &request) const
{
setHeader(request);
return manager->head(request);
}
void Fetcher::setHeader(QNetworkRequest &request) const
{
request.setRawHeader("User-Agent", "Kasts/0.1; Syndication");
}

View File

@ -35,7 +35,6 @@ public:
Q_INVOKABLE QString image(const QString &url) const;
void removeImage(const QString &url);
Q_INVOKABLE QNetworkReply *download(const QString &url, const QString &fileName) const;
QNetworkReply *get(QNetworkRequest &request) const;
QString imagePath(const QString &url) const;
QString enclosurePath(const QString &url) const;
@ -52,6 +51,7 @@ Q_SIGNALS:
void error(const QString &url, const QString &id, const int errorId, const QString &errorString);
void entryAdded(const QString &feedurl, const QString &id);
void downloadFinished(QString url) const;
void downloadFileSizeUpdated(QString url, int fileSize) const;
void updateProgressChanged(int progress);
void updateTotalChanged(int nrOfFeeds);
@ -69,6 +69,10 @@ private:
void processAuthor(const QString &url, const QString &entryId, const QString &authorName, const QString &authorUri, const QString &authorEmail);
void processEnclosure(Syndication::EnclosurePtr enclosure, Syndication::ItemPtr entry, const QString &feedUrl);
QNetworkReply *get(QNetworkRequest &request) const;
QNetworkReply *head(QNetworkRequest &request) const;
void setHeader(QNetworkRequest &request) const;
QNetworkAccessManager *manager;
int m_updateProgress;
int m_updateTotal;