QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
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 QGS_MARK_ALGORITHM_SOURCE
98
99 mFeedback = feedback;
100 QString url = parameterAsString( parameters, u"URL"_s, context );
101 if ( url.isEmpty() )
102 throw QgsProcessingException( tr( "No URL specified" ) );
103
104 const QString filePath = parameterAsFile( parameters, u"FILE"_s, context );
105 const bool exists = QFileInfo::exists( filePath );
106 if ( !feedback->isCanceled() && !exists )
107 throw QgsProcessingException( tr( "The file %1 doesn't exist." ).arg( filePath ) );
108
109 const QString formNameKey = parameterAsString( parameters, u"FORM_NAME"_s, context );
110
111 QUrl uploadUrl;
112 QStringList errors;
113
114
115 QgsFileUploader *uploader = new QgsFileUploader( filePath, url, formNameKey );
116
117 connect( mFeedback, &QgsFeedback::canceled, uploader, &QgsFileUploader::cancelUpload );
118 connect( uploader, &QgsFileUploader::uploadError, this, [&errors]( const QStringList &e ) { errors = e; } );
119 connect( uploader, &QgsFileUploader::uploadProgress, this, &QgsFileUploaderAlgorithm::receiveProgressFromUploader );
120 connect( uploader, &QgsFileUploader::uploadCompleted, this, [&uploadUrl]( const QUrl url ) { uploadUrl = url; } );
121
122 uploader->startUpload();
123
124 if ( errors.size() > 0 )
125 throw QgsProcessingException( errors.join( '\n' ) );
126
127
128 url = uploadUrl.toDisplayString();
129 feedback->pushInfo( QObject::tr( "Successfully uploaded file to %1" ).arg( url ) );
130
131 QVariantMap outputs;
132 return outputs;
133}
134
135void QgsFileUploaderAlgorithm::sendProgressFeedback()
136{
137 if ( !mSent.isEmpty() && mLastReport != mSent )
138 {
139 mLastReport = mSent;
140 if ( mTotal.isEmpty() )
141 mFeedback->pushInfo( tr( "%1 uploaded" ).arg( mSent ) );
142 else
143 mFeedback->pushInfo( tr( "%1 of %2 uploaded" ).arg( mSent, mTotal ) );
144 }
145}
146
147void QgsFileUploaderAlgorithm::receiveProgressFromUploader( qint64 bytesSent, qint64 bytesTotal )
148{
149 mSent = QgsFileUtils::representFileSize( bytesSent );
150 if ( bytesTotal > 0 )
151 {
152 if ( mTotal.isEmpty() )
153 mTotal = QgsFileUtils::representFileSize( bytesTotal );
154
155 mFeedback->setProgress( ( static_cast<double>( bytesSent ) / static_cast<double>( bytesTotal ) ) * 100.0 );
156 }
157}
158
@ File
Parameter is a single file.
Definition qgis.h:4008
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.