This commit is contained in:
David Rowe 2020-09-23 23:08:02 +12:00
parent 32efb2443e
commit 1cb1c63228
3 changed files with 24 additions and 72 deletions

View file

@ -3295,7 +3295,6 @@ void Application::initializeUi() {
qmlRegisterType<ResourceImageItem>("Hifi", 1, 0, "ResourceImageItem"); qmlRegisterType<ResourceImageItem>("Hifi", 1, 0, "ResourceImageItem");
qmlRegisterType<Preference>("Hifi", 1, 0, "Preference"); qmlRegisterType<Preference>("Hifi", 1, 0, "Preference");
qmlRegisterType<WebBrowserSuggestionsEngine>("HifiWeb", 1, 0, "WebBrowserSuggestionsEngine"); qmlRegisterType<WebBrowserSuggestionsEngine>("HifiWeb", 1, 0, "WebBrowserSuggestionsEngine");
// qmlRegisterType<ExternalResource>("ExternalResource", 1, 0, "ExternalResource");
{ {
auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>(); auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>();
@ -7569,10 +7568,6 @@ void Application::registerScriptEngineWithApplicationServices(const ScriptEngine
scriptEngine->registerGlobalObject("HifiAbout", AboutUtil::getInstance()); // Deprecated. scriptEngine->registerGlobalObject("HifiAbout", AboutUtil::getInstance()); // Deprecated.
scriptEngine->registerGlobalObject("ResourceRequestObserver", DependencyManager::get<ResourceRequestObserver>().data()); scriptEngine->registerGlobalObject("ResourceRequestObserver", DependencyManager::get<ResourceRequestObserver>().data());
//scriptEngine->registerGlobalObject("ExternalResource", ExternalResource::getInstance());
// scriptEngine->registerEnum("Script.ExternalPaths", QMetaEnum::fromType<ExternalResource::Bucket>());
registerInteractiveWindowMetaType(scriptEngine.data()); registerInteractiveWindowMetaType(scriptEngine.data());
auto pickScriptingInterface = DependencyManager::get<PickScriptingInterface>(); auto pickScriptingInterface = DependencyManager::get<PickScriptingInterface>();

View file

@ -4,7 +4,7 @@
// Created by Dale Glass on 6 Sep 2020 // Created by Dale Glass on 6 Sep 2020
// Copyright 2020 Vircadia contributors. // Copyright 2020 Vircadia contributors.
// //
// Flexible management for external resources (eg, on S3) // Flexible management for external resources (e.g., on S3).
// //
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
@ -23,25 +23,24 @@ ExternalResource* ExternalResource::getInstance() {
return &instance; return &instance;
} }
QUrl ExternalResource::getQUrl(Bucket bucket, const QUrl& relative_path) { QUrl ExternalResource::getQUrl(Bucket bucket, const QUrl& path) {
qCDebug(external_resource) << "Requested URL for bucket " << bucket << ", path " << relative_path; qCDebug(external_resource) << "Requested URL for bucket " << bucket << ", path " << path;
if (!_bucketBases.contains(bucket)) { if (!_bucketBases.contains(bucket)) {
qCCritical(external_resource) << "External resource " << relative_path << " was requested from unrecognized bucket " qCCritical(external_resource) << "External resource " << path << " was requested from unrecognized bucket " << bucket;
<< bucket;
return QUrl(); return QUrl();
} }
if (!relative_path.isValid()) { if (!path.isValid()) {
qCCritical(external_resource) << "External resource " << relative_path << " was requested from bucket " << bucket qCCritical(external_resource) << "External resource " << path << " was requested from bucket " << bucket
<< " with an invalid path. Error: " << relative_path.errorString(); << " with an invalid path. Error: " << path.errorString();
return QUrl(); return QUrl();
} }
if (!relative_path.isRelative()) { if (!path.isRelative()) {
qCWarning(external_resource) << "External resource " << relative_path << " was requested from bucket " << bucket qCWarning(external_resource) << "External resource " << path << " was requested from bucket " << bucket
<< " without using a relative path, returning as-is."; << " without using a relative path, returning as-is.";
return relative_path; return path;
} }
QUrl base; QUrl base;
@ -50,13 +49,13 @@ QUrl ExternalResource::getQUrl(Bucket bucket, const QUrl& relative_path) {
base = _bucketBases[bucket]; base = _bucketBases[bucket];
} }
QUrl merged = base.resolved(relative_path).adjusted(QUrl::NormalizePathSegments); QUrl merged = base.resolved(path).adjusted(QUrl::NormalizePathSegments);
if ( merged.isValid() ) { if ( merged.isValid() ) {
qCDebug(external_resource) << "External resource resolved to " << merged; qCDebug(external_resource) << "External resource resolved to " << merged;
} else { } else {
qCCritical(external_resource) << "External resource resolved to invalid URL " << merged << "; Error " << merged.errorString() qCCritical(external_resource) << "External resource resolved to invalid URL " << merged << "; Error "
<< "; base = " << base << "; relative_path = " << relative_path; << merged.errorString() << "; base = " << base << "; path = " << path;
} }
return merged; return merged;
@ -68,9 +67,9 @@ QString ExternalResource::getBase(Bucket bucket) {
}; };
bool ExternalResource::setBase(Bucket bucket, const QString& url) { bool ExternalResource::setBase(Bucket bucket, const QString& url) {
QUrl new_url(url); QUrl newURL(url);
if (!new_url.isValid() || new_url.isRelative()) { if (!newURL.isValid() || newURL.isRelative()) {
qCCritical(external_resource) << "Attempted to set bucket " << bucket << " to invalid URL " << url; qCCritical(external_resource) << "Attempted to set bucket " << bucket << " to invalid URL " << url;
return false; return false;
} }
@ -80,9 +79,9 @@ bool ExternalResource::setBase(Bucket bucket, const QString& url) {
return false; return false;
} }
qCDebug(external_resource) << "Setting base URL for " << bucket << " to " << new_url; qCDebug(external_resource) << "Setting base URL for " << bucket << " to " << newURL;
std::lock_guard<std::mutex> guard(_bucketMutex); std::lock_guard<std::mutex> guard(_bucketMutex);
_bucketBases[bucket] = new_url; _bucketBases[bucket] = newURL;
return true; return true;
} }

View file

@ -4,7 +4,7 @@
// Created by Dale Glass on 6 Sep 2020 // Created by Dale Glass on 6 Sep 2020
// Copyright 2020 Vircadia contributors. // Copyright 2020 Vircadia contributors.
// //
// Flexible management for external resources (eg, on S3) // Flexible management for external resources (e.g., on S3).
// //
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
@ -25,11 +25,11 @@
* *
* With the death of the original High Fidelity and the transition of the project to a community-managed * With the death of the original High Fidelity and the transition of the project to a community-managed
* one, it became necessary to deal with that the various assets that used to be located under the * one, it became necessary to deal with that the various assets that used to be located under the
* highfidelity.com domain will disappear, and there won't be a fixed place for them afterwards. Data * highfidelity.com domain, and there won't be a fixed place for them afterwards. Data
* hosted by community members may not remain forever, reorganization may be necessary, people may want * hosted by community members may not remain forever, reorganization may be necessary, people may want
* to run mirrors, and some might want to run without external internet access at all. * to run mirrors, and some might want to run without external Internet access at all.
* *
* This class makes it possible to deal with this in a more flexible manner: rather than hardcoding URLs * This class makes it possible to deal with this in a more flexible manner: rather than hard-coding URLs
* all over the codebase, now it's possible to easily change where all those things are downloaded from. * all over the codebase, now it's possible to easily change where all those things are downloaded from.
* *
* The term 'bucket' refers to the buckets used on Amazon S3, but there's no requirement for S3 or anything * The term 'bucket' refers to the buckets used on Amazon S3, but there's no requirement for S3 or anything
@ -94,52 +94,10 @@ public:
* @param relative_path The path of the resource within the bucket * @param relative_path The path of the resource within the bucket
* @returns The resulting URL as a QUrl * @returns The resulting URL as a QUrl
*/ */
QUrl getQUrl(Bucket bucket, const QUrl& relative_path); QUrl getQUrl(Bucket bucket, const QUrl& path);
/** QString getUrl(Bucket bucket, const QString& path) {
* Returns the location of a resource as a QUrl return ExternalResource::getQUrl(bucket, QUrl(path)).toString();
*
* Returns the location of the resource \p relative_path in bucket \p bucket
*
* @note The resulting path will be sanitized by condensing multiple instances of '/' to one.
* This is done for easier usage with Amazon S3 and compatible systems.
*
* @param bucket The bucket in which the resource is found
* @param relative_path The path of the resource within the bucket
* @returns The resulting URL as a QUrl
*/
QUrl getQUrl(Bucket bucket, QString path) { return getQUrl(bucket, QUrl(path)); }
/**
* Returns the location of a resource as a QString
*
* Returns the location of the resource \p relative_path in bucket \p bucket
*
* @note The resulting path will be sanitized by condensing multiple instances of '/' to one.
* This is done for easier usage with Amazon S3 and compatible systems.
*
* @param bucket The bucket in which the resource is found
* @param relative_path The path of the resource within the bucket
* @returns The resulting URL as a QString
*/
QString getUrl(Bucket bucket, const QUrl& relative_path) {
return ExternalResource::getQUrl(bucket, relative_path).toString();
};
/**
* Returns the location of a resource as a QString
*
* Returns the location of the resource \p relative_path in bucket \p bucket
*
* @note The resulting path will be sanitized by condensing multiple instances of '/' to one.
* This is done for easier usage with Amazon S3 and compatible systems.
*
* @param bucket The bucket in which the resource is found
* @param relative_path The path of the resource within the bucket
* @returns The resulting URL as a QString
*/
Q_INVOKABLE QString getUrl(Bucket bucket, const QString& relative_path) {
return ExternalResource::getQUrl(bucket, QUrl(relative_path)).toString();
}; };
/** /**