24#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 )
36 , mFeedback( std::make_unique<
QgsFeedback>( this ) )
40QgsHttpExternalStorageStoreTask::~QgsHttpExternalStorageStoreTask() =
default;
42bool QgsHttpExternalStorageStoreTask::run()
47 QNetworkRequest req( mUrl );
51 if ( !f.open( QIODevice::ReadOnly ) )
54 if ( mPrepareRequestHandler )
55 mPrepareRequestHandler( req, &f );
59 if ( !isCanceled() && bytesTotal > 0 )
61 const int progress = ( bytesReceived * 100 ) / bytesTotal;
62 setProgress( progress );
76void QgsHttpExternalStorageStoreTask::cancel()
82QString QgsHttpExternalStorageStoreTask::errorString()
const
87void QgsHttpExternalStorageStoreTask::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
89 mPrepareRequestHandler = std::move( handler );
92QgsHttpExternalStorageStoredContent::QgsHttpExternalStorageStoredContent(
const QString &filePath,
const QString &url,
const QString &authcfg )
94 QString storageUrl = url;
95 if ( storageUrl.endsWith(
"/" ) )
96 storageUrl.append( QFileInfo( filePath ).fileName() );
98 mUploadTask =
new QgsHttpExternalStorageStoreTask( storageUrl, filePath, authcfg );
109 reportError( mUploadTask->errorString() );
114 emit progressChanged( progress );
118void QgsHttpExternalStorageStoredContent::store()
125void QgsHttpExternalStorageStoredContent::cancel()
137 mUploadTask->cancel();
140QString QgsHttpExternalStorageStoredContent::url()
const
145void QgsHttpExternalStorageStoredContent::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
147 mUploadTask->setPrepareRequestHandler( std::move( handler ) );
151QgsHttpExternalStorageFetchedContent::QgsHttpExternalStorageFetchedContent(
QgsFetchedContent *fetchedContent )
152 : mFetchedContent( fetchedContent )
158 reportError( errorMsg );
162void QgsHttpExternalStorageFetchedContent::fetch()
164 if ( !mFetchedContent )
168 mFetchedContent->download();
178QString QgsHttpExternalStorageFetchedContent::filePath()
const
180 return mFetchedContent ? mFetchedContent->filePath() : QString();
183void QgsHttpExternalStorageFetchedContent::onFetched()
185 if ( !mFetchedContent )
195void QgsHttpExternalStorageFetchedContent::cancel()
197 mFetchedContent->cancel();
203QString QgsWebDavExternalStorage::type()
const
208QString QgsWebDavExternalStorage::displayName()
const
210 return QObject::tr(
"WebDAV Storage" );
215 return new QgsHttpExternalStorageStoredContent( filePath, url, authcfg );
222 return new QgsHttpExternalStorageFetchedContent( fetchedContent );
228QString QgsAwsS3ExternalStorage::type()
const
233QString QgsAwsS3ExternalStorage::displayName()
const
235 return QObject::tr(
"AWS S3" );
240 auto storedContent = std::make_unique<QgsHttpExternalStorageStoredContent>( filePath, url, authcfg );
241 storedContent->setPrepareRequestHandler( []( QNetworkRequest & request, QFile * f )
243 QCryptographicHash payloadCrypto( QCryptographicHash::Sha256 );
244 payloadCrypto.addData( f );
245 QByteArray payloadHash = payloadCrypto.result().toHex();
247 request.setRawHeader( QByteArray(
"X-Amz-Content-SHA256" ), payloadHash );
250 return storedContent.release();
257 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 uploadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when when data are sent during a request.
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.
@ NoError
No error was encountered.
Abstract base class for QgsExternalStorage fetched content.
Abstract base class for QgsExternalStorage stored content.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Holds information about fetched network content.
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)