add client side handling for mapping rename

This commit is contained in:
Stephen Birarda 2016-03-08 14:43:55 -08:00
parent 793d20306d
commit f9f42a085a
3 changed files with 108 additions and 2 deletions

View file

@ -174,6 +174,46 @@ void DeleteMappingsRequest::doStart() {
});
};
RenameMappingRequest::RenameMappingRequest(const AssetPath& oldPath, const AssetPath& newPath) :
_oldPath(oldPath),
_newPath(newPath)
{
}
void RenameMappingRequest::doStart() {
auto assetClient = DependencyManager::get<AssetClient>();
assetClient->renameAssetMapping(_oldPath, _newPath, [this, assetClient](bool responseReceived,
AssetServerError error,
QSharedPointer<ReceivedMessage> message) {
if (!responseReceived) {
_error = NetworkError;
} else {
switch (error) {
case AssetServerError::NoError:
_error = NoError;
break;
case AssetServerError::PermissionDenied:
_error = PermissionDenied;
break;
default:
_error = UnknownError;
break;
}
}
if (!error) {
// take the hash mapped for the old path from the cache
auto hash = assetClient->_mappingCache.take(_oldPath);
if (!hash.isEmpty()) {
// use the hash mapped for the old path for the new path
assetClient->_mappingCache[_newPath] = hash;
}
}
emit finished(this);
});
}
AssetClient::AssetClient() {
@ -300,6 +340,10 @@ SetMappingRequest* AssetClient::createSetMappingRequest(const AssetPath& path, c
return new SetMappingRequest(path, hash);
}
RenameMappingRequest* AssetClient::createRenameMappingRequest(const AssetPath& oldPath, const AssetPath& newPath) {
return new RenameMappingRequest(oldPath, newPath);
}
AssetRequest* AssetClient::createRequest(const AssetHash& hash) {
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
qCWarning(asset_client) << "Invalid hash size";
@ -573,7 +617,7 @@ bool AssetClient::setAssetMapping(const QString& path, const AssetHash& hash, Ma
packetList->writePrimitive(AssetMappingOperationType::Set);
packetList->writeString(path.toUtf8());
packetList->writeString(path);
packetList->write(QByteArray::fromHex(hash.toUtf8()));
nodeList->sendPacketList(std::move(packetList), *assetServer);
@ -586,6 +630,32 @@ bool AssetClient::setAssetMapping(const QString& path, const AssetHash& hash, Ma
return false;
}
bool AssetClient::renameAssetMapping(const AssetPath& oldPath, const AssetPath& newPath, MappingOperationCallback callback) {
auto nodeList = DependencyManager::get<NodeList>();
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
if (assetServer) {
auto packetList = NLPacketList::create(PacketType::AssetMappingOperation, QByteArray(), true, true);
auto messageID = ++_currentID;
packetList->writePrimitive(messageID);
packetList->writePrimitive(AssetMappingOperationType::Rename);
packetList->writeString(oldPath);
packetList->writeString(newPath);
nodeList->sendPacketList(std::move(packetList), *assetServer);
_pendingMappingRequests[assetServer][messageID] = callback;
return true;
}
return false;
}
bool AssetClient::uploadAsset(const QByteArray& data, UploadResultCallback callback) {
auto nodeList = DependencyManager::get<NodeList>();
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
@ -832,3 +902,19 @@ void AssetScriptingInterface::getAllMappings(QScriptValue callback) {
request->start();
}
void AssetScriptingInterface::renameMapping(QString oldPath, QString newPath, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createRenameMappingRequest(oldPath, newPath);
connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()) };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}

View file

@ -113,6 +113,21 @@ private:
AssetPathList _paths;
};
class RenameMappingRequest : public MappingRequest {
Q_OBJECT
public:
RenameMappingRequest(const AssetPath& oldPath, const AssetPath& newPath);
signals:
void finished(RenameMappingRequest* thisRequest);
private:
virtual void doStart() override;
AssetPath _oldPath;
AssetPath _newPath;
};
class GetAllMappingsRequest : public MappingRequest {
Q_OBJECT
public:
@ -138,6 +153,7 @@ public:
Q_INVOKABLE GetAllMappingsRequest* createGetAllMappingsRequest();
Q_INVOKABLE DeleteMappingsRequest* createDeleteMappingsRequest(const AssetPathList& paths);
Q_INVOKABLE SetMappingRequest* createSetMappingRequest(const AssetPath& path, const AssetHash& hash);
Q_INVOKABLE RenameMappingRequest* createRenameMappingRequest(const AssetPath& oldPath, const AssetPath& newPath);
Q_INVOKABLE AssetRequest* createRequest(const AssetHash& hash);
Q_INVOKABLE AssetUpload* createUpload(const QString& filename);
Q_INVOKABLE AssetUpload* createUpload(const QByteArray& data);
@ -161,6 +177,7 @@ private:
bool getAllAssetMappings(MappingOperationCallback callback);
bool setAssetMapping(const QString& path, const AssetHash& hash, MappingOperationCallback callback);
bool deleteAssetMappings(const AssetPathList& paths, MappingOperationCallback callback);
bool renameAssetMapping(const AssetPath& oldPath, const AssetPath& newPath, MappingOperationCallback callback);
bool getAssetInfo(const QString& hash, GetInfoCallback callback);
bool getAsset(const QString& hash, DataOffset start, DataOffset end,
@ -186,6 +203,7 @@ private:
friend class GetAllMappingsRequest;
friend class SetMappingRequest;
friend class DeleteMappingsRequest;
friend class RenameMappingRequest;
};
@ -200,6 +218,7 @@ public:
Q_INVOKABLE void getMapping(QString path, QScriptValue callback);
Q_INVOKABLE void deleteMappings(QStringList paths, QScriptValue callback);
Q_INVOKABLE void getAllMappings(QScriptValue callback);
Q_INVOKABLE void renameMapping(QString oldPath, QString newPath, QScriptValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
QScriptEngine* _engine;

View file

@ -44,7 +44,8 @@ enum AssetMappingOperationType : uint8_t {
Get = 0,
GetAll,
Set,
Delete
Delete,
Rename
};
QUrl getATPUrl(const QString& hash);