31#include "moc_qgsalgorithmfiledownloader.cpp"
33using namespace Qt::StringLiterals;
37QString QgsFileDownloaderAlgorithm::name()
const
39 return u
"filedownloader"_s;
42QString QgsFileDownloaderAlgorithm::displayName()
const
44 return tr(
"Download file via HTTP(S)" );
47QString QgsFileDownloaderAlgorithm::shortDescription()
const
49 return tr(
"Downloads a URL to the file system with an HTTP(S) GET or POST request." );
52QStringList QgsFileDownloaderAlgorithm::tags()
const
54 return tr(
"file,downloader,internet,url,fetch,get,post,request,https" ).split(
',' );
57QString QgsFileDownloaderAlgorithm::group()
const
59 return tr(
"File tools" );
62QString QgsFileDownloaderAlgorithm::groupId()
const
64 return u
"filetools"_s;
67QString QgsFileDownloaderAlgorithm::shortHelpString()
const
69 return tr(
"This algorithm downloads a URL to the file system with an HTTP(S) GET or POST request" );
72QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance()
const
74 return new QgsFileDownloaderAlgorithm();
77void QgsFileDownloaderAlgorithm::initAlgorithm(
const QVariantMap & )
81 auto methodParam = std::make_unique<QgsProcessingParameterEnum>(
83 QObject::tr(
"Method" ),
85 << QObject::tr(
"GET" )
86 << QObject::tr(
"POST" ),
90 methodParam->setHelp( QObject::tr(
"The HTTP method to use for the request" ) );
92 addParameter( methodParam.release() );
94 auto dataParam = std::make_unique<QgsProcessingParameterString>(
95 u
"DATA"_s, tr(
"Data" ), QVariant(),
false,
true
97 dataParam->setHelp( QObject::tr(
"The data to add in the body if the request is a POST" ) );
99 addParameter( dataParam.release() );
105 mFeedback = feedback;
106 QString url = parameterAsString( parameters, u
"URL"_s, context );
110 QString data = parameterAsString( parameters, u
"DATA"_s, context );
111 QString outputFile = parameterAsFileOutput( parameters, u
"OUTPUT"_s, context );
122 feedback->
pushWarning( tr(
"DATA parameter is not used when it's a GET request." ) );
132 connect( &timer, &QTimer::timeout,
this, &QgsFileDownloaderAlgorithm::sendProgressFeedback );
139 if ( errors.size() > 0 )
142 const bool exists = QFileInfo::exists( outputFile );
146 url = downloadedUrl.toDisplayString();
147 feedback->
pushInfo( QObject::tr(
"Successfully downloaded %1" ).arg( url ) );
152 const int length = url.size();
153 const int lastDotIndex = url.lastIndexOf(
"." );
154 const int lastSlashIndex = url.lastIndexOf(
"/" );
155 if ( lastDotIndex > -1 && lastDotIndex > lastSlashIndex && length - lastDotIndex <= 6 )
157 QFile tmpFile( outputFile );
158 tmpFile.rename( tmpFile.fileName() + url.mid( lastDotIndex ) );
159 outputFile += url.mid( lastDotIndex );
164 outputs.insert( u
"OUTPUT"_s, exists ? outputFile : QString() );
168void QgsFileDownloaderAlgorithm::sendProgressFeedback()
170 if ( !mReceived.isEmpty() && mLastReport != mReceived )
172 mLastReport = mReceived;
173 if ( mTotal.isEmpty() )
174 mFeedback->pushInfo( tr(
"%1 downloaded" ).arg( mReceived ) );
176 mFeedback->pushInfo( tr(
"%1 of %2 downloaded" ).arg( mReceived, mTotal ) );
180void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
183 if ( bytesTotal > 0 )
185 if ( mTotal.isEmpty() )
188 mFeedback->setProgress( ( bytesReceived * 100 ) / bytesTotal );
HttpMethod
Different methods of HTTP requests.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
bool isCanceled() const
Tells whether the operation has been canceled already.
void canceled()
Internal routines can connect to this signal if they use event loop.
A utility class for downloading files.
void cancelDownload()
Call to abort the download and delete this object after the cancellation has been processed.
void downloadExited()
Emitted always when the downloader exits.
void downloadError(QStringList errorMessages)
Emitted when an error makes the download fail.
void startDownload()
Called to start the download.
void downloadCompleted(const QUrl &url)
Emitted when the download has completed successfully.
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data are ready to be processed.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A string parameter for processing algorithms.
static const QString TEMPORARY_OUTPUT
Constant used to indicate that a Processing algorithm output should be a temporary layer/file.