module.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package modules
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "sort"
  6. "strings"
  7. user "imuslab.com/arozos/mod/user"
  8. )
  9. type ModuleInfo struct {
  10. Name string //Name of this module. e.g. "Audio"
  11. Desc string //Description for this module
  12. Group string //Group of the module, e.g. "system" / "media" etc
  13. IconPath string //Module icon image path e.g. "Audio/img/function_icon.png"
  14. Version string //Version of the module. Format: [0-9]*.[0-9][0-9].[0-9]
  15. StartDir string //Default starting dir, e.g. "Audio/index.html"
  16. SupportFW bool //Support floatWindow. If yes, floatWindow dir will be loaded
  17. LaunchFWDir string //This link will be launched instead of 'StartDir' if fw mode
  18. SupportEmb bool //Support embedded mode
  19. LaunchEmb string //This link will be launched instead of StartDir / Fw if a file is opened with this module
  20. InitFWSize []int //Floatwindow init size. [0] => Width, [1] => Height
  21. InitEmbSize []int //Embedded mode init size. [0] => Width, [1] => Height
  22. SupportedExt []string //Supported File Extensions. e.g. ".mp3", ".flac", ".wav"
  23. //Hidden properties
  24. allowReload bool //Allow module reload by user
  25. }
  26. type ModuleHandler struct {
  27. LoadedModule []*ModuleInfo
  28. userHandler *user.UserHandler
  29. tmpDirectory string
  30. }
  31. func NewModuleHandler(userHandler *user.UserHandler, tmpFolderPath string) *ModuleHandler {
  32. return &ModuleHandler{
  33. LoadedModule: []*ModuleInfo{},
  34. userHandler: userHandler,
  35. tmpDirectory: tmpFolderPath,
  36. }
  37. }
  38. //Register endpoint. Provide moduleInfo datastructure or unparsed json
  39. func (m *ModuleHandler) RegisterModule(module ModuleInfo) {
  40. m.LoadedModule = append(m.LoadedModule, &module)
  41. }
  42. //Sort the module list
  43. func (m *ModuleHandler) ModuleSortList() {
  44. sort.Slice(m.LoadedModule, func(i, j int) bool {
  45. return m.LoadedModule[i].Name < m.LoadedModule[j].Name
  46. })
  47. }
  48. //Register a module from JSON string
  49. func (m *ModuleHandler) RegisterModuleFromJSON(jsonstring string, allowReload bool) error {
  50. var thisModuleInfo ModuleInfo
  51. err := json.Unmarshal([]byte(jsonstring), &thisModuleInfo)
  52. if err != nil {
  53. return err
  54. }
  55. thisModuleInfo.allowReload = allowReload
  56. m.RegisterModule(thisModuleInfo)
  57. return nil
  58. }
  59. //Register a module from AGI script
  60. func (m *ModuleHandler) RegisterModuleFromAGI(jsonstring string) error {
  61. var thisModuleInfo ModuleInfo
  62. err := json.Unmarshal([]byte(jsonstring), &thisModuleInfo)
  63. if err != nil {
  64. return err
  65. }
  66. //AGI interface loaded module must allow runtime reload
  67. thisModuleInfo.allowReload = true
  68. m.RegisterModule(thisModuleInfo)
  69. return nil
  70. }
  71. func (m *ModuleHandler) DeregisterModule(moduleName string) {
  72. newLoadedModuleList := []*ModuleInfo{}
  73. for _, thisModule := range m.LoadedModule {
  74. if thisModule.Name != moduleName {
  75. newLoadedModuleList = append(newLoadedModuleList, thisModule)
  76. }
  77. }
  78. m.LoadedModule = newLoadedModuleList
  79. }
  80. //Get a list of module names
  81. func (m *ModuleHandler) GetModuleNameList() []string {
  82. result := []string{}
  83. for _, module := range m.LoadedModule {
  84. result = append(result, module.Name)
  85. }
  86. return result
  87. }
  88. //Handle Default Launcher
  89. func (m *ModuleHandler) HandleDefaultLauncher(w http.ResponseWriter, r *http.Request) {
  90. username, _ := m.userHandler.GetAuthAgent().GetUserName(w, r)
  91. opr, _ := mv(r, "opr", false) //Operation, accept {get, set, launch}
  92. ext, _ := mv(r, "ext", false)
  93. moduleName, _ := mv(r, "module", false)
  94. ext = strings.ToLower(ext)
  95. //Check if the default folder exists.
  96. if opr == "get" {
  97. //Get the opener for this file type
  98. value := ""
  99. err := m.userHandler.GetDatabase().Read("module", "default/"+username+"/"+ext, &value)
  100. if err != nil {
  101. sendErrorResponse(w, "No default opener")
  102. return
  103. }
  104. js, _ := json.Marshal(value)
  105. sendJSONResponse(w, string(js))
  106. return
  107. } else if opr == "launch" {
  108. //Get launch paramter for this extension
  109. value := ""
  110. err := m.userHandler.GetDatabase().Read("module", "default/"+username+"/"+ext, &value)
  111. if err != nil {
  112. sendErrorResponse(w, "No default opener")
  113. return
  114. }
  115. //Get the launch paramter of this module
  116. var modInfo *ModuleInfo = nil
  117. modExists := false
  118. for _, mod := range m.LoadedModule {
  119. if mod.Name == value {
  120. modInfo = mod
  121. modExists = true
  122. }
  123. }
  124. if !modExists {
  125. //This module has been removed or not exists anymore
  126. sendErrorResponse(w, "Default opener no longer exists.")
  127. return
  128. } else {
  129. //Return launch inforamtion
  130. jsonString, _ := json.Marshal(modInfo)
  131. sendJSONResponse(w, string(jsonString))
  132. }
  133. } else if opr == "set" {
  134. //Set the opener for this filetype
  135. if moduleName == "" {
  136. sendErrorResponse(w, "Missing paratmer 'module'")
  137. return
  138. }
  139. //Check if module name exists
  140. moduleValid := false
  141. for _, mod := range m.LoadedModule {
  142. if mod.Name == moduleName {
  143. moduleValid = true
  144. }
  145. }
  146. if moduleValid {
  147. m.userHandler.GetDatabase().Write("module", "default/"+username+"/"+ext, moduleName)
  148. sendJSONResponse(w, "\"OK\"")
  149. } else {
  150. sendErrorResponse(w, "Given module not exists.")
  151. }
  152. } else if opr == "list" {
  153. //List all the values that belongs to default opener
  154. dbDump, _ := m.userHandler.GetDatabase().ListTable("module")
  155. results := [][]string{}
  156. for _, entry := range dbDump {
  157. key := string(entry[0])
  158. if strings.Contains(key, "default/"+username+"/") {
  159. //This is a correct matched entry
  160. extInfo := strings.Split(key, "/")
  161. ext := extInfo[len(extInfo)-1:]
  162. moduleName := ""
  163. json.Unmarshal(entry[1], &moduleName)
  164. results = append(results, []string{ext[0], moduleName})
  165. }
  166. }
  167. jsonString, _ := json.Marshal(results)
  168. sendJSONResponse(w, string(jsonString))
  169. return
  170. }
  171. }
  172. func (m *ModuleHandler) ListLoadedModules(w http.ResponseWriter, r *http.Request) {
  173. userinfo, _ := m.userHandler.GetUserInfoFromRequest(w, r)
  174. ///Parse a list of modules where the user has permission to access
  175. userAccessableModules := []*ModuleInfo{}
  176. for _, thisModule := range m.LoadedModule {
  177. thisModuleName := thisModule.Name
  178. if userinfo.GetModuleAccessPermission(thisModuleName) {
  179. userAccessableModules = append(userAccessableModules, thisModule)
  180. } else if thisModule.Group == "Utilities" {
  181. //Always allow utilties to be loaded
  182. userAccessableModules = append(userAccessableModules, thisModule)
  183. }
  184. }
  185. //Return the loaded modules as a list of JSON string
  186. jsonString, _ := json.Marshal(userAccessableModules)
  187. sendJSONResponse(w, string(jsonString))
  188. }
  189. func (m *ModuleHandler) GetModuleInfoByID(moduleid string) *ModuleInfo {
  190. for _, module := range m.LoadedModule {
  191. if module.Name == moduleid {
  192. return module
  193. }
  194. }
  195. return nil
  196. }
  197. func (m *ModuleHandler) GetLaunchParameter(w http.ResponseWriter, r *http.Request) {
  198. moduleName, _ := mv(r, "module", false)
  199. if moduleName == "" {
  200. sendErrorResponse(w, "Missing paramter 'module'.")
  201. return
  202. }
  203. //Loop through the modules and see if the module exists.
  204. var targetLaunchInfo *ModuleInfo = nil
  205. found := false
  206. for _, module := range m.LoadedModule {
  207. thisModuleName := module.Name
  208. if thisModuleName == moduleName {
  209. targetLaunchInfo = module
  210. found = true
  211. }
  212. }
  213. if found {
  214. jsonString, _ := json.Marshal(targetLaunchInfo)
  215. sendJSONResponse(w, string(jsonString))
  216. return
  217. } else {
  218. sendErrorResponse(w, "Given module not exists.")
  219. return
  220. }
  221. }