module.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  24. type ModuleHandler struct {
  25. LoadedModule []ModuleInfo
  26. userHandler *user.UserHandler
  27. tmpDirectory string
  28. }
  29. func NewModuleHandler(userHandler *user.UserHandler, tmpFolderPath string) *ModuleHandler {
  30. return &ModuleHandler{
  31. LoadedModule: []ModuleInfo{},
  32. userHandler: userHandler,
  33. tmpDirectory: tmpFolderPath,
  34. }
  35. }
  36. //Register endpoint. Provide moduleInfo datastructure or unparsed json
  37. func (m *ModuleHandler) RegisterModule(module ModuleInfo) {
  38. m.LoadedModule = append(m.LoadedModule, module)
  39. }
  40. //Sort the module list
  41. func (m *ModuleHandler) ModuleSortList() {
  42. sort.Slice(m.LoadedModule, func(i, j int) bool {
  43. return m.LoadedModule[i].Name < m.LoadedModule[j].Name
  44. })
  45. }
  46. //Register a module from JSON string
  47. func (m *ModuleHandler) RegisterModuleFromJSON(jsonstring string) error {
  48. var thisModuleInfo ModuleInfo
  49. err := json.Unmarshal([]byte(jsonstring), &thisModuleInfo)
  50. if err != nil {
  51. return err
  52. }
  53. m.RegisterModule(thisModuleInfo)
  54. return nil
  55. }
  56. //Get a list of module names
  57. func (m *ModuleHandler) GetModuleNameList() []string {
  58. result := []string{}
  59. for _, module := range m.LoadedModule {
  60. result = append(result, module.Name)
  61. }
  62. return result
  63. }
  64. //Handle Default Launcher
  65. func (m *ModuleHandler) HandleDefaultLauncher(w http.ResponseWriter, r *http.Request) {
  66. username, _ := m.userHandler.GetAuthAgent().GetUserName(w, r)
  67. opr, _ := mv(r, "opr", false) //Operation, accept {get, set, launch}
  68. ext, _ := mv(r, "ext", false)
  69. moduleName, _ := mv(r, "module", false)
  70. //Check if the default folder exists.
  71. if opr == "get" {
  72. //Get the opener for this file type
  73. value := ""
  74. err := m.userHandler.GetDatabase().Read("module", "default/"+username+"/"+ext, &value)
  75. if err != nil {
  76. sendErrorResponse(w, "No default opener")
  77. return
  78. }
  79. js, _ := json.Marshal(value)
  80. sendJSONResponse(w, string(js))
  81. return
  82. } else if opr == "launch" {
  83. //Get launch paramter for this extension
  84. value := ""
  85. err := m.userHandler.GetDatabase().Read("module", "default/"+username+"/"+ext, &value)
  86. if err != nil {
  87. sendErrorResponse(w, "No default opener")
  88. return
  89. }
  90. //Get the launch paramter of this module
  91. var modInfo ModuleInfo
  92. modExists := false
  93. for _, mod := range m.LoadedModule {
  94. if mod.Name == value {
  95. modInfo = mod
  96. modExists = true
  97. }
  98. }
  99. if !modExists {
  100. //This module has been removed or not exists anymore
  101. sendErrorResponse(w, "Default opener no longer exists.")
  102. return
  103. } else {
  104. //Return launch inforamtion
  105. jsonString, _ := json.Marshal(modInfo)
  106. sendJSONResponse(w, string(jsonString))
  107. }
  108. } else if opr == "set" {
  109. //Set the opener for this filetype
  110. if moduleName == "" {
  111. sendErrorResponse(w, "Missing paratmer 'module'")
  112. return
  113. }
  114. //Check if module name exists
  115. moduleValid := false
  116. for _, mod := range m.LoadedModule {
  117. if mod.Name == moduleName {
  118. moduleValid = true
  119. }
  120. }
  121. if moduleValid {
  122. m.userHandler.GetDatabase().Write("module", "default/"+username+"/"+ext, moduleName)
  123. sendJSONResponse(w, "\"OK\"")
  124. } else {
  125. sendErrorResponse(w, "Given module not exists.")
  126. }
  127. } else if opr == "list" {
  128. //List all the values that belongs to default opener
  129. dbDump, _ := m.userHandler.GetDatabase().ListTable("module")
  130. results := [][]string{}
  131. for _, entry := range dbDump {
  132. key := string(entry[0])
  133. if strings.Contains(key, "default/"+username+"/") {
  134. //This is a correct matched entry
  135. extInfo := strings.Split(key, "/")
  136. ext := extInfo[len(extInfo)-1:]
  137. moduleName := ""
  138. json.Unmarshal(entry[1], &moduleName)
  139. results = append(results, []string{ext[0], moduleName})
  140. }
  141. }
  142. jsonString, _ := json.Marshal(results)
  143. sendJSONResponse(w, string(jsonString))
  144. return
  145. }
  146. }
  147. func (m *ModuleHandler) ListLoadedModules(w http.ResponseWriter, r *http.Request) {
  148. userinfo, _ := m.userHandler.GetUserInfoFromRequest(w, r)
  149. ///Parse a list of modules where the user has permission to access
  150. userAccessableModules := []ModuleInfo{}
  151. for _, thisModule := range m.LoadedModule {
  152. thisModuleName := thisModule.Name
  153. if userinfo.GetModuleAccessPermission(thisModuleName) {
  154. userAccessableModules = append(userAccessableModules, thisModule)
  155. } else if thisModule.Group == "Utilities" {
  156. //Always allow utilties to be loaded
  157. userAccessableModules = append(userAccessableModules, thisModule)
  158. }
  159. }
  160. //Return the loaded modules as a list of JSON string
  161. jsonString, _ := json.Marshal(userAccessableModules)
  162. sendJSONResponse(w, string(jsonString))
  163. }
  164. func (m *ModuleHandler) GetModuleInfoByID(moduleid string) *ModuleInfo {
  165. for _, module := range m.LoadedModule {
  166. if module.Name == moduleid {
  167. return &module
  168. }
  169. }
  170. return nil
  171. }
  172. func (m *ModuleHandler) GetLaunchParameter(w http.ResponseWriter, r *http.Request) {
  173. moduleName, _ := mv(r, "module", false)
  174. if moduleName == "" {
  175. sendErrorResponse(w, "Missing paramter 'module'.")
  176. return
  177. }
  178. //Loop through the modules and see if the module exists.
  179. var targetLaunchInfo ModuleInfo
  180. found := false
  181. for _, module := range m.LoadedModule {
  182. thisModuleName := module.Name
  183. if thisModuleName == moduleName {
  184. targetLaunchInfo = module
  185. found = true
  186. }
  187. }
  188. if found {
  189. jsonString, _ := json.Marshal(targetLaunchInfo)
  190. sendJSONResponse(w, string(jsonString))
  191. return
  192. } else {
  193. sendErrorResponse(w, "Given module not exists.")
  194. return
  195. }
  196. }