mirror of
https://github.com/overte-org/overte.git
synced 2025-04-23 18:13:29 +02:00
Factor couple methods in AssetUtils
This commit is contained in:
parent
ae32b0dc98
commit
51acf07c15
6 changed files with 87 additions and 62 deletions
|
@ -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());
|
||||
|
||||
|
|
|
@ -11,11 +11,6 @@
|
|||
|
||||
#include "AssetUploadDialogFactory.h"
|
||||
|
||||
#include <AssetClient.h>
|
||||
#include <AssetUpload.h>
|
||||
#include <AssetUtils.h>
|
||||
#include <NodeList.h>
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
#include <QtWidgets/QDialogButtonBox>
|
||||
#include <QtWidgets/QFileDialog>
|
||||
|
@ -24,6 +19,12 @@
|
|||
#include <QtWidgets/QLineEdit>
|
||||
#include <QtWidgets/QVBoxLayout>
|
||||
|
||||
#include <AssetClient.h>
|
||||
#include <AssetUpload.h>
|
||||
#include <AssetUtils.h>
|
||||
#include <NodeList.h>
|
||||
#include <ResourceManager.h>
|
||||
|
||||
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);
|
||||
|
|
|
@ -14,10 +14,8 @@
|
|||
#include <algorithm>
|
||||
|
||||
#include <QtCore/QThread>
|
||||
#include <QtNetwork/QAbstractNetworkCache>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
67
libraries/networking/src/AssetUtils.cpp
Normal file
67
libraries/networking/src/AssetUtils.cpp
Normal file
|
@ -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 <QtCore/QCryptographicHash>
|
||||
#include <QtNetwork/QAbstractNetworkCache>
|
||||
|
||||
#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;
|
||||
}
|
|
@ -12,10 +12,11 @@
|
|||
#ifndef hifi_AssetUtils_h
|
||||
#define hifi_AssetUtils_h
|
||||
|
||||
#include <QtCore/QCryptographicHash>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <QtCore/QByteArray>
|
||||
#include <QtCore/QUrl>
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue