31QString QgsFileDownloaderAlgorithm::name()
const
33 return QStringLiteral(
"filedownloader" );
36QString QgsFileDownloaderAlgorithm::displayName()
const
38 return tr(
"Download file via HTTP(S)" );
41QString QgsFileDownloaderAlgorithm::shortDescription()
const
43 return tr(
"Downloads a URL to the file system with an HTTP(S) GET or POST request" );
46QStringList QgsFileDownloaderAlgorithm::tags()
const
48 return tr(
"file,downloader,internet,url,fetch,get,post,request,https" ).split(
',' );
51QString QgsFileDownloaderAlgorithm::group()
const
53 return tr(
"File tools" );
56QString QgsFileDownloaderAlgorithm::groupId()
const
58 return QStringLiteral(
"filetools" );
61QString QgsFileDownloaderAlgorithm::shortHelpString()
const
63 return tr(
"This algorithm downloads a URL to the file system with an HTTP(S) GET or POST request" );
66QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance()
const
68 return new QgsFileDownloaderAlgorithm();
71void QgsFileDownloaderAlgorithm::initAlgorithm(
const QVariantMap & )
75 std::unique_ptr< QgsProcessingParameterEnum > methodParam = std::make_unique < QgsProcessingParameterEnum > (
76 QStringLiteral(
"METHOD" ),
77 QObject::tr(
"Method" ),
79 << QObject::tr(
"GET" )
80 << QObject::tr(
"POST" ),
84 methodParam->setHelp( QObject::tr(
"The HTTP method to use for the request" ) );
86 addParameter( methodParam.release() );
88 std::unique_ptr< QgsProcessingParameterString > dataParam = std::make_unique < QgsProcessingParameterString >(
89 QStringLiteral(
"DATA" ), tr(
"Data" ), QVariant(),
false,
true );
90 dataParam->setHelp( QObject::tr(
"The data to add in the body if the request is a POST" ) );
92 addParameter( dataParam.release() );
94 tr(
"File destination" ), QObject::tr(
"All files (*.*)" ), QVariant(),
false ) );
100 QString url = parameterAsString( parameters, QStringLiteral(
"URL" ), context );
104 QString data = parameterAsString( parameters, QStringLiteral(
"DATA" ), context );
105 QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT" ), context );
116 feedback->
pushWarning( tr(
"DATA parameter is not used when it's a GET request." ) );
126 connect( &timer, &QTimer::timeout,
this, &QgsFileDownloaderAlgorithm::sendProgressFeedback );
133 if ( errors.size() > 0 )
136 const bool exists = QFileInfo::exists( outputFile );
140 url = downloadedUrl.toDisplayString();
141 feedback->
pushInfo( QObject::tr(
"Successfully downloaded %1" ).arg( url ) );
146 const int length = url.size();
147 const int lastDotIndex = url.lastIndexOf(
"." );
148 const int lastSlashIndex = url.lastIndexOf(
"/" );
149 if ( lastDotIndex > -1 && lastDotIndex > lastSlashIndex && length - lastDotIndex <= 6 )
151 QFile tmpFile( outputFile );
152 tmpFile.rename( tmpFile.fileName() + url.mid( lastDotIndex ) );
153 outputFile += url.mid( lastDotIndex );
158 outputs.insert( QStringLiteral(
"OUTPUT" ), exists ? outputFile : QString() );
162void QgsFileDownloaderAlgorithm::sendProgressFeedback()
164 if ( !mReceived.isEmpty() && mLastReport != mReceived )
166 mLastReport = mReceived;
167 if ( mTotal.isEmpty() )
168 mFeedback->pushInfo( tr(
"%1 downloaded" ).arg( mReceived ) );
170 mFeedback->pushInfo( tr(
"%1 of %2 downloaded" ).arg( mReceived, mTotal ) );
174void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
177 if ( bytesTotal > 0 )
179 if ( mTotal.isEmpty() )
182 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.