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::statusChanged, &DownloadProgressModel::instance(), &DownloadProgressModel::monitorDownloadProgress);
connect(this, &Enclosure::downloadError, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages); connect(this, &Enclosure::downloadError, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages);
connect(&Fetcher::instance(), &Fetcher::downloadFileSizeUpdated, this, [this](QString url, int fileSize) { connect(&Fetcher::instance(), &Fetcher::downloadFileSizeUpdated, this, [this](QString url, int fileSize, int resumedAt) {
if ((url == m_url) && (fileSize != m_size)) { if ((url == m_url) && ((m_size != fileSize) && (m_size != fileSize + resumedAt)) && (fileSize > 1000)) {
qDebug() << "Correct filesize for enclosure" << url << "to" << fileSize; // 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); setSize(fileSize);
} }
}); });
@ -151,7 +155,7 @@ void Enclosure::processDownloadedFile()
// if not, correct the filesize in the database // if not, correct the filesize in the database
// otherwise the file will get deleted because of mismatch in signature // otherwise the file will get deleted because of mismatch in signature
if (file.size() != m_size) { 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()); setSize(file.size());
} }

View File

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

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 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 entryAdded(const QString &feedurl, const QString &id);
void downloadFinished(QString url) const; 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 updateProgressChanged(int progress);
void updateTotalChanged(int nrOfFeeds); void updateTotalChanged(int nrOfFeeds);