Skip to content
Permalink
Browse files
Merge pull request #44094 from nirvn/filedownloaderalg_throw_fix
[processing] Fix file download alg stuck in infinit loop when network errors occur
  • Loading branch information
rouault committed Jul 10, 2021
2 parents 72626d1 + 4f6f4be commit 2071cd80d6f65f2a81855b56ed90a5f57b98593f
@@ -78,9 +78,10 @@ QVariantMap QgsFileDownloaderAlgorithm::processAlgorithm( const QVariantMap &par
QEventLoop loop;
QTimer timer;
QUrl downloadedUrl;
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::downloadCompleted, this, [&downloadedUrl]( const QUrl url ) { downloadedUrl = url; } );
connect( downloader, &QgsFileDownloader::downloadExited, &loop, &QEventLoop::quit );
@@ -91,6 +92,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." ) );
@@ -117,11 +121,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 2071cd8

Please sign in to comment.