31QString QgsFileDownloaderAlgorithm::name()
const
33 return QStringLiteral(
"filedownloader" );
36QString QgsFileDownloaderAlgorithm::displayName()
const
38 return tr(
"Download file" );
41QStringList QgsFileDownloaderAlgorithm::tags()
const
43 return tr(
"file,downloader,internet,url,fetch,get,https" ).split(
',' );
46QString QgsFileDownloaderAlgorithm::group()
const
48 return tr(
"File tools" );
51QString QgsFileDownloaderAlgorithm::groupId()
const
53 return QStringLiteral(
"filetools" );
56QString QgsFileDownloaderAlgorithm::shortHelpString()
const
58 return tr(
"This algorithm downloads a URL on the file system." );
61QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance()
const
63 return new QgsFileDownloaderAlgorithm();
66void QgsFileDownloaderAlgorithm::initAlgorithm(
const QVariantMap & )
70 std::unique_ptr< QgsProcessingParameterEnum > methodParam = std::make_unique < QgsProcessingParameterEnum > (
71 QStringLiteral(
"METHOD" ),
72 QObject::tr(
"Method" ),
74 << QObject::tr(
"GET" )
75 << QObject::tr(
"POST" ),
79 methodParam->setHelp( QObject::tr(
"The HTTP method to use for the request" ) );
81 addParameter( methodParam.release() );
83 std::unique_ptr< QgsProcessingParameterString > dataParam = std::make_unique < QgsProcessingParameterString >(
84 QStringLiteral(
"DATA" ), tr(
"Data" ), QVariant(),
false,
true );
85 dataParam->setHelp( QObject::tr(
"The data to add in the body if the request is a POST" ) );
87 addParameter( dataParam.release() );
90 tr(
"File destination" ), QObject::tr(
"All files (*.*)" ), QVariant(),
true ) );
96 QString url = parameterAsString( parameters, QStringLiteral(
"URL" ), context );
100 QString data = parameterAsString( parameters, QStringLiteral(
"DATA" ), context );
101 QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT" ), context );
112 feedback->
pushWarning( tr(
"DATA parameter is not used when it's a GET request." ) );
122 connect( &timer, &QTimer::timeout,
this, &QgsFileDownloaderAlgorithm::sendProgressFeedback );
129 if ( errors.size() > 0 )
132 const bool exists = QFileInfo::exists( outputFile );
136 url = downloadedUrl.toDisplayString();
137 feedback->
pushInfo( QObject::tr(
"Successfully downloaded %1" ).arg( url ) );
142 const int length = url.size();
143 const int lastDotIndex = url.lastIndexOf(
"." );
144 const int lastSlashIndex = url.lastIndexOf(
"/" );
145 if ( lastDotIndex > -1 && lastDotIndex > lastSlashIndex && length - lastDotIndex <= 6 )
147 QFile tmpFile( outputFile );
148 tmpFile.rename( tmpFile.fileName() + url.mid( lastDotIndex ) );
149 outputFile += url.mid( lastDotIndex );
154 outputs.insert( QStringLiteral(
"OUTPUT" ), exists ? outputFile : QString() );
158void QgsFileDownloaderAlgorithm::sendProgressFeedback()
160 if ( !mReceived.isEmpty() && mLastReport != mReceived )
162 mLastReport = mReceived;
163 if ( mTotal.isEmpty() )
164 mFeedback->pushInfo( tr(
"%1 downloaded" ).arg( mReceived ) );
166 mFeedback->pushInfo( tr(
"%1 of %2 downloaded" ).arg( mReceived, mTotal ) );
170void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
173 if ( bytesTotal > 0 )
175 if ( mTotal.isEmpty() )
178 mFeedback->setProgress( ( bytesReceived * 100 ) / bytesTotal );
HttpMethod
Different methods of HTTP requests.
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.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
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.