// // BaseAssetScriptingInterface.h // libraries/networking/src // // Copyright 2017 High Fidelity, Inc. // Copyright 2023 Overte e.V. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // SPDX-License-Identifier: Apache-2.0 // // BaseAssetScriptingInterface contains the engine-agnostic support code that can be used from // both QML JS and ScriptEngine JS engine implementations #ifndef hifi_BaseAssetScriptingInterface_h #define hifi_BaseAssetScriptingInterface_h #include #include #include #include "AssetClient.h" #include #include "NetworkAccessManager.h" #include class BaseAssetScriptingInterface : public QObject { Q_OBJECT public: /*@jsdoc *

Types of response that {@link Assets.decompressData}, {@link Assets.getAsset}, or {@link Assets.loadFromCache} may * provide.

* * * * * * * * * *
ValueDescription
"arraybuffer"A binary ArrayBuffer object.
"json"A parsed JSON object.
"text"UTF-8 decoded string value.
* @typedef {string} Assets.ResponseType */ const QStringList RESPONSE_TYPES{ "text", "arraybuffer", "json" }; using Promise = MiniPromise::Promise; QSharedPointer assetClient(); BaseAssetScriptingInterface(QObject* parent = nullptr); public slots: /*@jsdoc * Checks whether a string is a valid path. Note: A valid path must start with a "/". * @function Assets.isValidPath * @param {string} path - The path to check. * @returns {boolean} true if the path is a valid path, false if it isn't. */ bool isValidPath(QString input) { return AssetUtils::isValidPath(input); } /*@jsdoc * Checks whether a string is a valid path and filename. Note: A valid path and filename must start with a "/" * but must not end with a "/". * @function Assets.isValidFilePath * @param {string} path - The path to check. * @returns {boolean} true if the path is a valid file path, false if it isn't. */ bool isValidFilePath(QString input) { return AssetUtils::isValidFilePath(input); } /*@jsdoc * Gets the normalized ATP URL for a path or hash: ensures that it has "atp:" at the start. * @function Assets.getATPUrl * @param {string} url - The URL to normalize. * @returns {string} The normalized ATP URL. */ QUrl getATPUrl(QString input) { return AssetUtils::getATPUrl(input); } /*@jsdoc * Gets the SHA256 hexadecimal hash portion of an asset server URL. * @function Assets.extractAssetHash * @param {string} url - The URL to get the SHA256 hexadecimal hash from. * @returns {string} The SHA256 hexadecimal hash portion of the URL if present and valid, "" otherwise. */ QString extractAssetHash(QString input) { return AssetUtils::extractAssetHash(input); } /*@jsdoc * Checks whether a string is a valid SHA256 hexadecimal hash, i.e., 64 hexadecimal characters. * @function Assets.isValidHash * @param {string} hash - The hash to check. * @returns {boolean} true if the hash is a valid SHA256 hexadecimal string, false if it isn't. */ bool isValidHash(QString input) { return AssetUtils::isValidHash(input); } /*@jsdoc * Calculates the SHA256 hash of given data. * @function Assets.hashData * @param {string|ArrayBuffer} data - The data to calculate the hash of. * @returns {ArrayBuffer} The SHA256 hash of the data. */ QByteArray hashData(const QByteArray& data) { return AssetUtils::hashData(data); } /*@jsdoc * Calculates the SHA256 hash of given data, in hexadecimal format. * @function Assets.hashDataHex * @param {string|ArrayBuffer} data - The data to calculate the hash of. * @returns {string} The SHA256 hash of the data, in hexadecimal format. * @example Calculate the hash of some text. * var text = "Hello world!"; * print("Hash: " + Assets.hashDataHex(text)); */ QString hashDataHex(const QByteArray& data) { return hashData(data).toHex(); } protected: bool _cacheReady{ false }; bool initializeCache(); Promise getCacheStatus(); Promise queryCacheMeta(const QUrl& url); Promise loadFromCache(const QUrl& url, bool decompress = false, const QString& responseType = "arraybuffer"); Promise saveToCache(const QUrl& url, const QByteArray& data, const QVariantMap& metadata = QVariantMap()); Promise loadAsset(QString asset, bool decompress, QString responseType); Promise getAssetInfo(QString asset); Promise downloadBytes(QString hash); Promise uploadBytes(const QByteArray& bytes); Promise compressBytes(const QByteArray& bytes, int level = -1); Promise convertBytes(const QByteArray& dataByteArray, const QString& responseType); Promise decompressBytes(const QByteArray& bytes); Promise symlinkAsset(QString hash, QString path); }; #endif // hifi_BaseAssetScriptingInterface_h