Cache on upload

This commit is contained in:
Atlante45 2015-10-12 14:51:28 -07:00
parent 51acf07c15
commit 4abac35c72
2 changed files with 14 additions and 5 deletions

View file

@ -23,9 +23,9 @@ AssetUpload::AssetUpload(QObject* object, const QString& filename) :
} }
void AssetUpload::start() { void AssetUpload::start(bool cacheOnSuccess) {
if (QThread::currentThread() != thread()) { if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "start", Qt::AutoConnection); QMetaObject::invokeMethod(this, "start", Q_ARG(bool, cacheOnSuccess));
return; return;
} }
@ -37,14 +37,16 @@ void AssetUpload::start() {
// file opened, read the data and grab the extension // file opened, read the data and grab the extension
_extension = QFileInfo(_filename).suffix(); _extension = QFileInfo(_filename).suffix();
auto data = file.readAll(); _data = file.readAll();
// ask the AssetClient to upload the asset and emit the proper signals from the passed callback // ask the AssetClient to upload the asset and emit the proper signals from the passed callback
auto assetClient = DependencyManager::get<AssetClient>(); auto assetClient = DependencyManager::get<AssetClient>();
qCDebug(asset_client) << "Attempting to upload" << _filename << "to asset-server."; qCDebug(asset_client) << "Attempting to upload" << _filename << "to asset-server.";
assetClient->uploadAsset(data, _extension, [this](bool responseReceived, AssetServerError error, const QString& hash){ assetClient->uploadAsset(_data, _extension,
[this, cacheOnSuccess](bool responseReceived, AssetServerError error,
const QString& hash){
if (!responseReceived) { if (!responseReceived) {
_error = NetworkError; _error = NetworkError;
} else { } else {
@ -63,6 +65,12 @@ void AssetUpload::start() {
break; break;
} }
} }
if (cacheOnSuccess && _error == NoError &&
hash == hashData(_data).toHex()) {
saveToCache(getUrl(hash, _extension), _data);
}
emit finished(this, hash); emit finished(this, hash);
}); });
} else { } else {

View file

@ -37,7 +37,7 @@ public:
AssetUpload(QObject* parent, const QString& filename); AssetUpload(QObject* parent, const QString& filename);
Q_INVOKABLE void start(); Q_INVOKABLE void start(bool cacheOnSuccess = true);
const QString& getFilename() const { return _filename; } const QString& getFilename() const { return _filename; }
const QString& getExtension() const { return _extension; } const QString& getExtension() const { return _extension; }
@ -50,6 +50,7 @@ signals:
private: private:
QString _filename; QString _filename;
QString _extension; QString _extension;
QByteArray _data;
Error _error; Error _error;
}; };