From 7f997649dfe17949597fd0926797d7e18b780695 Mon Sep 17 00:00:00 2001 From: Moe Date: Sun, 9 Aug 2020 23:13:18 -0700 Subject: [PATCH] customAutoLoad Module UI : Enable, Disable, Delete, Install functions --- languages/en_CA.json | 5 + libs/customAutoLoad.js | 82 ++++++++++--- web/libs/js/super.customAutoLoad.js | 114 ++++++++++++++++++ .../blocks/superCustomAutoLoadManager.ejs | 4 + web/pages/super.ejs | 8 +- 5 files changed, 193 insertions(+), 20 deletions(-) create mode 100644 web/libs/js/super.customAutoLoad.js create mode 100644 web/pages/blocks/superCustomAutoLoadManager.ejs diff --git a/languages/en_CA.json b/languages/en_CA.json index 99939555..a33bb8a7 100644 --- a/languages/en_CA.json +++ b/languages/en_CA.json @@ -199,6 +199,7 @@ "File Type": "File Type", "Filesize": "Filesize", "Video Status": "Video Status", + "Custom Auto Load": "Custom Auto Load", "Preferences": "Preferences", "Equal to": "Equal to", "Not Equal to": "Not Equal to", @@ -482,6 +483,10 @@ "DetectorText": "

When the Width and Height boxes are shown you should set them to 640x480 or below. This will optimize the read speed of frames.

", "RecordingText": "It is recommended that you set Record File Type to WebMMP4 and Video Codec to libvpxcopy or libx264 because your Input Type is set to .", "Mode": "Mode", + "Install": "Install", + "Enable": "Enable", + "Disable": "Disable", + "Delete": "Delete", "Name": "Name", "Skip Ping": "Skip Ping", "Retry Connection": "Retry Connection Number of times allowed to fail", diff --git a/libs/customAutoLoad.js b/libs/customAutoLoad.js index cf47d748..8946f6af 100644 --- a/libs/customAutoLoad.js +++ b/libs/customAutoLoad.js @@ -31,8 +31,12 @@ module.exports = async (s,config,lang,app,io) => { if(!fs.existsSync(modulePath + '/index.js')){ foundModules[moduleName].noIndex = true } - if(!fs.existsSync(modulePath + '/package.json')){ + if(fs.existsSync(modulePath + '/package.json')){ foundModules[moduleName].properties = getModuleProperties(moduleName) + }else{ + foundModules[moduleName].properties = { + name: moduleName + } } }else{ foundModules[moduleName].isIgnitor = (moduleName.indexOf('.js') > -1) @@ -104,6 +108,8 @@ module.exports = async (s,config,lang,app,io) => { installProcess.on('exit',(data) => { resolve() }) + }else{ + resolve() } }) } @@ -112,15 +118,28 @@ module.exports = async (s,config,lang,app,io) => { const modulePath = getModulePath(name) const properties = getModuleProperties(name); const propertiesPath = modulePath + 'package.json' - var packageJson = JSON.parse(fs.readFileSync(propertiesPath)) + var packageJson = { + name: name + } + try{ + packageJson = JSON.parse(fs.readFileSync(propertiesPath)) + }catch(err){ + + } packageJson.disabled = status; fs.writeFileSync(propertiesPath,s.prettyPrint(packageJson)) } const deleteModule = (name) => { // requires restart for changes to take effect - const modulePath = getModulePath(name) - fs.unlink(modulePath) - s.file('delete',modulePath) + try{ + const modulePath = modulesBasePath + name + fs.unlinkSync(modulePath) + s.file('delete',modulePath) + return true + }catch(err){ + console.log(err) + return false + } } const loadModule = (shinobiModule) => { const moduleName = shinobiModule.name @@ -248,6 +267,11 @@ module.exports = async (s,config,lang,app,io) => { } } } + const moveModuleToNameInProperties = (modulePath,packageRoot,properties) => { + fs.renameSync(modulePath + packageRoot,modulesBasePath + properties.name) + fs.unlinkSync(modulePath) + s.file('delete',modulePath) + } const initializeAllModules = async () => { s.customAutoLoadModules = {} s.customAutoLoadTree = { @@ -271,25 +295,45 @@ module.exports = async (s,config,lang,app,io) => { loadModule(shinobiModule) }) }else{ - fs.mkdirSync(modulesBasePath) + fs.mkdir(modulesBasePath,() => {}) } }) } /** * API : Superuser : Custom Auto Load Package Download. */ + app.get(config.webPaths.superApiPrefix+':auth/package/list', async (req,res) => { + s.superAuth(req.params, async (resp) => { + s.closeJsonResponse(res,{ + ok: true, + modules: getModules() + }) + },res,req) + }) + /** + * API : Superuser : Custom Auto Load Package Download. + */ app.post(config.webPaths.superApiPrefix+':auth/package/download', async (req,res) => { s.superAuth(req.params, async (resp) => { - const url = req.body.downloadUrl - const packageRoot = req.body.packageRoot || '' - const packageName = req.body.packageName || extractNameFromPackage(url) - const modulePath = getModulePath(packageName) - await downloadModule(url,packageName) - const properties = getModuleProperties(packageName) - fs.renameSync(modulePath + packageRoot,modulesBasePath + properties.name) - fs.unlinkSync(modulePath) - s.file('delete',modulePath) - s.closeJsonResponse(res,{ok: true}) + try{ + const url = req.body.downloadUrl + const packageRoot = req.body.packageRoot || '' + const packageName = req.body.packageName || extractNameFromPackage(url) + const modulePath = getModulePath(packageName) + await downloadModule(url,packageName) + const properties = getModuleProperties(packageName) + const newName = await moveModuleToNameInProperties(modulePath,packageRoot,properties) + disableModule(newName,true) + s.closeJsonResponse(res,{ + ok: true, + moduleName: newName + }) + }catch(err){ + console.log(err) + s.closeJsonResponse(res,{ + ok: false + }) + } },res,req) }) /** @@ -309,7 +353,7 @@ module.exports = async (s,config,lang,app,io) => { s.superAuth(req.params, async (resp) => { const status = req.body.status const packageName = req.body.packageName - disableModule(packageName,status === 'true' ? true : false) + disableModule(packageName,status == 'true' ? true : false) s.closeJsonResponse(res,{ok: true}) },res,req) }) @@ -319,8 +363,8 @@ module.exports = async (s,config,lang,app,io) => { app.post(config.webPaths.superApiPrefix+':auth/package/delete', async (req,res) => { s.superAuth(req.params, async (resp) => { const packageName = req.body.packageName - deleteModule(packageName) - s.closeJsonResponse(res,{ok: true}) + const response = deleteModule(packageName) + s.closeJsonResponse(res,{ok: response}) },res,req) }) /** diff --git a/web/libs/js/super.customAutoLoad.js b/web/libs/js/super.customAutoLoad.js new file mode 100644 index 00000000..d54c18b2 --- /dev/null +++ b/web/libs/js/super.customAutoLoad.js @@ -0,0 +1,114 @@ +$(document).ready(function(){ + var loadedModules = {} + var listElement = $('#customAutoLoadList') + var getModules = function(callback) { + $.get(superApiPrefix + $user.sessionKey + '/package/list',callback) + } + var drawModuleBlock = function(module){ + listElement.append(`
+
+

