Add function hasValidScriptSuffix and check if script extensions are JavaScript or JSON files

This commit is contained in:
Liv 2017-05-23 13:08:50 -07:00
parent 1fa3e6bda2
commit e9fac38bbb
2 changed files with 16 additions and 0 deletions

View file

@ -425,6 +425,15 @@ QString ScriptEngine::getFilename() const {
return lastPart;
}
bool ScriptEngine::hasValidScriptSuffix(const QString& scriptFileName) {
QFileInfo fileInfo(scriptFileName);
QString scriptSuffixToLower = fileInfo.completeSuffix().toLower();
if (scriptSuffixToLower == "js" || scriptSuffixToLower == "json") {
return true;
}
return false;
}
void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) {
if (_isRunning) {
return;
@ -434,6 +443,12 @@ void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) {
_fileNameString = url.toString();
_isReloading = reload;
// Check that script is an actual script
if (!hasValidScriptSuffix(_fileNameString)) {
qCDebug(scriptengine) << "File extension of file: " + _fileNameString + " is not a currently supported script type";
return;
}
const auto maxRetries = 0; // for consistency with previous scriptCache->getScript() behavior
auto scriptCache = DependencyManager::get<ScriptCache>();
scriptCache->getScriptContents(url.toString(), [this](const QString& url, const QString& scriptContents, bool isURL, bool success, const QString&status) {

View file

@ -146,6 +146,7 @@ public:
/// to run... NOTE - this is used by Application currently to load the url. We don't really want it to be exposed
/// to scripts. we may not need this to be invokable
void loadURL(const QUrl& scriptURL, bool reload);
bool hasValidScriptSuffix(const QString& scriptFileName);
Q_INVOKABLE QString getContext() const;
Q_INVOKABLE bool isClientScript() const { return _context == CLIENT_SCRIPT; }