module.util.go 5.3 KB

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