28#include <QCryptographicHash>
32QgsHttpExternalStorageStoreTask::QgsHttpExternalStorageStoreTask(
const QUrl &url,
const QString &filePath,
const QString &authCfg )
33 :
QgsTask( tr(
"Storing %1" ).arg( QFileInfo( filePath ).baseName() ) )
35 , mFilePath( filePath )
41bool QgsHttpExternalStorageStoreTask::run()
46 QNetworkRequest req( mUrl );
49 QFile *f =
new QFile( mFilePath );
50 f->open( QIODevice::ReadOnly );
52 if ( mPrepareRequestHandler )
53 mPrepareRequestHandler( req, f );
57 if ( !isCanceled() && bytesTotal > 0 )
59 const int progress = ( bytesReceived * 100 ) / bytesTotal;
60 setProgress( progress );
74void QgsHttpExternalStorageStoreTask::cancel()
80QString QgsHttpExternalStorageStoreTask::errorString()
const
85void QgsHttpExternalStorageStoreTask::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
87 mPrepareRequestHandler = handler;
90QgsHttpExternalStorageStoredContent::QgsHttpExternalStorageStoredContent(
const QString &filePath,
const QString &url,
const QString &authcfg )
92 QString storageUrl = url;
93 if ( storageUrl.endsWith(
"/" ) )
94 storageUrl.append( QFileInfo( filePath ).fileName() );
96 mUploadTask =
new QgsHttpExternalStorageStoreTask( storageUrl, filePath, authcfg );
107 reportError( mUploadTask->errorString() );
112 emit progressChanged( progress );
116void QgsHttpExternalStorageStoredContent::store()
123void QgsHttpExternalStorageStoredContent::cancel()
135 mUploadTask->cancel();
138QString QgsHttpExternalStorageStoredContent::url()
const
143void QgsHttpExternalStorageStoredContent::setPrepareRequestHandler( std::function<
void( QNetworkRequest &request, QFile *f ) > handler )
145 mUploadTask->setPrepareRequestHandler( handler );
149QgsHttpExternalStorageFetchedContent::QgsHttpExternalStorageFetchedContent(
QgsFetchedContent *fetchedContent )
150 : mFetchedContent( fetchedContent )
156 reportError( errorMsg );
160void QgsHttpExternalStorageFetchedContent::fetch()
162 if ( !mFetchedContent )
166 mFetchedContent->download();
176QString QgsHttpExternalStorageFetchedContent::filePath()
const
178 return mFetchedContent ? mFetchedContent->filePath() : QString();
181void QgsHttpExternalStorageFetchedContent::onFetched()
183 if ( !mFetchedContent )
193void QgsHttpExternalStorageFetchedContent::cancel()
195 mFetchedContent->cancel();
201QString QgsWebDavExternalStorage::type()
const
203 return QStringLiteral(
"WebDAV" );
206QString QgsWebDavExternalStorage::displayName()
const
208 return QObject::tr(
"WebDAV Storage" );
213 return new QgsHttpExternalStorageStoredContent( filePath, url, authcfg );
220 return new QgsHttpExternalStorageFetchedContent( fetchedContent );
226QString QgsAwsS3ExternalStorage::type()
const
228 return QStringLiteral(
"AWSS3" );
231QString QgsAwsS3ExternalStorage::displayName()
const
233 return QObject::tr(
"AWS S3" );
238 std::unique_ptr<QgsHttpExternalStorageStoredContent> storedContent = std::make_unique<QgsHttpExternalStorageStoredContent>( filePath, url, authcfg );
239 storedContent->setPrepareRequestHandler( []( QNetworkRequest & request, QFile * f )
241 QCryptographicHash payloadCrypto( QCryptographicHash::Sha256 );
242 payloadCrypto.addData( f );
243 QByteArray payloadHash = payloadCrypto.result().toHex();
245 request.setRawHeader( QByteArray(
"X-Amz-Content-SHA256" ), payloadHash );
248 return storedContent.release();
255 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.
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)