module.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. ext = strings.ToLower(ext)
  71. //Check if the default folder exists.
  72. if opr == "get" {
  73. //Get the opener for this file type
  74. value := ""
  75. err := m.userHandler.GetDatabase().Read("module", "default/"+username+"/"+ext, &value)
  76. if err != nil {
  77. sendErrorResponse(w, "No default opener")
  78. return
  79. }
  80. js, _ := json.Marshal(value)
  81. sendJSONResponse(w, string(js))
  82. return
  83. } else if opr == "launch" {
  84. //Get launch paramter for this extension
  85. value := ""
  86. err := m.userHandler.GetDatabase().Read("module", "default/"+username+"/"+ext, &value)
  87. if err != nil {
  88. sendErrorResponse(w, "No default opener")
  89. return
  90. }
  91. //Get the launch paramter of this module
  92. var modInfo ModuleInfo
  93. modExists := false
  94. for _, mod := range m.LoadedModule {
  95. if mod.Name == value {
  96. modInfo = mod
  97. modExists = true
  98. }
  99. }
  100. if !modExists {
  101. //This module has been removed or not exists anymore
  102. sendErrorResponse(w, "Default opener no longer exists.")
  103. return
  104. } else {
  105. //Return launch inforamtion
  106. jsonString, _ := json.Marshal(modInfo)
  107. sendJSONResponse(w, string(jsonString))
  108. }
  109. } else if opr == "set" {
  110. //Set the opener for this filetype
  111. if moduleName == "" {
  112. sendErrorResponse(w, "Missing paratmer 'module'")
  113. return
  114. }
  115. //Check if module name exists
  116. moduleValid := false
  117. for _, mod := range m.LoadedModule {
  118. if mod.Name == moduleName {
  119. moduleValid = true
  120. }
  121. }
  122. if moduleValid {
  123. m.userHandler.GetDatabase().Write("module", "default/"+username+"/"+ext, moduleName)
  124. sendJSONResponse(w, "\"OK\"")
  125. } else {
  126. sendErrorResponse(w, "Given module not exists.")
  127. }
  128. } else if opr == "list" {
  129. //List all the values that belongs to default opener
  130. dbDump, _ := m.userHandler.GetDatabase().ListTable("module")
  131. results := [][]string{}
  132. for _, entry := range dbDump {
  133. key := string(entry[0])
  134. if strings.Contains(key, "default/"+username+"/") {
  135. //This is a correct matched entry
  136. extInfo := strings.Split(key, "/")
  137. ext := extInfo[len(extInfo)-1:]
  138. moduleName := ""
  139. json.Unmarshal(entry[1], &moduleName)
  140. results = append(results, []string{ext[0], moduleName})
  141. }
  142. }
  143. jsonString, _ := json.Marshal(results)
  144. sendJSONResponse(w, string(jsonString))
  145. return
  146. }
  147. }
  148. func (m *ModuleHandler) ListLoadedModules(w http.ResponseWriter, r *http.Request) {
  149. userinfo, _ := m.userHandler.GetUserInfoFromRequest(w, r)
  150. ///Parse a list of modules where the user has permission to access
  151. userAccessableModules := []ModuleInfo{}
  152. for _, thisModule := range m.LoadedModule {
  153. thisModuleName := thisModule.Name
  154. if userinfo.GetModuleAccessPermission(thisModuleName) {
  155. userAccessableModules = append(userAccessableModules, thisModule)
  156. } else if thisModule.Group == "Utilities" {
  157. //Always allow utilties to be loaded
  158. userAccessableModules = append(userAccessableModules, thisModule)
  159. }
  160. }
  161. //Return the loaded modules as a list of JSON string
  162. jsonString, _ := json.Marshal(userAccessableModules)
  163. sendJSONResponse(w, string(jsonString))
  164. }
  165. func (m *ModuleHandler) GetModuleInfoByID(moduleid string) *ModuleInfo {
  166. for _, module := range m.LoadedModule {
  167. if module.Name == moduleid {
  168. return &module
  169. }
  170. }
  171. return nil
  172. }
  173. func (m *ModuleHandler) GetLaunchParameter(w http.ResponseWriter, r *http.Request) {
  174. moduleName, _ := mv(r, "module", false)
  175. if moduleName == "" {
  176. sendErrorResponse(w, "Missing paramter 'module'.")
  177. return
  178. }
  179. //Loop through the modules and see if the module exists.
  180. var targetLaunchInfo ModuleInfo
  181. found := false
  182. for _, module := range m.LoadedModule {
  183. thisModuleName := module.Name
  184. if thisModuleName == moduleName {
  185. targetLaunchInfo = module
  186. found = true
  187. }
  188. }
  189. if found {
  190. jsonString, _ := json.Marshal(targetLaunchInfo)
  191. sendJSONResponse(w, string(jsonString))
  192. return
  193. } else {
  194. sendErrorResponse(w, "Given module not exists.")
  195. return
  196. }
  197. }