30#include "moc_qgsalgorithmfiledownloader.cpp"
34QString QgsFileDownloaderAlgorithm::name()
const
36 return QStringLiteral(
"filedownloader" );
39QString QgsFileDownloaderAlgorithm::displayName()
const
41 return tr(
"Download file via HTTP(S)" );
44QString QgsFileDownloaderAlgorithm::shortDescription()
const
46 return tr(
"Downloads a URL to the file system with an HTTP(S) GET or POST request." );
49QStringList QgsFileDownloaderAlgorithm::tags()
const
51 return tr(
"file,downloader,internet,url,fetch,get,post,request,https" ).split(
',' );
54QString QgsFileDownloaderAlgorithm::group()
const
56 return tr(
"File tools" );
59QString QgsFileDownloaderAlgorithm::groupId()
const
61 return QStringLiteral(
"filetools" );
64QString QgsFileDownloaderAlgorithm::shortHelpString()
const
66 return tr(
"This algorithm downloads a URL to the file system with an HTTP(S) GET or POST request" );
69QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance()
const
71 return new QgsFileDownloaderAlgorithm();
74void QgsFileDownloaderAlgorithm::initAlgorithm(
const QVariantMap & )
78 auto methodParam = std::make_unique<QgsProcessingParameterEnum>(
79 QStringLiteral(
"METHOD" ),
80 QObject::tr(
"Method" ),
82 << QObject::tr(
"GET" )
83 << QObject::tr(
"POST" ),
87 methodParam->setHelp( QObject::tr(
"The HTTP method to use for the request" ) );
89 addParameter( methodParam.release() );
91 auto dataParam = std::make_unique<QgsProcessingParameterString>(
92 QStringLiteral(
"DATA" ), tr(
"Data" ), QVariant(),
false,
true
94 dataParam->setHelp( QObject::tr(
"The data to add in the body if the request is a POST" ) );
96 addParameter( dataParam.release() );
102 mFeedback = feedback;
103 QString url = parameterAsString( parameters, QStringLiteral(
"URL" ), context );
107 QString data = parameterAsString( parameters, QStringLiteral(
"DATA" ), context );
108 QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT" ), context );
119 feedback->
pushWarning( tr(
"DATA parameter is not used when it's a GET request." ) );
129 connect( &timer, &QTimer::timeout,
this, &QgsFileDownloaderAlgorithm::sendProgressFeedback );
136 if ( errors.size() > 0 )
139 const bool exists = QFileInfo::exists( outputFile );
143 url = downloadedUrl.toDisplayString();
144 feedback->
pushInfo( QObject::tr(
"Successfully downloaded %1" ).arg( url ) );
149 const int length = url.size();
150 const int lastDotIndex = url.lastIndexOf(
"." );
151 const int lastSlashIndex = url.lastIndexOf(
"/" );
152 if ( lastDotIndex > -1 && lastDotIndex > lastSlashIndex && length - lastDotIndex <= 6 )
154 QFile tmpFile( outputFile );
155 tmpFile.rename( tmpFile.fileName() + url.mid( lastDotIndex ) );
156 outputFile += url.mid( lastDotIndex );
161 outputs.insert( QStringLiteral(
"OUTPUT" ), exists ? outputFile : QString() );
165void QgsFileDownloaderAlgorithm::sendProgressFeedback()
167 if ( !mReceived.isEmpty() && mLastReport != mReceived )
169 mLastReport = mReceived;
170 if ( mTotal.isEmpty() )
171 mFeedback->pushInfo( tr(
"%1 downloaded" ).arg( mReceived ) );
173 mFeedback->pushInfo( tr(
"%1 of %2 downloaded" ).arg( mReceived, mTotal ) );
177void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
180 if ( bytesTotal > 0 )
182 if ( mTotal.isEmpty() )
185 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.