24#include <QCryptographicHash>
30using namespace Qt::StringLiterals;
34QgsHttpExternalStorageStoreTask::QgsHttpExternalStorageStoreTask(
const QUrl &url,
const QString &filePath,
const QString &authCfg )
35 :
QgsTask( tr(
"Storing %1" ).arg( QFileInfo( filePath ).baseName() ) )
37 , mFilePath( filePath )
39 , mFeedback( std::make_unique<
QgsFeedback>( this ) )
42QgsHttpExternalStorageStoreTask::~QgsHttpExternalStorageStoreTask() =
default;
44bool QgsHttpExternalStorageStoreTask::run()
49 QNetworkRequest req( mUrl );
53 if ( !f.open( QIODevice::ReadOnly ) )
56 if ( mPrepareRequestHandler )
57 mPrepareRequestHandler( req, &f );
60 if ( !isCanceled() && bytesTotal > 0 )
62 const int progress = ( bytesReceived * 100 ) / bytesTotal;
63 setProgress( progress );
77void QgsHttpExternalStorageStoreTask::cancel()
83QString QgsHttpExternalStorageStoreTask::errorString()
const
88void QgsHttpExternalStorageStoreTask::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
90 mPrepareRequestHandler = std::move( handler );
93QgsHttpExternalStorageStoredContent::QgsHttpExternalStorageStoredContent(
const QString &filePath,
const QString &url,
const QString &authcfg )
95 QString storageUrl = url;
96 if ( storageUrl.endsWith(
"/" ) )
97 storageUrl.append( QFileInfo( filePath ).fileName() );
99 mUploadTask =
new QgsHttpExternalStorageStoreTask( storageUrl, filePath, authcfg );
109 connect( mUploadTask, &
QgsTask::progressChanged,
this, [
this](
double progress ) { emit progressChanged( progress ); } );
112void QgsHttpExternalStorageStoredContent::store()
119void QgsHttpExternalStorageStoredContent::cancel()
130 mUploadTask->cancel();
133QString QgsHttpExternalStorageStoredContent::url()
const
138void QgsHttpExternalStorageStoredContent::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
140 mUploadTask->setPrepareRequestHandler( std::move( handler ) );
144QgsHttpExternalStorageFetchedContent::QgsHttpExternalStorageFetchedContent(
QgsFetchedContent *fetchedContent )
145 : mFetchedContent( fetchedContent )
150 reportError( errorMsg );
154void QgsHttpExternalStorageFetchedContent::fetch()
156 if ( !mFetchedContent )
160 mFetchedContent->download();
170QString QgsHttpExternalStorageFetchedContent::filePath()
const
172 return mFetchedContent ? mFetchedContent->filePath() : QString();
175void QgsHttpExternalStorageFetchedContent::onFetched()
177 if ( !mFetchedContent )
187void QgsHttpExternalStorageFetchedContent::cancel()
189 mFetchedContent->cancel();
195QString QgsWebDavExternalStorage::type()
const
200QString QgsWebDavExternalStorage::displayName()
const
202 return QObject::tr(
"WebDAV Storage" );
207 return new QgsHttpExternalStorageStoredContent( filePath, url, authcfg );
214 return new QgsHttpExternalStorageFetchedContent( fetchedContent );
220QString QgsAwsS3ExternalStorage::type()
const
225QString QgsAwsS3ExternalStorage::displayName()
const
227 return QObject::tr(
"AWS S3" );
232 auto storedContent = std::make_unique<QgsHttpExternalStorageStoredContent>( filePath, url, authcfg );
233 storedContent->setPrepareRequestHandler( []( QNetworkRequest &request, QFile *f ) {
234 QCryptographicHash payloadCrypto( QCryptographicHash::Sha256 );
235 payloadCrypto.addData( f );
236 QByteArray payloadHash = payloadCrypto.result().toHex();
238 request.setRawHeader( QByteArray(
"X-Amz-Content-SHA256" ), payloadHash );
241 return storedContent.release();
248 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(), const QgsHttpHeaders &headers=QgsHttpHeaders())
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)