diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9e9830d977..a553eb4e9e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4118,7 +4118,7 @@ void Application::modelUploadFinished(AssetUpload* upload, const QString& hash) EntityItemProperties properties; properties.setType(EntityTypes::Model); - properties.setModelURL(QString("%1:%2.%3").arg(ATP_SCHEME).arg(hash).arg(upload->getExtension())); + properties.setModelURL(QString("%1:%2.%3").arg(URL_SCHEME_ATP).arg(hash).arg(upload->getExtension())); properties.setPosition(_myCamera.getPosition() + _myCamera.getOrientation() * Vectors::FRONT * 2.0f); properties.setName(QUrl(upload->getFilename()).fileName()); diff --git a/interface/src/ui/AssetUploadDialogFactory.cpp b/interface/src/ui/AssetUploadDialogFactory.cpp index 4910e6d604..e169d88c8c 100644 --- a/interface/src/ui/AssetUploadDialogFactory.cpp +++ b/interface/src/ui/AssetUploadDialogFactory.cpp @@ -11,11 +11,6 @@ #include "AssetUploadDialogFactory.h" -#include -#include -#include -#include - #include #include #include @@ -24,6 +19,12 @@ #include #include +#include +#include +#include +#include +#include + AssetUploadDialogFactory& AssetUploadDialogFactory::getInstance() { static AssetUploadDialogFactory staticInstance; return staticInstance; @@ -85,7 +86,7 @@ void AssetUploadDialogFactory::handleUploadFinished(AssetUpload* upload, const Q // setup the line edit to hold the copiable text QLineEdit* lineEdit = new QLineEdit; - QString atpURL = QString("%1:%2.%3").arg(ATP_SCHEME).arg(hash).arg(upload->getExtension()); + QString atpURL = QString("%1:%2.%3").arg(URL_SCHEME_ATP).arg(hash).arg(upload->getExtension()); // set the ATP URL as the text value so it's copiable lineEdit->insert(atpURL); diff --git a/libraries/networking/src/AssetRequest.cpp b/libraries/networking/src/AssetRequest.cpp index b4e3882a5e..a0a3ca5339 100644 --- a/libraries/networking/src/AssetRequest.cpp +++ b/libraries/networking/src/AssetRequest.cpp @@ -14,10 +14,8 @@ #include #include -#include #include "AssetClient.h" -#include "NetworkAccessManager.h" #include "NetworkLogging.h" #include "NodeList.h" #include "ResourceCache.h" @@ -41,7 +39,8 @@ void AssetRequest::start() { } // Try to load from cache - if (loadFromCache()) { + _data = loadFromCache(getUrl()); + if (!_data.isNull()) { _info.hash = _hash; _info.size = _data.size(); _error = NoError; @@ -112,7 +111,7 @@ void AssetRequest::start() { _totalReceived += data.size(); emit progress(_totalReceived, _info.size); - if (saveToCache(data)) { + if (saveToCache(getUrl(), data)) { qCDebug(asset_client) << getUrl().toDisplayString() << "saved to disk cache"; } } else { @@ -133,49 +132,6 @@ void AssetRequest::start() { } QUrl AssetRequest::getUrl() const { - if (!_extension.isEmpty()) { - return QUrl(QString("%1:%2.%3").arg(URL_SCHEME_ATP, _hash, _extension)); - } else { - return QUrl(QString("%1:%2").arg(URL_SCHEME_ATP, _hash)); - } -} - -bool AssetRequest::loadFromCache() { - if (auto cache = NetworkAccessManager::getInstance().cache()) { - auto url = getUrl(); - if (auto ioDevice = cache->data(url)) { - _data = ioDevice->readAll(); - return true; - } else { - qCDebug(asset_client) << url.toDisplayString() << "not in disk cache"; - } - } else { - qCWarning(asset_client) << "No disk cache to load assets from."; - } - return false; -} - -bool AssetRequest::saveToCache(const QByteArray& file) const { - if (auto cache = NetworkAccessManager::getInstance().cache()) { - auto url = getUrl(); - - if (!cache->metaData(url).isValid()) { - QNetworkCacheMetaData metaData; - metaData.setUrl(url); - metaData.setSaveToDisk(true); - metaData.setLastModified(QDateTime::currentDateTime()); - metaData.setExpirationDate(QDateTime()); // Never expires - - if (auto ioDevice = cache->prepare(metaData)) { - ioDevice->write(file); - cache->insert(ioDevice); - return true; - } - qCWarning(asset_client) << "Could not save" << url.toDisplayString() << "to disk cache."; - } - } else { - qCWarning(asset_client) << "No disk cache to save assets to."; - } - return false; + return ::getUrl(_hash, _extension); } diff --git a/libraries/networking/src/AssetRequest.h b/libraries/networking/src/AssetRequest.h index 75e2353425..a5275e718a 100644 --- a/libraries/networking/src/AssetRequest.h +++ b/libraries/networking/src/AssetRequest.h @@ -53,9 +53,6 @@ signals: void progress(qint64 totalReceived, qint64 total); private: - bool loadFromCache(); - bool saveToCache(const QByteArray& file) const; - State _state = NotStarted; Error _error = NoError; AssetInfo _info; diff --git a/libraries/networking/src/AssetUtils.cpp b/libraries/networking/src/AssetUtils.cpp new file mode 100644 index 0000000000..0cf45c3a4f --- /dev/null +++ b/libraries/networking/src/AssetUtils.cpp @@ -0,0 +1,67 @@ +// +// AssetUtils.h +// libraries/networking/src +// +// Created by Clément Brisset on 10/12/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "AssetUtils.h" + +#include +#include + +#include "NetworkAccessManager.h" +#include "NetworkLogging.h" + +#include "ResourceManager.h" + +QUrl getUrl(const QString& hash, const QString& extension) { + if (!extension.isEmpty()) { + return QUrl(QString("%1:%2.%3").arg(URL_SCHEME_ATP, hash, extension)); + } else { + return QUrl(QString("%1:%2").arg(URL_SCHEME_ATP, hash)); + } +} + +QByteArray hashData(const QByteArray& data) { + return QCryptographicHash::hash(data, QCryptographicHash::Sha256); +} + +QByteArray loadFromCache(const QUrl& url) { + if (auto cache = NetworkAccessManager::getInstance().cache()) { + if (auto ioDevice = cache->data(url)) { + return ioDevice->readAll(); + } else { + qCDebug(asset_client) << url.toDisplayString() << "not in disk cache"; + } + } else { + qCWarning(asset_client) << "No disk cache to load assets from."; + } + return QByteArray(); +} + +bool saveToCache(const QUrl& url, const QByteArray& file) { + if (auto cache = NetworkAccessManager::getInstance().cache()) { + if (!cache->metaData(url).isValid()) { + QNetworkCacheMetaData metaData; + metaData.setUrl(url); + metaData.setSaveToDisk(true); + metaData.setLastModified(QDateTime::currentDateTime()); + metaData.setExpirationDate(QDateTime()); // Never expires + + if (auto ioDevice = cache->prepare(metaData)) { + ioDevice->write(file); + cache->insert(ioDevice); + return true; + } + qCWarning(asset_client) << "Could not save" << url.toDisplayString() << "to disk cache."; + } + } else { + qCWarning(asset_client) << "No disk cache to save assets to."; + } + return false; +} \ No newline at end of file diff --git a/libraries/networking/src/AssetUtils.h b/libraries/networking/src/AssetUtils.h index b18ff329fc..5fd5c9144d 100644 --- a/libraries/networking/src/AssetUtils.h +++ b/libraries/networking/src/AssetUtils.h @@ -12,10 +12,11 @@ #ifndef hifi_AssetUtils_h #define hifi_AssetUtils_h -#include - #include +#include +#include + using MessageID = uint32_t; using DataOffset = int64_t; @@ -31,8 +32,11 @@ enum AssetServerError : uint8_t { PermissionDenied }; -const QString ATP_SCHEME = "atp"; +QUrl getUrl(const QString& hash, const QString& extension = QString()); -inline QByteArray hashData(const QByteArray& data) { return QCryptographicHash::hash(data, QCryptographicHash::Sha256); } +QByteArray hashData(const QByteArray& data); + +QByteArray loadFromCache(const QUrl& url); +bool saveToCache(const QUrl& url, const QByteArray& file); #endif