disk.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package main
  2. /*
  3. ArOZ Online Disk Service Endpoint Handler
  4. This is a module to provide access to the disk services
  5. */
  6. import (
  7. "net/http"
  8. "imuslab.com/arozos/mod/disk/diskcapacity"
  9. "imuslab.com/arozos/mod/disk/diskmg"
  10. diskspace "imuslab.com/arozos/mod/disk/diskspace"
  11. smart "imuslab.com/arozos/mod/disk/smart"
  12. sortfile "imuslab.com/arozos/mod/disk/sortfile"
  13. prout "imuslab.com/arozos/mod/prouter"
  14. "imuslab.com/arozos/mod/utils"
  15. )
  16. func DiskServiceInit() {
  17. //Register Disk Utilities under System Setting
  18. //Disk info are only viewable by administrator
  19. router := prout.NewModuleRouter(prout.RouterOption{
  20. ModuleName: "System Setting",
  21. AdminOnly: false,
  22. UserHandler: userHandler,
  23. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  24. utils.SendErrorResponse(w, "Permission Denied")
  25. },
  26. })
  27. //Anyone logged in can load router
  28. authRouter := prout.NewModuleRouter(prout.RouterOption{
  29. AdminOnly: false,
  30. UserHandler: userHandler,
  31. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  32. utils.SendErrorResponse(w, "Permission Denied")
  33. },
  34. })
  35. //Disk Space Display endpoint
  36. router.HandleFunc("/system/disk/space/list", diskspace.HandleDiskSpaceList)
  37. //Handle Virtual Disk Properties display endpoints
  38. dc := diskcapacity.NewCapacityResolver(userHandler)
  39. router.HandleFunc("/system/disk/space/resolve", dc.HandleCapacityResolving)
  40. authRouter.HandleFunc("/system/disk/space/tmp", dc.HandleTmpCapacityResolving)
  41. //New Large File Scanner
  42. lfs := sortfile.NewLargeFileScanner(userHandler)
  43. router.HandleFunc("/system/disk/space/largeFiles", lfs.HandleLargeFileList)
  44. //Register settings
  45. registerSetting(settingModule{
  46. Name: "Space Finder",
  47. Desc: "Reclaim Storage Space on Disks",
  48. IconPath: "SystemAO/disk/space/img/small_icon.png",
  49. Group: "Disk",
  50. StartDir: "SystemAO/disk/space/finder.html",
  51. RequireAdmin: false,
  52. })
  53. if *allow_hardware_management {
  54. //Displaying remaining space on disk, only enabled when allow hardware is true
  55. registerSetting(settingModule{
  56. Name: "Disk Space",
  57. Desc: "System Storage Space on Disks",
  58. IconPath: "SystemAO/disk/space/img/small_icon.png",
  59. Group: "Disk",
  60. StartDir: "SystemAO/disk/space/diskspace.html",
  61. RequireAdmin: false,
  62. })
  63. }
  64. //Register Disk SMART services
  65. if sudo_mode {
  66. //Create a new admin router
  67. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  68. ModuleName: "System Setting",
  69. AdminOnly: true,
  70. UserHandler: userHandler,
  71. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  72. utils.SendErrorResponse(w, "Permission Denied")
  73. },
  74. })
  75. /*
  76. SMART Listener
  77. Handle disk SMART and disk information
  78. See disk/SMART for more information
  79. */
  80. if *allow_hardware_management {
  81. smartListener, err := smart.NewSmartListener()
  82. if err != nil {
  83. //Listener creation failed
  84. systemWideLogger.PrintAndLog("Disk", "Failed to create SMART listener: "+err.Error(), err)
  85. } else {
  86. //Listener created. Register endpoints
  87. //Register as a system setting
  88. registerSetting(settingModule{
  89. Name: "Disk SMART",
  90. Desc: "HardDisk Health Checking",
  91. IconPath: "SystemAO/disk/smart/img/small_icon.png",
  92. Group: "Disk",
  93. StartDir: "SystemAO/disk/smart/smart.html",
  94. RequireAdmin: true,
  95. })
  96. /*
  97. registerSetting(settingModule{
  98. Name: "SMART Log",
  99. Desc: "HardDisk Health Log",
  100. IconPath: "SystemAO/disk/smart/img/small_icon.png",
  101. Group: "Disk",
  102. StartDir: "SystemAO/disk/smart/log.html",
  103. RequireAdmin: true,
  104. })
  105. */
  106. adminRouter.HandleFunc("/system/disk/smart/getSMART", smartListener.GetSMART)
  107. }
  108. }
  109. /*
  110. Disk Manager Initialization
  111. See disk/diskmg.go for more details
  112. For setting register, see setting.advance.go
  113. */
  114. if *allow_hardware_management {
  115. adminRouter.HandleFunc("/system/disk/diskmg/view", diskmg.HandleView)
  116. adminRouter.HandleFunc("/system/disk/diskmg/platform", diskmg.HandlePlatform)
  117. adminRouter.HandleFunc("/system/disk/diskmg/mount", func(w http.ResponseWriter, r *http.Request) {
  118. //Mount option require passing in all filesystem handlers
  119. allFsh := GetAllLoadedFsh()
  120. diskmg.HandleMount(w, r, allFsh)
  121. })
  122. adminRouter.HandleFunc("/system/disk/diskmg/format", func(w http.ResponseWriter, r *http.Request) {
  123. //Check if request are made in POST mode
  124. if r.Method != http.MethodPost {
  125. w.WriteHeader(http.StatusMethodNotAllowed)
  126. w.Write([]byte("405 - Method Not Allowed"))
  127. return
  128. }
  129. //Check if ArozOS is running in sudo mode
  130. if !sudo_mode {
  131. w.WriteHeader(http.StatusUnauthorized)
  132. w.Write([]byte("401 - Unauthorized (Is ArozOS running in sudo mode?)"))
  133. return
  134. }
  135. //Format option require passing in all filesystem handlers
  136. allFsh := GetAllLoadedFsh()
  137. diskmg.HandleFormat(w, r, allFsh)
  138. })
  139. adminRouter.HandleFunc("/system/disk/diskmg/mpt", diskmg.HandleListMountPoints)
  140. }
  141. }
  142. }