Improve estimate of enclosure download size, also when resuming

This commit is contained in:
Bart De Vries 2021-06-23 22:29:15 +02:00
parent 69a87b1afc
commit 03081edc66
3 changed files with 17 additions and 12 deletions

View File

@ -27,9 +27,13 @@ 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;
connect(&Fetcher::instance(), &Fetcher::downloadFileSizeUpdated, this, [this](QString url, int fileSize, int resumedAt) {
if ((url == m_url) && ((m_size != fileSize) && (m_size != fileSize + resumedAt)) && (fileSize > 1000)) {
// Sometimes, when resuming a download, the complete file size is
// reported. Other times only the remaining part.
// Sometimes the value is rubbish (e.g. 2)
// We assume that the value when starting a new download is correct.
qDebug() << "Correct filesize for enclosure" << url << "from" << m_size << "to" << fileSize;
setSize(fileSize);
}
});
@ -151,7 +155,7 @@ void Enclosure::processDownloadedFile()
// if not, correct the filesize in the database
// otherwise the file will get deleted because of mismatch in signature
if (file.size() != m_size) {
qCDebug(kastsEnclosure) << "enclosure file size mismatch" << m_entry->title();
qCDebug(kastsEnclosure) << "enclosure file size mismatch" << m_entry->title() << "from" << m_size << "to" << file.size();
setSize(file.size());
}

View File

@ -362,10 +362,12 @@ QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) co
request.setTransferTimeout();
QFile *file = new QFile(filePath);
int resumedAt = 0;
if (file->exists() && file->size() > 0) {
// try to resume download
qCDebug(kastsFetcher) << "Resuming download at" << file->size() << "bytes";
QByteArray rangeHeaderValue = QByteArray("bytes=") + QByteArray::number(file->size()) + QByteArray("-");
resumedAt = file->size();
qCDebug(kastsFetcher) << "Resuming download at" << resumedAt << "bytes";
QByteArray rangeHeaderValue = QByteArray("bytes=") + QByteArray::number(resumedAt) + QByteArray("-");
request.setRawHeader(QByteArray("Range"), rangeHeaderValue);
file->open(QIODevice::WriteOnly | QIODevice::Append);
} else {
@ -376,10 +378,9 @@ QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) co
QNetworkReply *headerReply = head(request);
connect(headerReply, &QNetworkReply::finished, this, [=]() {
if (headerReply->isOpen()) {
qCDebug(kastsFetcher) << "size" << headerReply->header(QNetworkRequest::ContentLengthHeader);
int fileSize = headerReply->header(QNetworkRequest::ContentLengthHeader).toInt();
Q_EMIT downloadFileSizeUpdated(url, fileSize);
qCDebug(kastsFetcher) << "Reported download size" << fileSize;
Q_EMIT downloadFileSizeUpdated(url, fileSize, resumedAt);
}
headerReply->deleteLater();
});
@ -452,4 +453,4 @@ QNetworkReply *Fetcher::head(QNetworkRequest &request) const
void Fetcher::setHeader(QNetworkRequest &request) const
{
request.setRawHeader("User-Agent", "Kasts/0.1; Syndication");
}
}

View File

@ -54,7 +54,7 @@ Q_SIGNALS:
void error(Error::Type type, 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 downloadFileSizeUpdated(QString url, int fileSize, int resumedAt) const;
void updateProgressChanged(int progress);
void updateTotalChanged(int nrOfFeeds);
@ -80,4 +80,4 @@ private:
int m_updateProgress;
int m_updateTotal;
bool m_updating;
};
};