27#include <QCryptographicHash>
31QgsHttpExternalStorageStoreTask::QgsHttpExternalStorageStoreTask(
const QUrl &url,
const QString &filePath,
const QString &authCfg )
32 :
QgsTask( tr(
"Storing %1" ).arg( QFileInfo( filePath ).baseName() ) )
34 , mFilePath( filePath )
40bool QgsHttpExternalStorageStoreTask::run()
45 QNetworkRequest req( mUrl );
48 QFile *f =
new QFile( mFilePath );
49 f->open( QIODevice::ReadOnly );
51 if ( mPrepareRequestHandler )
52 mPrepareRequestHandler( req, f );
56 if ( !isCanceled() && bytesTotal > 0 )
58 const int progress = ( bytesReceived * 100 ) / bytesTotal;
59 setProgress( progress );
73void QgsHttpExternalStorageStoreTask::cancel()
79QString QgsHttpExternalStorageStoreTask::errorString()
const
84void QgsHttpExternalStorageStoreTask::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
86 mPrepareRequestHandler = handler;
89QgsHttpExternalStorageStoredContent::QgsHttpExternalStorageStoredContent(
const QString &filePath,
const QString &url,
const QString &authcfg )
91 QString storageUrl = url;
92 if ( storageUrl.endsWith(
"/" ) )
93 storageUrl.append( QFileInfo( filePath ).fileName() );
95 mUploadTask =
new QgsHttpExternalStorageStoreTask( storageUrl, filePath, authcfg );
106 reportError( mUploadTask->errorString() );
111 emit progressChanged( progress );
115void QgsHttpExternalStorageStoredContent::store()
122void QgsHttpExternalStorageStoredContent::cancel()
134 mUploadTask->cancel();
137QString QgsHttpExternalStorageStoredContent::url()
const
142void QgsHttpExternalStorageStoredContent::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
144 mUploadTask->setPrepareRequestHandler( handler );
148QgsHttpExternalStorageFetchedContent::QgsHttpExternalStorageFetchedContent(
QgsFetchedContent *fetchedContent )
149 : mFetchedContent( fetchedContent )
155 reportError( errorMsg );
159void QgsHttpExternalStorageFetchedContent::fetch()
161 if ( !mFetchedContent )
165 mFetchedContent->download();
175QString QgsHttpExternalStorageFetchedContent::filePath()
const
177 return mFetchedContent ? mFetchedContent->filePath() : QString();
180void QgsHttpExternalStorageFetchedContent::onFetched()
182 if ( !mFetchedContent )
192void QgsHttpExternalStorageFetchedContent::cancel()
194 mFetchedContent->cancel();
200QString QgsWebDavExternalStorage::type()
const
202 return QStringLiteral(
"WebDAV" );
205QString QgsWebDavExternalStorage::displayName()
const
207 return QObject::tr(
"WebDAV Storage" );
212 return new QgsHttpExternalStorageStoredContent( filePath, url, authcfg );
219 return new QgsHttpExternalStorageFetchedContent( fetchedContent );
225QString QgsAwsS3ExternalStorage::type()
const
227 return QStringLiteral(
"AWSS3" );
230QString QgsAwsS3ExternalStorage::displayName()
const
232 return QObject::tr(
"AWS S3" );
237 std::unique_ptr<QgsHttpExternalStorageStoredContent> storedContent = std::make_unique<QgsHttpExternalStorageStoredContent>( filePath, url, authcfg );
238 storedContent->setPrepareRequestHandler( []( QNetworkRequest & request, QFile * f )
240 QCryptographicHash payloadCrypto( QCryptographicHash::Sha256 );
241 payloadCrypto.addData( f );
242 QByteArray payloadHash = payloadCrypto.result().toHex();
244 request.setRawHeader( QByteArray(
"X-Amz-Content-SHA256" ), payloadHash );
247 return storedContent.release();
254 return new QgsHttpExternalStorageFetchedContent( fetchedContent );
@ Canceled
Content fetching/storing has been canceled.
@ Running
Content fetching/storing is in progress.
@ Finished
Content fetching/storing is finished and successful.
@ Deferred
Do not start immediately the action.
static QgsNetworkContentFetcherRegistry * networkContentFetcherRegistry()
Returns the application's network content registry used for fetching temporary files during QGIS sess...
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode put(QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback=nullptr)
Performs a "put" operation on the specified request, using the given data.
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
void uploadProgress(qint64, qint64)
Emitted when when data are sent during a request.
@ NoError
No error was encountered.
Class for QgsExternalStorage fetched content.
Class for QgsExternalStorage stored content.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
FetchedContent holds useful information about a network content being fetched.
void errorOccurred(QNetworkReply::NetworkError code, const QString &errorMsg)
Emitted when an error with code error occurred while processing the request errorMsg is a textual des...
@ Finished
Download finished and successful.
void fetched()
Emitted when the file is fetched and accessible.
QgsFetchedContent * fetch(const QString &url, Qgis::ActionStart fetchingMode=Qgis::ActionStart::Deferred, const QString &authConfig=QString())
Initialize a download for the given URL.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
Abstract base class for long running background tasks.
void taskCompleted()
Will be emitted by task to indicate its successful completion.
void progressChanged(double progress)
Will be emitted by task when its progress changes.
virtual void cancel()
Notifies the task that it should terminate.
void taskTerminated()
Will be emitted by task if it has terminated for any reason other then completion (e....
#define QgsSetRequestInitiatorClass(request, _class)