Showing the changes to libraries/script-engine/src/AssetScriptingInterface.cpp (1 of 5 files). Show all
Unified · Split
| @@ -0,0 +1,172 @@ | ||
| 1 | +// | |
| 2 | +// AssetScriptingInterface.cpp | |
| 3 | +// libraries/script-engine/src | |
| 4 | +// | |
| 5 | +// Created by Stephen Birarda on 2016-03-08. | |
| 6 | +// Copyright 2016 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include "AssetScriptingInterface.h" | |
| 13 | + | |
| 14 | +#include <QtScript/QScriptEngine> | |
| 15 | + | |
| 16 | +#include <AssetRequest.h> | |
| 17 | +#include <AssetUpload.h> | |
| 18 | +#include <NetworkLogging.h> | |
| 19 | + | |
| 20 | +AssetScriptingInterface::AssetScriptingInterface(QScriptEngine* engine) : | |
| 21 | + _engine(engine) | |
| 22 | +{ | |
| 23 | + | |
| 24 | +} | |
| 25 | + | |
| 26 | +void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { | |
| 27 | + QByteArray dataByteArray = data.toUtf8(); | |
| 28 | + auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray); | |
| 29 | + if (!upload) { | |
| 30 | + qCWarning(asset_client) << "Error uploading file to asset server"; | |
| 31 | + return; | |
| 32 | + } | |
| 33 | + | |
| 34 | + QObject::connect(upload, &AssetUpload::finished, this, [this, callback](AssetUpload* upload, const QString& hash) mutable { | |
| 35 | + if (callback.isFunction()) { | |
| 36 | + QString url = "atp://" + hash; | |
| 37 | + QScriptValueList args { url }; | |
| 38 | + callback.call(_engine->currentContext()->thisObject(), args); | |
| 39 | + } | |
| 40 | + }); | |
| 41 | + upload->start(); | |
| 42 | +} | |
| 43 | + | |
| 44 | +void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callback) { | |
| 45 | + const QString ATP_SCHEME { "atp://" }; | |
| 46 | + | |
| 47 | + if (!urlString.startsWith(ATP_SCHEME)) { | |
| 48 | + return; | |
| 49 | + } | |
| 50 | + | |
| 51 | + // Make request to atp | |
| 52 | + auto path = urlString.right(urlString.length() - ATP_SCHEME.length()); | |
| 53 | + auto parts = path.split(".", QString::SkipEmptyParts); | |
| 54 | + auto hash = parts.length() > 0 ? parts[0] : ""; | |
| 55 | + | |
| 56 | + if (hash.length() != SHA256_HASH_HEX_LENGTH) { | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + | |
| 60 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 61 | + auto assetRequest = assetClient->createRequest(hash); | |
| 62 | + | |
| 63 | + if (!assetRequest) { | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + | |
| 67 | + _pendingRequests << assetRequest; | |
| 68 | + | |
| 69 | + connect(assetRequest, &AssetRequest::finished, this, [this, callback](AssetRequest* request) mutable { | |
| 70 | + Q_ASSERT(request->getState() == AssetRequest::Finished); | |
| 71 | + | |
| 72 | + if (request->getError() == AssetRequest::Error::NoError) { | |
| 73 | + if (callback.isFunction()) { | |
| 74 | + QString data = QString::fromUtf8(request->getData()); | |
| 75 | + QScriptValueList args { data }; | |
| 76 | + callback.call(_engine->currentContext()->thisObject(), args); | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + request->deleteLater(); | |
| 81 | + _pendingRequests.remove(request); | |
| 82 | + }); | |
| 83 | + | |
| 84 | + assetRequest->start(); | |
| 85 | +} | |
| 86 | + | |
| 87 | +void AssetScriptingInterface::setMapping(QString path, QString hash, QScriptValue callback) { | |
| 88 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 89 | + auto request = assetClient->createSetMappingRequest(path, hash); | |
| 90 | + | |
| 91 | + connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable { | |
| 92 | + QScriptValueList args { uint8_t(request->getError()) }; | |
| 93 | + | |
| 94 | + callback.call(_engine->currentContext()->thisObject(), args); | |
| 95 | + | |
| 96 | + request->deleteLater(); | |
| 97 | + | |
| 98 | + }); | |
| 99 | + | |
| 100 | + request->start(); | |
| 101 | +} | |
| 102 | + | |
| 103 | +void AssetScriptingInterface::getMapping(QString path, QScriptValue callback) { | |
| 104 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 105 | + auto request = assetClient->createGetMappingRequest(path); | |
| 106 | + | |
| 107 | + connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable { | |
| 108 | + QScriptValueList args { uint8_t(request->getError()), request->getHash() }; | |
| 109 | + | |
| 110 | + callback.call(_engine->currentContext()->thisObject(), args); | |
| 111 | + | |
| 112 | + request->deleteLater(); | |
| 113 | + | |
| 114 | + }); | |
| 115 | + | |
| 116 | + request->start(); | |
| 117 | +} | |
| 118 | + | |
| 119 | +void AssetScriptingInterface::deleteMappings(QStringList paths, QScriptValue callback) { | |
| 120 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 121 | + auto request = assetClient->createDeleteMappingsRequest(paths); | |
| 122 | + | |
| 123 | + connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable { | |
| 124 | + QScriptValueList args { uint8_t(request->getError()) }; | |
| 125 | + | |
| 126 | + callback.call(_engine->currentContext()->thisObject(), args); | |
| 127 | + | |
| 128 | + request->deleteLater(); | |
| 129 | + | |
| 130 | + }); | |
| 131 | + | |
| 132 | + request->start(); | |
| 133 | +} | |
| 134 | + | |
| 135 | +void AssetScriptingInterface::getAllMappings(QScriptValue callback) { | |
| 136 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 137 | + auto request = assetClient->createGetAllMappingsRequest(); | |
| 138 | + | |
| 139 | + connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable { | |
| 140 | + auto mappings = request->getMappings(); | |
| 141 | + auto map = callback.engine()->newObject(); | |
| 142 | + | |
| 143 | + for (auto& kv : mappings ) { | |
| 144 | + map.setProperty(kv.first, kv.second); | |
| 145 | + } | |
| 146 | + | |
| 147 | + QScriptValueList args { uint8_t(request->getError()), map }; | |
| 148 | + | |
| 149 | + callback.call(_engine->currentContext()->thisObject(), args); | |
| 150 | + | |
| 151 | + request->deleteLater(); | |
| 152 | + | |
| 153 | + }); | |
| 154 | + | |
| 155 | + request->start(); | |
| 156 | +} | |
| 157 | + | |
| 158 | +void AssetScriptingInterface::renameMapping(QString oldPath, QString newPath, QScriptValue callback) { | |
| 159 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 160 | + auto request = assetClient->createRenameMappingRequest(oldPath, newPath); | |
| 161 | + | |
| 162 | + connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable { | |
| 163 | + QScriptValueList args { uint8_t(request->getError()) }; | |
| 164 | + | |
| 165 | + callback.call(_engine->currentContext()->thisObject(), args); | |
| 166 | + | |
| 167 | + request->deleteLater(); | |
| 168 | + | |
| 169 | + }); | |
| 170 | + | |
| 171 | + request->start(); | |
| 172 | +} | |