${module.properties.name ? module.properties.name : module.name}

+
+ ${!module.isIgnitor ? ` + ${lang.Install} + ${module.disabled ? lang.Enable : lang.Disable} + ` : ''} + ${lang.Delete} +
+
+
`) + } + var downloadModule = function(url,packageRoot){ + $.confirm.create({ + title: 'Module Download', + body: `Do you want to download the module from ${url}? `, + clickOptions: { + class: 'btn-success', + title: lang.Download, + }, + clickCallback: function(){ + $.post(superApiPrefix + $user.sessionKey + '/package/download',{ + downloadUrl: url, + packageRoot: packageRoot, + },function(data){ + + }) + } + }) + } + var installModule = function(packageName,callback){ + $.confirm.create({ + title: 'Install Module', + body: `Do you want to install the module ${packageName}?`, + clickOptions: { + class: 'btn-success', + title: lang.Install, + }, + clickCallback: function(){ + $.post(superApiPrefix + $user.sessionKey + '/package/install',{ + packageName: packageName, + },callback) + } + }) + } + var deleteModule = function(packageName,callback){ + $.confirm.create({ + title: 'Delete Module', + body: `Do you want to delete the module ${packageName}?`, + clickOptions: { + class: 'btn-danger', + title: lang.Delete, + }, + clickCallback: function(){ + $.post(superApiPrefix + $user.sessionKey + '/package/delete',{ + packageName: packageName, + },callback) + } + }) + } + var setModuleStatus = function(packageName,status,callback){ + $.post(superApiPrefix + $user.sessionKey + '/package/status',{ + status: status, + packageName: packageName, + },callback) + } + $('body').on(`click`,`[calm-action]`,function(){ + var el = $(this) + var action = el.attr('calm-action') + var card = el.parent('[package-name]') + var packageName = card.attr('package-name') + switch(action){ + case'install': + installModule(packageName,function(data){ + if(data.ok){ + console.log(data) + } + }) + break; + case'status': + setModuleStatus(packageName,!!!loadedModules[packageName].disabled,function(data){ + if(data.ok){ + loadedModules[packageName].disabled = !!!loadedModules[packageName].disabled + el.text(loadedModules[packageName].disabled ? lang.Enable : lang.Disable) + } + }) + break; + case'delete': + deleteModule(packageName,function(data){ + if(data.ok){ + card.remove() + } + }) + break; + } + }) + setTimeout(function(){ + getModules(function(data){ + loadedModules = data.modules + console.log(loadedModules) + $.each(data.modules,function(n,module){ + drawModuleBlock(module) + }) + }) + },2000) +}) diff --git a/web/pages/blocks/superCustomAutoLoadManager.ejs b/web/pages/blocks/superCustomAutoLoadManager.ejs new file mode 100644 index 00000000..561f07bb --- /dev/null +++ b/web/pages/blocks/superCustomAutoLoadManager.ejs @@ -0,0 +1,4 @@ +
+ +
+ diff --git a/web/pages/super.ejs b/web/pages/super.ejs index 731c00e0..dddbc38f 100644 --- a/web/pages/super.ejs +++ b/web/pages/super.ejs @@ -40,6 +40,7 @@ <% }) %> + @@ -87,6 +88,9 @@ +
@@ -101,6 +105,9 @@
<% include blocks/superSystemTab.ejs %>
+
+ <% include blocks/superCustomAutoLoadManager.ejs %> +
<% include blocks/changeSuperPreferences.ejs %>
@@ -151,7 +158,6 @@ } -