module.util.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package main
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. module "imuslab.com/arozos/mod/modules"
  6. )
  7. /*
  8. MODULE UTIL HANDLER
  9. This is a util module for doing basic registry works and < 20 line server side handling.
  10. DO NOT USE THIS TO WRITE A NEW MODULE
  11. >> Updates v1.112
  12. This util functions will be deprecated before v1.120.
  13. Please migrate all of the modules out as WebApps using agi interface
  14. */
  15. //Register the utilities here
  16. func util_init() {
  17. //PDF Viewer
  18. /*
  19. moduleHandler.RegisterModule(module.ModuleInfo{
  20. Name: "PDF Reader",
  21. Desc: "The browser build in PDF Reader",
  22. Group: "Utilities",
  23. IconPath: "SystemAO/utilities/img/pdfReader.png",
  24. Version: "1.2",
  25. SupportFW: false,
  26. SupportEmb: true,
  27. LaunchEmb: "SystemAO/utilities/pdfReader.html",
  28. InitEmbSize: []int{1080, 580},
  29. SupportedExt: []string{".pdf"},
  30. })
  31. */
  32. //Open Documents Viewer
  33. moduleHandler.RegisterModule(module.ModuleInfo{
  34. Name: "OpenOffice Reader",
  35. Desc: "Open OpenOffice files",
  36. Group: "Utilities",
  37. IconPath: "SystemAO/utilities/img/odfReader.png",
  38. Version: "0.8",
  39. SupportFW: false,
  40. SupportEmb: true,
  41. LaunchEmb: "SystemAO/utilities/odfReader.html",
  42. InitEmbSize: []int{1080, 580},
  43. SupportedExt: []string{".odt", ".odp", ".ods"},
  44. })
  45. /*
  46. Notebook - The build in basic text editor
  47. */
  48. //Open Documents Viewer
  49. moduleHandler.RegisterModule(module.ModuleInfo{
  50. Name: "Notebook",
  51. Desc: "Basic Text Editor",
  52. Group: "Utilities",
  53. IconPath: "SystemAO/utilities/img/notebook.png",
  54. Version: "1.0",
  55. SupportFW: false,
  56. SupportEmb: true,
  57. LaunchEmb: "SystemAO/utilities/notebook.html",
  58. InitEmbSize: []int{1080, 580},
  59. SupportedExt: []string{".txt", ".md"},
  60. })
  61. http.HandleFunc("/system/utils/notebook/save", system_util_handleNotebookSave)
  62. /*
  63. ArOZ Media Player - The basic video player
  64. */
  65. //Open Documents Viewer
  66. moduleHandler.RegisterModule(module.ModuleInfo{
  67. Name: "ArOZ Media Player",
  68. Desc: "Basic Video Player",
  69. Group: "Utilities",
  70. IconPath: "SystemAO/utilities/img/mediaPlayer.png",
  71. Version: "1.0",
  72. SupportFW: false,
  73. SupportEmb: true,
  74. LaunchEmb: "SystemAO/utilities/mediaPlayer.html",
  75. InitEmbSize: []int{720, 480},
  76. SupportedExt: []string{".mp4", ".webm", ".ogv"},
  77. })
  78. /*
  79. ArOZ Audio Player - Basic Audio File Player
  80. */
  81. moduleHandler.RegisterModule(module.ModuleInfo{
  82. Name: "Audio Player",
  83. Desc: "Basic Audio Player",
  84. Group: "Utilities",
  85. IconPath: "SystemAO/utilities/img/audio.png",
  86. Version: "1.0",
  87. SupportFW: false,
  88. SupportEmb: true,
  89. LaunchEmb: "SystemAO/utilities/audio.html",
  90. InitEmbSize: []int{600, 175},
  91. SupportedExt: []string{".mp3", ".wav", ".ogg", ".flac"},
  92. })
  93. /*
  94. STL File Viewer - Plotted from ArOZ Online Beta
  95. */
  96. moduleHandler.RegisterModule(module.ModuleInfo{
  97. Name: "STL Viewer",
  98. Desc: "3D Model Viewer for STL Files",
  99. Group: "Utilities",
  100. IconPath: "SystemAO/utilities/img/stlViewer.png",
  101. Version: "1.0",
  102. SupportFW: false,
  103. SupportEmb: true,
  104. LaunchEmb: "SystemAO/utilities/stlViewer.html",
  105. InitEmbSize: []int{720, 480},
  106. SupportedExt: []string{".stl"},
  107. })
  108. /*
  109. Gcode File Viewer - Plotted from ArOZ Online Beta
  110. */
  111. moduleHandler.RegisterModule(module.ModuleInfo{
  112. Name: "Gcode Viewer",
  113. Desc: "Gcode Toolpath Viewer",
  114. Group: "Utilities",
  115. IconPath: "SystemAO/utilities/img/gcodeViewer.png",
  116. Version: "1.0",
  117. SupportFW: false,
  118. SupportEmb: true,
  119. LaunchEmb: "SystemAO/utilities/gcodeViewer.html",
  120. InitEmbSize: []int{720, 480},
  121. SupportedExt: []string{".gcode", ".gco"},
  122. })
  123. /*
  124. Basic Timer
  125. */
  126. moduleHandler.RegisterModule(module.ModuleInfo{
  127. Name: "Timer",
  128. Desc: "Basic Timer Utility",
  129. Group: "Utilities",
  130. IconPath: "SystemAO/utilities/img/timer.png",
  131. StartDir: "SystemAO/utilities/timer.html",
  132. Version: "1.0",
  133. SupportFW: true,
  134. SupportEmb: false,
  135. LaunchFWDir: "SystemAO/utilities/timer.html",
  136. InitFWSize: []int{380, 190},
  137. })
  138. }
  139. /*
  140. Util functions
  141. Please put the functions in the space below
  142. */
  143. /*
  144. Notebook function handlers
  145. Handle save of new notebook text file
  146. */
  147. func system_util_handleNotebookSave(w http.ResponseWriter, r *http.Request) {
  148. username, err := authAgent.GetUserName(w, r)
  149. if err != nil {
  150. sendErrorResponse(w, "User not logged in")
  151. return
  152. }
  153. userinfo, _ := userHandler.GetUserInfoFromUsername(username)
  154. filepath, _ := mv(r, "filepath", true)
  155. newcontent, _ := mv(r, "content", true)
  156. if filepath == "" {
  157. sendErrorResponse(w, "Undefined filepath given.")
  158. return
  159. }
  160. //Check if user can write
  161. if !userinfo.CanWrite(filepath) {
  162. sendErrorResponse(w, "Write request denied")
  163. return
  164. }
  165. //Get real path of file
  166. realpath, _ := userinfo.VirtualPathToRealPath(filepath)
  167. //Check if file exists. If yes, remove its ownership and size allocation
  168. if fileExists(realpath) {
  169. userinfo.RemoveOwnershipFromFile(realpath)
  170. }
  171. if userinfo.StorageQuota.HaveSpace(int64(len(newcontent))) {
  172. //have space. Set this file to the owner's
  173. userinfo.RemoveOwnershipFromFile(realpath)
  174. } else {
  175. //Out of space. Add this file back to the user ownership
  176. userinfo.SetOwnerOfFile(realpath)
  177. sendErrorResponse(w, "Storage Quota Fulled")
  178. return
  179. }
  180. err = ioutil.WriteFile(realpath, []byte(newcontent), 0755)
  181. if err != nil {
  182. sendErrorResponse(w, err.Error())
  183. return
  184. }
  185. userinfo.SetOwnerOfFile(realpath)
  186. sendOK(w)
  187. }