19#include "moc_qgsalgorithmfiledownloader.cpp"
32QString QgsFileDownloaderAlgorithm::name()
const
34 return QStringLiteral(
"filedownloader" );
37QString QgsFileDownloaderAlgorithm::displayName()
const
39 return tr(
"Download file via HTTP(S)" );
42QString QgsFileDownloaderAlgorithm::shortDescription()
const
44 return tr(
"Downloads a URL to the file system with an HTTP(S) GET or POST request" );
47QStringList QgsFileDownloaderAlgorithm::tags()
const
49 return tr(
"file,downloader,internet,url,fetch,get,post,request,https" ).split(
',' );
52QString QgsFileDownloaderAlgorithm::group()
const
54 return tr(
"File tools" );
57QString QgsFileDownloaderAlgorithm::groupId()
const
59 return QStringLiteral(
"filetools" );
62QString QgsFileDownloaderAlgorithm::shortHelpString()
const
64 return tr(
"This algorithm downloads a URL to the file system with an HTTP(S) GET or POST request" );
67QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance()
const
69 return new QgsFileDownloaderAlgorithm();
72void QgsFileDownloaderAlgorithm::initAlgorithm(
const QVariantMap & )
76 std::unique_ptr<QgsProcessingParameterEnum> methodParam = std::make_unique<QgsProcessingParameterEnum>(
77 QStringLiteral(
"METHOD" ),
78 QObject::tr(
"Method" ),
80 << QObject::tr(
"GET" )
81 << QObject::tr(
"POST" ),
85 methodParam->setHelp( QObject::tr(
"The HTTP method to use for the request" ) );
87 addParameter( methodParam.release() );
89 std::unique_ptr<QgsProcessingParameterString> dataParam = std::make_unique<QgsProcessingParameterString>(
90 QStringLiteral(
"DATA" ), tr(
"Data" ), QVariant(),
false,
true
92 dataParam->setHelp( QObject::tr(
"The data to add in the body if the request is a POST" ) );
94 addParameter( dataParam.release() );
100 mFeedback = feedback;
101 QString url = parameterAsString( parameters, QStringLiteral(
"URL" ), context );
105 QString data = parameterAsString( parameters, QStringLiteral(
"DATA" ), context );
106 QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT" ), context );
117 feedback->
pushWarning( tr(
"DATA parameter is not used when it's a GET request." ) );
127 connect( &timer, &QTimer::timeout,
this, &QgsFileDownloaderAlgorithm::sendProgressFeedback );
134 if ( errors.size() > 0 )
137 const bool exists = QFileInfo::exists( outputFile );
141 url = downloadedUrl.toDisplayString();
142 feedback->
pushInfo( QObject::tr(
"Successfully downloaded %1" ).arg( url ) );
147 const int length = url.size();
148 const int lastDotIndex = url.lastIndexOf(
"." );
149 const int lastSlashIndex = url.lastIndexOf(
"/" );
150 if ( lastDotIndex > -1 && lastDotIndex > lastSlashIndex && length - lastDotIndex <= 6 )
152 QFile tmpFile( outputFile );
153 tmpFile.rename( tmpFile.fileName() + url.mid( lastDotIndex ) );
154 outputFile += url.mid( lastDotIndex );
159 outputs.insert( QStringLiteral(
"OUTPUT" ), exists ? outputFile : QString() );
163void QgsFileDownloaderAlgorithm::sendProgressFeedback()
165 if ( !mReceived.isEmpty() && mLastReport != mReceived )
167 mLastReport = mReceived;
168 if ( mTotal.isEmpty() )
169 mFeedback->pushInfo( tr(
"%1 downloaded" ).arg( mReceived ) );
171 mFeedback->pushInfo( tr(
"%1 of %2 downloaded" ).arg( mReceived, mTotal ) );
175void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
178 if ( bytesTotal > 0 )
180 if ( mTotal.isEmpty() )
183 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.
QgsFileDownloader is 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 QString tempFolder(const QgsProcessingContext *context=nullptr)
Returns a session specific processing temporary folder for use in processing algorithms.