From 7c0b3953e4d47db6de434aaf3f423183148f68b5 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 11 Feb 2016 15:34:43 -0800 Subject: [PATCH] handle custom path in asset-server for assets --- assignment-client/src/assets/AssetServer.cpp | 50 +++++++++++++++++--- assignment-client/src/assets/AssetServer.h | 2 + 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 8ab53d3b87..443fbff3b1 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -45,25 +45,61 @@ AssetServer::AssetServer(ReceivedMessage& message) : } void AssetServer::run() { + + qDebug() << "Waiting for connection to domain to request settings from domain-server."; + + // wait until we have the domain-server settings, otherwise we bail + DomainHandler& domainHandler = DependencyManager::get()->getDomainHandler(); + connect(&domainHandler, &DomainHandler::settingsReceived, this, &AssetServer::completeSetup); + connect(&domainHandler, &DomainHandler::settingsReceiveFail, this, &AssetServer::domainSettingsRequestFailed); + ThreadedAssignment::commonInit(ASSET_SERVER_LOGGING_TARGET_NAME, NodeType::AssetServer); +} +void AssetServer::completeSetup() { auto nodeList = DependencyManager::get(); - nodeList->addNodeTypeToInterestSet(NodeType::Agent); - const QString RESOURCES_PATH = "assets"; + auto& domainHandler = nodeList->getDomainHandler(); + const QJsonObject& settingsObject = domainHandler.getSettingsObject(); - _resourcesDirectory = QDir(ServerPathUtils::getDataDirectory()).filePath(RESOURCES_PATH); + static const QString ASSET_SERVER_SETTINGS_KEY = "asset_server"; + auto assetServerObject = settingsObject[ASSET_SERVER_SETTINGS_KEY]; + + // get the path to the asset folder from the domain server settings + static const QString ASSETS_PATH_OPTION = "assets_path"; + auto assetsJSONValue = assetServerObject.toObject()[ASSETS_PATH_OPTION]; + + if (assetsJSONValue.type() != QJsonValue::String) { + qCritical() << "Received an assets path from the domain-server that could not be parsed. Stopping assignment."; + setFinished(true); + return; + } + + auto assetsPathString = assetsJSONValue.toString(); + QDir assetsPath { assetsPathString }; + QString absoluteFilePath = assetsPath.absolutePath(); + + if (assetsPath.isRelative()) { + // if the domain settings passed us a relative path, make an absolute path that is relative to the + // default data directory + absoluteFilePath = ServerPathUtils::getDataFilePath(assetsPathString); + } + + _resourcesDirectory = QDir(absoluteFilePath); qDebug() << "Creating resources directory"; _resourcesDirectory.mkpath("."); bool noExistingAssets = !_resourcesDirectory.exists() \ - || _resourcesDirectory.entryList(QDir::Files).size() == 0; + || _resourcesDirectory.entryList(QDir::Files).size() == 0; if (noExistingAssets) { qDebug() << "Asset resources directory not found, searching for existing asset resources"; QString oldDataDirectory = QCoreApplication::applicationDirPath(); - auto oldResourcesDirectory = QDir(oldDataDirectory).filePath("resources/" + RESOURCES_PATH); + + const QString OLD_RESOURCES_PATH = "assets"; + + auto oldResourcesDirectory = QDir(oldDataDirectory).filePath("resources/" + OLD_RESOURCES_PATH); if (QDir(oldResourcesDirectory).exists()) { @@ -111,10 +147,12 @@ void AssetServer::run() { auto hexHash = hash.toHex(); qDebug() << "\tMoving " << filename << " to " << hexHash; - + file.rename(_resourcesDirectory.absoluteFilePath(hexHash) + "." + fileInfo.suffix()); } } + + nodeList->addNodeTypeToInterestSet(NodeType::Agent); } void AssetServer::handleAssetGetInfo(QSharedPointer message, SharedNodePointer senderNode) { diff --git a/assignment-client/src/assets/AssetServer.h b/assignment-client/src/assets/AssetServer.h index fe83ce92a6..7d6e26af08 100644 --- a/assignment-client/src/assets/AssetServer.h +++ b/assignment-client/src/assets/AssetServer.h @@ -29,6 +29,8 @@ public slots: void run(); private slots: + void completeSetup(); + void handleAssetGetInfo(QSharedPointer packet, SharedNodePointer senderNode); void handleAssetGet(QSharedPointer packet, SharedNodePointer senderNode); void handleAssetUpload(QSharedPointer packetList, SharedNodePointer senderNode);