21 #include <QNetworkAccessManager>
22 #include <QNetworkRequest>
23 #include <QNetworkReply>
30 , mDownloadCanceled( false )
31 , mHttpMethod( httpMethod )
34 mFile.setFileName( outputFileName );
46 mReply->deleteLater();
54 QNetworkRequest request( mUrl );
56 if ( !mAuthCfg.isEmpty() )
63 disconnect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
64 disconnect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
65 disconnect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
67 mReply->deleteLater();
70 switch ( mHttpMethod )
74 mReply = nam->get( request );
79 mReply = nam->post( request, mData );
84 if ( !mAuthCfg.isEmpty() )
89 connect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
90 connect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
91 connect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
94 connect( nam, &QgsNetworkAccessManager::sslErrors,
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
100 mDownloadCanceled =
true;
105 void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
107 if ( reply == mReply )
108 error( tr(
"Network request %1 timed out" ).arg( mUrl.toString() ) );
112 void QgsFileDownloader::onSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
114 if ( reply == mReply )
116 QStringList errorMessages;
117 errorMessages.reserve( errors.size() + 1 );
118 errorMessages << QStringLiteral(
"SSL Errors: " );
120 for (
const QSslError &error : errors )
121 errorMessages << error.errorString();
123 error( errorMessages );
129 void QgsFileDownloader::error(
const QStringList &errorMessages )
131 for (
const QString &error : errorMessages )
139 void QgsFileDownloader::error(
const QString &errorMessage )
141 error( QStringList() << errorMessage );
144 void QgsFileDownloader::onReadyRead()
147 if ( mFile.fileName().isEmpty() )
149 error( tr(
"No output filename specified" ) );
152 else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
154 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
159 const QByteArray data = mReply->readAll();
164 void QgsFileDownloader::onFinished()
167 if ( ! mErrors.isEmpty() || mDownloadCanceled )
169 if ( mFile.isOpen() )
171 if ( mFile.exists() )
177 if ( mFile.isOpen() )
184 const QVariant redirectionTarget = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
185 if ( mReply->error() )
188 error( tr(
"Download failed: %1" ).arg( mReply->errorString() ) );
190 else if ( !redirectionTarget.isNull() )
192 const QUrl newUrl = mUrl.resolved( redirectionTarget.toUrl() );
194 mReply->deleteLater();
195 if ( !mFile.open( QIODevice::WriteOnly ) )
198 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
218 void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
220 if ( mDownloadCanceled )