put existing AssetUtils functions in namespace per CR feedback

This commit is contained in:
humbletim 2018-01-03 04:01:43 -05:00
parent 8d2e041678
commit a863aec4a0
10 changed files with 30 additions and 28 deletions

View file

@ -21,6 +21,7 @@
#include "AssetUtils.h"
#include "ReceivedMessage.h"
using namespace AssetUtils;
namespace std {
template <>

View file

@ -22,6 +22,8 @@
#include <AssetUtils.h>
using namespace AssetUtils;
class BakeAssetTask : public QObject, public QRunnable {
Q_OBJECT
public:

View file

@ -20,6 +20,7 @@
#include "ClientServerUtils.h"
using namespace AssetUtils;
UploadAssetTask::UploadAssetTask(QSharedPointer<ReceivedMessage> receivedMessage, SharedNodePointer senderNode,
const QDir& resourcesDir, uint64_t filesizeLimit) :
@ -54,7 +55,7 @@ void UploadAssetTask::run() {
} else {
QByteArray fileData = buffer.read(fileSize);
auto hash = hashData(fileData);
auto hash = AssetUtils::hashData(fileData);
auto hexHash = hash.toHex();
qDebug() << "Hash for uploaded file from" << uuidStringWithoutCurlyBraces(_senderNode->getUUID())
@ -66,7 +67,7 @@ void UploadAssetTask::run() {
if (file.exists()) {
// check if the local file has the correct contents, otherwise we overwrite
if (file.open(QIODevice::ReadOnly) && hashData(file.readAll()) == hash) {
if (file.open(QIODevice::ReadOnly) && AssetUtils::hashData(file.readAll()) == hash) {
qDebug() << "Not overwriting existing verified file: " << hexHash;
existingCorrectFile = true;

View file

@ -34,6 +34,8 @@
MessageID AssetClient::_currentID = 0;
using AssetUtils::AssetMappingOperationType;
AssetClient::AssetClient() {
_cacheDir = qApp->property(hifi::properties::APP_LOCAL_DATA_PATH).toString();
setCustomDeleter([](Dependency* dependency){
@ -251,7 +253,7 @@ MessageID AssetClient::getAsset(const QString& hash, DataOffset start, DataOffse
ReceivedAssetCallback callback, ProgressCallback progressCallback) {
Q_ASSERT(QThread::currentThread() == thread());
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
if (hash.length() != AssetUtils::SHA256_HASH_HEX_LENGTH) {
qCWarning(asset_client) << "Invalid hash size";
return false;
}
@ -263,7 +265,7 @@ MessageID AssetClient::getAsset(const QString& hash, DataOffset start, DataOffse
auto messageID = ++_currentID;
auto payloadSize = sizeof(messageID) + SHA256_HASH_LENGTH + sizeof(start) + sizeof(end);
auto payloadSize = sizeof(messageID) + AssetUtils::SHA256_HASH_LENGTH + sizeof(start) + sizeof(end);
auto packet = NLPacket::create(PacketType::AssetGet, payloadSize, true);
qCDebug(asset_client) << "Requesting data from" << start << "to" << end << "of" << hash << "from asset-server.";
@ -295,7 +297,7 @@ MessageID AssetClient::getAssetInfo(const QString& hash, GetInfoCallback callbac
if (assetServer) {
auto messageID = ++_currentID;
auto payloadSize = sizeof(messageID) + SHA256_HASH_LENGTH;
auto payloadSize = sizeof(messageID) + AssetUtils::SHA256_HASH_LENGTH;
auto packet = NLPacket::create(PacketType::AssetGetInfo, payloadSize, true);
packet->writePrimitive(messageID);
@ -317,7 +319,7 @@ void AssetClient::handleAssetGetInfoReply(QSharedPointer<ReceivedMessage> messag
MessageID messageID;
message->readPrimitive(&messageID);
auto assetHash = message->read(SHA256_HASH_LENGTH);
auto assetHash = message->read(AssetUtils::SHA256_HASH_LENGTH);
AssetServerError error;
message->readPrimitive(&error);
@ -351,7 +353,7 @@ void AssetClient::handleAssetGetInfoReply(QSharedPointer<ReceivedMessage> messag
void AssetClient::handleAssetGetReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
Q_ASSERT(QThread::currentThread() == thread());
auto assetHash = message->readHead(SHA256_HASH_LENGTH);
auto assetHash = message->readHead(AssetUtils::SHA256_HASH_LENGTH);
qCDebug(asset_client) << "Got reply for asset: " << assetHash.toHex();
MessageID messageID;
@ -751,7 +753,7 @@ void AssetClient::handleAssetUploadReply(QSharedPointer<ReceivedMessage> message
if (error) {
qCWarning(asset_client) << "Error uploading file to asset server";
} else {
auto hash = message->read(SHA256_HASH_LENGTH);
auto hash = message->read(AssetUtils::SHA256_HASH_LENGTH);
hashString = hash.toHex();
qCDebug(asset_client) << "Successfully uploaded asset to asset-server - SHA256 hash is " << hashString;

View file

@ -42,6 +42,8 @@ struct AssetInfo {
int64_t size;
};
using namespace AssetUtils;
using MappingOperationCallback = std::function<void(bool responseReceived, AssetServerError serverError, QSharedPointer<ReceivedMessage> message)>;
using ReceivedAssetCallback = std::function<void(bool responseReceived, AssetServerError serverError, const QByteArray& data)>;
using GetInfoCallback = std::function<void(bool responseReceived, AssetServerError serverError, AssetInfo info)>;

View file

@ -52,7 +52,7 @@ void AssetRequest::start() {
}
// in case we haven't parsed a valid hash, return an error now
if (!isValidHash(_hash)) {
if (!AssetUtils::isValidHash(_hash)) {
_error = InvalidHash;
_state = Finished;
@ -61,7 +61,7 @@ void AssetRequest::start() {
}
// Try to load from cache
_data = loadFromCache(getUrl());
_data = AssetUtils::loadFromCache(getUrl());
if (!_data.isNull()) {
_error = NoError;
@ -104,7 +104,7 @@ void AssetRequest::start() {
break;
}
} else {
if (!_byteRange.isSet() && hashData(data).toHex() != _hash) {
if (!_byteRange.isSet() && AssetUtils::hashData(data).toHex() != _hash) {
// the hash of the received data does not match what we expect, so we return an error
_error = HashVerificationFailed;
}
@ -115,7 +115,7 @@ void AssetRequest::start() {
emit progress(_totalReceived, data.size());
if (!_byteRange.isSet()) {
saveToCache(getUrl(), data);
AssetUtils::saveToCache(getUrl(), data);
}
}
}

View file

@ -52,7 +52,7 @@ public:
const State& getState() const { return _state; }
const Error& getError() const { return _error; }
const QString getErrorString() const;
QUrl getUrl() const { return ::getATPUrl(_hash); }
QUrl getUrl() const { return AssetUtils::getATPUrl(_hash); }
QString getHash() const { return _hash; }
bool loadedFromCache() const { return _loadedFromCache; }

View file

@ -104,8 +104,8 @@ void AssetUpload::start() {
}
}
if (_error == NoError && hash == hashData(_data).toHex()) {
saveToCache(getATPUrl(hash), _data);
if (_error == NoError && hash == AssetUtils::hashData(_data).toHex()) {
AssetUtils::saveToCache(AssetUtils::getATPUrl(hash), _data);
}
emit finished(this, hash);

View file

@ -23,6 +23,8 @@
#include "ResourceManager.h"
namespace AssetUtils {
// Extract the valid AssetHash portion from atp: URLs like "[atp:]HASH[.fbx][?query]"
// (or an invalid AssetHash if not found)
AssetHash extractAssetHash(const QString& input) {
@ -131,3 +133,5 @@ QString bakingStatusToString(BakingStatus status) {
return "--";
}
}
} // namespace AssetUtils

View file

@ -19,6 +19,8 @@
#include <QtCore/QByteArray>
#include <QtCore/QUrl>
namespace AssetUtils {
using DataOffset = int64_t;
using AssetPath = QString;
@ -85,18 +87,6 @@ bool isValidHash(const QString& hashString);
QString bakingStatusToString(BakingStatus status);
// backwards-compatible namespace aliases
// (allows new code to be explicit -- eg: `AssetUtils::isValidPath(path)` vs. `isValidPath(path)`)
namespace AssetUtils {
static const auto& loadFromCache = ::loadFromCache;
static const auto& saveToCache = ::saveToCache;
static const auto& hashData = ::hashData;
static const auto& getATPUrl = ::getATPUrl;
static const auto& extractAssetHash = ::extractAssetHash;
static const auto& isValidFilePath = ::isValidFilePath;
static const auto& isValidPath = ::isValidPath;
static const auto& isValidHash = ::isValidHash;
};
} // namespace AssetUtils
#endif // hifi_AssetUtils_h