QGIS API Documentation 4.1.0-Master (376402f9aeb)
Loading...
Searching...
No Matches
qgsalgorithmfileuploader.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmfileuploader.cpp
3 ---------------------
4 Date : August 2025
5 Copyright : (C) 2025 by Valentin Buira
6 Email : valentin dot buira at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgis.h"
21#include "qgsfileuploader.h"
22#include "qgsfileutils.h"
25
26#include <QFileInfo>
27#include <QString>
28#include <QTimer>
29#include <QUrl>
30
31#include "moc_qgsalgorithmfileuploader.cpp"
32
33using namespace Qt::StringLiterals;
34
36
37QString QgsFileUploaderAlgorithm::name() const
38{
39 return u"fileuploader"_s;
40}
41
42QString QgsFileUploaderAlgorithm::displayName() const
43{
44 return tr( "Upload a file via HTTP(S)" );
45}
46
47QString QgsFileUploaderAlgorithm::shortDescription() const
48{
49 return tr( "Uploads a file to the URL via a HTTP(S) request." );
50}
51
52QStringList QgsFileUploaderAlgorithm::tags() const
53{
54 return tr( "file,uploader,internet,url,upload,post,request,https" ).split( ',' );
55}
56
57QString QgsFileUploaderAlgorithm::group() const
58{
59 return tr( "File tools" );
60}
61
62QString QgsFileUploaderAlgorithm::groupId() const
63{
64 return u"filetools"_s;
65}
66
67QString QgsFileUploaderAlgorithm::shortHelpString() const
68{
69 return tr(
70 "This algorithm uploads a file to the URL via a HTTP(S) request\n\n"
71 "The optional form name field parameter emulates a filled-in form in which a user has pressed the submit button. This enables uploading of binary files when the URL endpoint requires a form name "
72 "key."
73 );
74}
75
76QgsFileUploaderAlgorithm *QgsFileUploaderAlgorithm::createInstance() const
77{
78 return new QgsFileUploaderAlgorithm();
79}
80
81void QgsFileUploaderAlgorithm::initAlgorithm( const QVariantMap & )
82{
83 addParameter(
84 new QgsProcessingParameterFile( u"FILE"_s, QObject::tr( "File to upload" ), Qgis::ProcessingFileParameterBehavior::File, QString(), QVariant(), false, QObject::tr( "All files (%1)" ).arg( "*.*"_L1 ) )
85 );
86 addParameter( new QgsProcessingParameterString( u"URL"_s, tr( "Destination URL" ), QVariant(), false, false ) );
87
88 auto formNameParam = std::make_unique<QgsProcessingParameterString>( u"FORM_NAME"_s, tr( "Form name field" ), QString(), false, true );
89 formNameParam->setHelp(
90 QObject::tr( "The optional form name field parameter emulates a filled-in form in which a user has pressed the submit button. This enables uploading of binary files when url end point requires a form name key" )
91 );
92 addParameter( formNameParam.release() );
93}
94
95QVariantMap QgsFileUploaderAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
96{
97 mFeedback = feedback;
98 QString url = parameterAsString( parameters, u"URL"_s, context );
99 if ( url.isEmpty() )
100 throw QgsProcessingException( tr( "No URL specified" ) );
101
102 const QString filePath = parameterAsFile( parameters, u"FILE"_s, context );
103 const bool exists = QFileInfo::exists( filePath );
104 if ( !feedback->isCanceled() && !exists )
105 throw QgsProcessingException( tr( "The file %1 doesn't exist." ).arg( filePath ) );
106
107 const QString formNameKey = parameterAsString( parameters, u"FORM_NAME"_s, context );
108
109 QUrl uploadUrl;
110 QStringList errors;
111
112
113 QgsFileUploader *uploader = new QgsFileUploader( filePath, url, formNameKey );
114
115 connect( mFeedback, &QgsFeedback::canceled, uploader, &QgsFileUploader::cancelUpload );
116 connect( uploader, &QgsFileUploader::uploadError, this, [&errors]( const QStringList &e ) { errors = e; } );
117 connect( uploader, &QgsFileUploader::uploadProgress, this, &QgsFileUploaderAlgorithm::receiveProgressFromUploader );
118 connect( uploader, &QgsFileUploader::uploadCompleted, this, [&uploadUrl]( const QUrl url ) { uploadUrl = url; } );
119
120 uploader->startUpload();
121
122 if ( errors.size() > 0 )
123 throw QgsProcessingException( errors.join( '\n' ) );
124
125
126 url = uploadUrl.toDisplayString();
127 feedback->pushInfo( QObject::tr( "Successfully uploaded file to %1" ).arg( url ) );
128
129 QVariantMap outputs;
130 return outputs;
131}
132
133void QgsFileUploaderAlgorithm::sendProgressFeedback()
134{
135 if ( !mSent.isEmpty() && mLastReport != mSent )
136 {
137 mLastReport = mSent;
138 if ( mTotal.isEmpty() )
139 mFeedback->pushInfo( tr( "%1 uploaded" ).arg( mSent ) );
140 else
141 mFeedback->pushInfo( tr( "%1 of %2 uploaded" ).arg( mSent, mTotal ) );
142 }
143}
144
145void QgsFileUploaderAlgorithm::receiveProgressFromUploader( qint64 bytesSent, qint64 bytesTotal )
146{
147 mSent = QgsFileUtils::representFileSize( bytesSent );
148 if ( bytesTotal > 0 )
149 {
150 if ( mTotal.isEmpty() )
151 mTotal = QgsFileUtils::representFileSize( bytesTotal );
152
153 mFeedback->setProgress( ( static_cast<double>( bytesSent ) / static_cast<double>( bytesTotal ) ) * 100.0 );
154 }
155}
156
@ File
Parameter is a single file.
Definition qgis.h:3973
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
void canceled()
Internal routines can connect to this signal if they use event loop.
A utility class for uploading files.
void startUpload()
Called to start the upload.
void cancelUpload()
Call to abort the upload and delete this object after the cancellation has been processed.
void uploadError(QStringList errorMessages)
Emitted when an error makes the upload fail.
void uploadCompleted(const QUrl &url)
Emitted when the upload has completed successfully.
void uploadProgress(qint64 bytesSent, 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.
An input file or folder parameter for processing algorithms.
A string parameter for processing algorithms.