From 19b00e1364a1c3fda9db2255c4da09ca43207f04 Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Tue, 10 Oct 2017 14:16:21 -0400 Subject: [PATCH] Add option to limit assets filesize in settings --- assignment-client/src/assets/AssetServer.cpp | 16 +++++++++++++--- assignment-client/src/assets/AssetServer.h | 2 ++ assignment-client/src/assets/UploadAssetTask.cpp | 7 ++++--- assignment-client/src/assets/UploadAssetTask.h | 4 +++- domain-server/resources/describe-settings.json | 8 ++++++++ 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index c03721d097..4eb9e44bf4 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -228,7 +228,8 @@ void updateConsumedCores() { AssetServer::AssetServer(ReceivedMessage& message) : ThreadedAssignment(message), _transferTaskPool(this), - _bakingTaskPool(this) + _bakingTaskPool(this), + _filesizeLimit(MAX_UPLOAD_SIZE) { // store the current state of image compression so we can reset it when this assignment is complete _wasColorTextureCompressionEnabled = image::isColorTexturesCompressionEnabled(); @@ -336,8 +337,8 @@ void AssetServer::completeSetup() { auto maxBandwidthValue = assetServerObject[MAX_BANDWIDTH_OPTION]; auto maxBandwidthFloat = maxBandwidthValue.toDouble(-1); + const int BITS_PER_MEGABITS = 1000 * 1000; if (maxBandwidthFloat > 0.0) { - const int BITS_PER_MEGABITS = 1000 * 1000; int maxBandwidth = maxBandwidthFloat * BITS_PER_MEGABITS; nodeList->setConnectionMaxBandwidth(maxBandwidth); qCInfo(asset_server) << "Set maximum bandwith per connection to" << maxBandwidthFloat << "Mb/s." @@ -399,6 +400,15 @@ void AssetServer::completeSetup() { qCCritical(asset_server) << "Asset Server assignment will not continue because mapping file could not be loaded."; setFinished(true); } + + // get file size limit for an asset + static const QString ASSETS_FILESIZE_LIMIT_OPTION = "assets_filesize_limit"; + auto assetsFilesizeLimitJSONValue = assetServerObject[ASSETS_FILESIZE_LIMIT_OPTION]; + auto assetsFilesizeLimit = assetsFilesizeLimitJSONValue.toInt(MAX_UPLOAD_SIZE); + + if (assetsFilesizeLimit != 0 && assetsFilesizeLimit < MAX_UPLOAD_SIZE) { + _filesizeLimit = assetsFilesizeLimit * BITS_PER_MEGABITS; + } } void AssetServer::cleanupUnmappedFiles() { @@ -721,7 +731,7 @@ void AssetServer::handleAssetUpload(QSharedPointer message, Sha if (senderNode->getCanWriteToAssetServer()) { qCDebug(asset_server) << "Starting an UploadAssetTask for upload from" << uuidStringWithoutCurlyBraces(senderNode->getUUID()); - auto task = new UploadAssetTask(message, senderNode, _filesDirectory); + auto task = new UploadAssetTask(message, senderNode, _filesDirectory, _filesizeLimit); _transferTaskPool.start(task); } else { // this is a node the domain told us is not allowed to rez entities diff --git a/assignment-client/src/assets/AssetServer.h b/assignment-client/src/assets/AssetServer.h index aeb40a416f..e6393e6a98 100644 --- a/assignment-client/src/assets/AssetServer.h +++ b/assignment-client/src/assets/AssetServer.h @@ -127,6 +127,8 @@ private: bool _wasGrayscaleTextureCompressionEnabled { false }; bool _wasNormalTextureCompressionEnabled { false }; bool _wasCubeTextureCompressionEnabled { false }; + + uint64_t _filesizeLimit; }; #endif diff --git a/assignment-client/src/assets/UploadAssetTask.cpp b/assignment-client/src/assets/UploadAssetTask.cpp index 7e8e94c34d..5e6d59d032 100644 --- a/assignment-client/src/assets/UploadAssetTask.cpp +++ b/assignment-client/src/assets/UploadAssetTask.cpp @@ -22,10 +22,11 @@ UploadAssetTask::UploadAssetTask(QSharedPointer receivedMessage, SharedNodePointer senderNode, - const QDir& resourcesDir) : + const QDir& resourcesDir, uint64_t filesizeLimit) : _receivedMessage(receivedMessage), _senderNode(senderNode), - _resourcesDir(resourcesDir) + _resourcesDir(resourcesDir), + _filesizeLimit(filesizeLimit) { } @@ -48,7 +49,7 @@ void UploadAssetTask::run() { auto replyPacket = NLPacket::create(PacketType::AssetUploadReply, -1, true); replyPacket->writePrimitive(messageID); - if (fileSize > MAX_UPLOAD_SIZE) { + if (fileSize > _filesizeLimit) { replyPacket->writePrimitive(AssetServerError::AssetTooLarge); } else { QByteArray fileData = buffer.read(fileSize); diff --git a/assignment-client/src/assets/UploadAssetTask.h b/assignment-client/src/assets/UploadAssetTask.h index 700eecbf9a..8c9e0d234a 100644 --- a/assignment-client/src/assets/UploadAssetTask.h +++ b/assignment-client/src/assets/UploadAssetTask.h @@ -26,7 +26,8 @@ class Node; class UploadAssetTask : public QRunnable { public: - UploadAssetTask(QSharedPointer message, QSharedPointer senderNode, const QDir& resourcesDir); + UploadAssetTask(QSharedPointer message, QSharedPointer senderNode, + const QDir& resourcesDir, uint64_t filesizeLimit); void run() override; @@ -34,6 +35,7 @@ private: QSharedPointer _receivedMessage; QSharedPointer _senderNode; QDir _resourcesDir; + uint64_t _filesizeLimit; }; #endif // hifi_UploadAssetTask_h diff --git a/domain-server/resources/describe-settings.json b/domain-server/resources/describe-settings.json index 8d0e949ff3..19f1718370 100644 --- a/domain-server/resources/describe-settings.json +++ b/domain-server/resources/describe-settings.json @@ -858,6 +858,14 @@ "help": "The path to the directory assets are stored in.
If this path is relative, it will be relative to the application data directory.
If you change this path you will need to manually copy any existing assets from the previous directory.", "default": "", "advanced": true + }, + { + "name": "assets_filesize_limit", + "type": "int", + "label": "File Size Limit", + "help": "The file size limit of an asset that can be imported into the asset server in MBytes. 0 (default) means no limit on file size.", + "default": 0, + "advanced": true } ] },