31 QString QgsFileDownloaderAlgorithm::name()
const
33 return QStringLiteral(
"filedownloader" );
36 QString QgsFileDownloaderAlgorithm::displayName()
const
38 return tr(
"Download file" );
41 QStringList QgsFileDownloaderAlgorithm::tags()
const
43 return tr(
"file,downloader,internet,url,fetch,get,https" ).split(
',' );
46 QString QgsFileDownloaderAlgorithm::group()
const
48 return tr(
"File tools" );
51 QString QgsFileDownloaderAlgorithm::groupId()
const
53 return QStringLiteral(
"filetools" );
56 QString QgsFileDownloaderAlgorithm::shortHelpString()
const
58 return tr(
"This algorithm downloads a URL on the file system." );
61 QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance()
const
63 return new QgsFileDownloaderAlgorithm();
66 void 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() );
158 void 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 ) );
170 void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
173 if ( bytesTotal > 0 )
175 if ( mTotal.isEmpty() )
178 mFeedback->setProgress( ( bytesReceived * 100 ) / bytesTotal );