diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index fad8ece7bf..33ae631b77 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -136,6 +136,8 @@ void AssetServer::completeSetup() { performMappingMigration(); + cleanupUnmappedFiles(); + nodeList->addNodeTypeToInterestSet(NodeType::Agent); } @@ -189,6 +191,34 @@ void AssetServer::performMappingMigration() { } } +void AssetServer::cleanupUnmappedFiles() { + QRegExp hashFileRegex { "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}" }; + + auto files = _filesDirectory.entryInfoList(QDir::Files); + + // grab the currently mapped hashes + auto mappedHashes = _fileMappings.values(); + + qDebug() << "Performing unmapped asset cleanup."; + + for (const auto& fileInfo : files) { + if (hashFileRegex.exactMatch(fileInfo.fileName())) { + if (mappedHashes.contains(fileInfo.fileName())) { + qDebug() << "\tLeaving" << fileInfo.fileName() << "in asset files directory since it is mapped"; + } else { + // remove the unmapped file + QFile removeableFile { fileInfo.absoluteFilePath() }; + + if (removeableFile.remove()) { + qDebug() << "\tDeleted" << fileInfo.fileName() << "from asset files directory since it is unmapped."; + } else { + qDebug() << "\tAttempt to delete unmapped file" << fileInfo.fileName() << "failed"; + } + } + } + } +} + void AssetServer::handleAssetMappingOperation(QSharedPointer message, SharedNodePointer senderNode) { MessageID messageID; message->readPrimitive(&messageID); diff --git a/assignment-client/src/assets/AssetServer.h b/assignment-client/src/assets/AssetServer.h index 1a8ebed50b..7e7c3c8b69 100644 --- a/assignment-client/src/assets/AssetServer.h +++ b/assignment-client/src/assets/AssetServer.h @@ -62,6 +62,9 @@ private: void performMappingMigration(); + // deletes any unmapped files from the local asset directory + void cleanupUnmappedFiles(); + Mappings _fileMappings; QDir _resourcesDirectory;