Merge pull request #6439 from ZappoMan/assetJS

add Assets.uploadData() and Assets.downloadData()
This commit is contained in:
samcake 2015-11-19 17:56:10 -08:00
commit 732c911fd7
5 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,11 @@
var data = "this is some data";
var extension = "txt";
var uploadedFile;
Assets.uploadData(data, extension, function (url) {
print("data uploaded to:" + url);
uploadedFile = url;
Assets.downloadData(url, function (data) {
print("data downloaded from:" + url + " the data is:" + data);
});
});

View file

@ -365,3 +365,65 @@ void AssetClient::handleNodeKilled(SharedNodePointer node) {
}
}
}
void AssetScriptingInterface::uploadData(QString data, QString extension, QScriptValue callback) {
QByteArray dataByteArray = data.toUtf8();
auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray, extension);
QObject::connect(upload, &AssetUpload::finished, this, [callback, extension](AssetUpload* upload, const QString& hash) mutable {
if (callback.isFunction()) {
QString url = "atp://" + hash + "." + extension;
QScriptValueList args { url };
callback.call(QScriptValue(), args);
}
});
// start the upload now
upload->start();
}
void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callback) {
const QString ATP_SCHEME { "atp://" };
if (!urlString.startsWith(ATP_SCHEME)) {
return;
}
// Make request to atp
auto path = urlString.right(urlString.length() - ATP_SCHEME.length());
auto parts = path.split(".", QString::SkipEmptyParts);
auto hash = parts.length() > 0 ? parts[0] : "";
auto extension = parts.length() > 1 ? parts[1] : "";
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
return;
}
auto assetClient = DependencyManager::get<AssetClient>();
auto assetRequest = assetClient->createRequest(hash, extension);
if (!assetRequest) {
return;
}
_pendingRequests << assetRequest;
connect(assetRequest, &AssetRequest::finished, [this, callback](AssetRequest* request) mutable {
Q_ASSERT(request->getState() == AssetRequest::Finished);
if (request->getError() == AssetRequest::Error::NoError) {
if (callback.isFunction()) {
QString data = QString::fromUtf8(request->getData());
QScriptValueList args { data };
callback.call(QScriptValue(), args);
}
}
request->deleteLater();
_pendingRequests.remove(request);
});
assetRequest->start();
}

View file

@ -14,6 +14,7 @@
#define hifi_AssetClient_h
#include <QString>
#include <QScriptValue>
#include <DependencyManager.h>
@ -21,6 +22,7 @@
#include "LimitedNodeList.h"
#include "NLPacket.h"
#include "Node.h"
#include "ResourceCache.h"
class AssetRequest;
class AssetUpload;
@ -68,4 +70,15 @@ private:
friend class AssetUpload;
};
class AssetScriptingInterface : public QObject {
Q_OBJECT
public:
Q_INVOKABLE void uploadData(QString data, QString extension, QScriptValue callback);
Q_INVOKABLE void downloadData(QString url, QScriptValue downloadComplete);
protected:
QSet<AssetRequest*> _pendingRequests;
};
#endif

View file

@ -381,6 +381,9 @@ void ScriptEngine::init() {
auto recordingInterface = DependencyManager::get<RecordingScriptingInterface>();
registerGlobalObject("Recording", recordingInterface.data());
registerGlobalObject("Assets", &_assetScriptingInterface);
}
void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) {

View file

@ -23,6 +23,7 @@
#include <AnimationCache.h>
#include <AnimVariant.h>
#include <AssetClient.h>
#include <AvatarData.h>
#include <AvatarHashMap.h>
#include <LimitedNodeList.h>
@ -195,6 +196,8 @@ private:
ArrayBufferClass* _arrayBufferClass;
AssetScriptingInterface _assetScriptingInterface;
QHash<EntityItemID, RegisteredEventHandlers> _registeredHandlers;
void forwardHandlerCall(const EntityItemID& entityID, const QString& eventName, QScriptValueList eventHanderArgs);
Q_INVOKABLE void entityScriptContentAvailable(const EntityItemID& entityID, const QString& scriptOrURL, const QString& contents, bool isURL, bool success);