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 ) )
43QgsHttpExternalStorageStoreTask::~QgsHttpExternalStorageStoreTask() =
default;
45bool QgsHttpExternalStorageStoreTask::run()
50 QNetworkRequest req( mUrl );
54 if ( !f.open( QIODevice::ReadOnly ) )
57 if ( mPrepareRequestHandler )
58 mPrepareRequestHandler( req, &f );
62 if ( !isCanceled() && bytesTotal > 0 )
64 const int progress = ( bytesReceived * 100 ) / bytesTotal;
65 setProgress( progress );
79void QgsHttpExternalStorageStoreTask::cancel()
85QString QgsHttpExternalStorageStoreTask::errorString()
const
90void QgsHttpExternalStorageStoreTask::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
92 mPrepareRequestHandler = std::move( handler );
95QgsHttpExternalStorageStoredContent::QgsHttpExternalStorageStoredContent(
const QString &filePath,
const QString &url,
const QString &authcfg )
97 QString storageUrl = url;
98 if ( storageUrl.endsWith(
"/" ) )
99 storageUrl.append( QFileInfo( filePath ).fileName() );
101 mUploadTask =
new QgsHttpExternalStorageStoreTask( storageUrl, filePath, authcfg );
112 reportError( mUploadTask->errorString() );
117 emit progressChanged( progress );
121void QgsHttpExternalStorageStoredContent::store()
128void QgsHttpExternalStorageStoredContent::cancel()
140 mUploadTask->cancel();
143QString QgsHttpExternalStorageStoredContent::url()
const
148void QgsHttpExternalStorageStoredContent::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
150 mUploadTask->setPrepareRequestHandler( std::move( handler ) );
154QgsHttpExternalStorageFetchedContent::QgsHttpExternalStorageFetchedContent(
QgsFetchedContent *fetchedContent )
155 : mFetchedContent( fetchedContent )
161 reportError( errorMsg );
165void QgsHttpExternalStorageFetchedContent::fetch()
167 if ( !mFetchedContent )
171 mFetchedContent->download();
181QString QgsHttpExternalStorageFetchedContent::filePath()
const
183 return mFetchedContent ? mFetchedContent->filePath() : QString();
186void QgsHttpExternalStorageFetchedContent::onFetched()
188 if ( !mFetchedContent )
198void QgsHttpExternalStorageFetchedContent::cancel()
200 mFetchedContent->cancel();
206QString QgsWebDavExternalStorage::type()
const
211QString QgsWebDavExternalStorage::displayName()
const
213 return QObject::tr(
"WebDAV Storage" );
218 return new QgsHttpExternalStorageStoredContent( filePath, url, authcfg );
225 return new QgsHttpExternalStorageFetchedContent( fetchedContent );
231QString QgsAwsS3ExternalStorage::type()
const
236QString QgsAwsS3ExternalStorage::displayName()
const
238 return QObject::tr(
"AWS S3" );
243 auto storedContent = std::make_unique<QgsHttpExternalStorageStoredContent>( filePath, url, authcfg );
244 storedContent->setPrepareRequestHandler( []( QNetworkRequest & request, QFile * f )
246 QCryptographicHash payloadCrypto( QCryptographicHash::Sha256 );
247 payloadCrypto.addData( f );
248 QByteArray payloadHash = payloadCrypto.result().toHex();
250 request.setRawHeader( QByteArray(
"X-Amz-Content-SHA256" ), payloadHash );
253 return storedContent.release();
260 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)