Skip to content
Permalink
Browse files
[processing] Fix file download alg stuck in infinit loop when network…
… errors occur
  • Loading branch information
nirvn authored and nyalldawson committed Jul 21, 2021
1 parent 785e785 commit 237394f34cbdc59abb97d5d67ff422931bd25841
@@ -77,9 +77,10 @@ QVariantMap QgsFileDownloaderAlgorithm::processAlgorithm( const QVariantMap &par

QEventLoop loop;
QTimer timer;
QStringList errors;
QgsFileDownloader *downloader = new QgsFileDownloader( QUrl( url ), outputFile, QString(), true );
connect( mFeedback, &QgsFeedback::canceled, downloader, &QgsFileDownloader::cancelDownload );
connect( downloader, &QgsFileDownloader::downloadError, this, &QgsFileDownloaderAlgorithm::reportErrors );
connect( downloader, &QgsFileDownloader::downloadError, this, [&errors, &loop]( const QStringList & e ) { errors = e; loop.exit(); } );
connect( downloader, &QgsFileDownloader::downloadProgress, this, &QgsFileDownloaderAlgorithm::receiveProgressFromDownloader );
connect( downloader, &QgsFileDownloader::downloadExited, &loop, &QEventLoop::quit );
connect( &timer, &QTimer::timeout, this, &QgsFileDownloaderAlgorithm::sendProgressFeedback );
@@ -89,6 +90,9 @@ QVariantMap QgsFileDownloaderAlgorithm::processAlgorithm( const QVariantMap &par
loop.exec();

timer.stop();
if ( errors.size() > 0 )
throw QgsProcessingException( errors.join( '\n' ) );

bool exists = QFileInfo::exists( outputFile );
if ( !feedback->isCanceled() && !exists )
throw QgsProcessingException( tr( "Output file doesn't exist." ) );
@@ -98,11 +102,6 @@ QVariantMap QgsFileDownloaderAlgorithm::processAlgorithm( const QVariantMap &par
return outputs;
}

void QgsFileDownloaderAlgorithm::reportErrors( const QStringList &errors )
{
throw QgsProcessingException( errors.join( '\n' ) );
}

void QgsFileDownloaderAlgorithm::sendProgressFeedback()
{
if ( !mReceived.isEmpty() && mLastReport != mReceived )
@@ -55,7 +55,6 @@ class QgsFileDownloaderAlgorithm : public QObject, public QgsProcessingAlgorithm
QString mReceived;
QgsProcessingFeedback *mFeedback = nullptr;
QString mLastReport;
void reportErrors( const QStringList &errors );
void receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal );
void sendProgressFeedback();
};

0 comments on commit 237394f

Please sign in to comment.