diskcapacity.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package diskcapacity
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "path/filepath"
  6. "imuslab.com/arozos/mod/disk/diskcapacity/dftool"
  7. "imuslab.com/arozos/mod/filesystem/arozfs"
  8. "imuslab.com/arozos/mod/user"
  9. "imuslab.com/arozos/mod/utils"
  10. )
  11. /*
  12. Disk Capacity
  13. This is a simple module to check how many storage space is remaining
  14. on a given directory in accessiable file system paths
  15. Author: tobychui
  16. */
  17. type Resolver struct {
  18. UserHandler *user.UserHandler
  19. }
  20. type CapacityInfo struct {
  21. PhysicalDevice string
  22. FileSystemType string
  23. MountingHierarchy string
  24. Used int64
  25. Available int64
  26. Total int64
  27. }
  28. // Create a new Capacity Resolver with the given user handler
  29. func NewCapacityResolver(u *user.UserHandler) *Resolver {
  30. return &Resolver{
  31. UserHandler: u,
  32. }
  33. }
  34. func (cr *Resolver) HandleCapacityResolving(w http.ResponseWriter, r *http.Request) {
  35. //Check if the request user is authenticated
  36. userinfo, err := cr.UserHandler.GetUserInfoFromRequest(w, r)
  37. if err != nil {
  38. utils.SendErrorResponse(w, "User not logged in")
  39. return
  40. }
  41. //Get vpath from paramter
  42. vpath, err := utils.PostPara(r, "path")
  43. if err != nil {
  44. utils.SendErrorResponse(w, "Vpath is not defined")
  45. return
  46. }
  47. capinfo, err := cr.ResolveCapacityInfo(userinfo.Username, vpath)
  48. if err != nil {
  49. utils.SendErrorResponse(w, "Unable to resolve path capacity information: "+err.Error())
  50. return
  51. }
  52. //Get Storage Hierarcy
  53. fsh, err := userinfo.GetFileSystemHandlerFromVirtualPath(vpath)
  54. if err != nil {
  55. capinfo.MountingHierarchy = "Unknown"
  56. } else {
  57. capinfo.MountingHierarchy = fsh.Hierarchy
  58. }
  59. //Send the requested path capacity information
  60. js, _ := json.Marshal(capinfo)
  61. utils.SendJSONResponse(w, string(js))
  62. }
  63. // Get the capacity in tmp folder, accessible by everyone by default
  64. func (cr *Resolver) HandleTmpCapacityResolving(w http.ResponseWriter, r *http.Request) {
  65. //Check if the request user is authenticated
  66. userinfo, err := cr.UserHandler.GetUserInfoFromRequest(w, r)
  67. if err != nil {
  68. utils.SendErrorResponse(w, "User not logged in")
  69. return
  70. }
  71. capinfo, _ := cr.ResolveCapacityInfo(userinfo.Username, "tmp:/")
  72. js, _ := json.Marshal(capinfo)
  73. utils.SendJSONResponse(w, string(js))
  74. }
  75. func (cr *Resolver) ResolveCapacityInfo(username string, vpath string) (*CapacityInfo, error) {
  76. //Resolve the vpath for this user
  77. userinfo, err := cr.UserHandler.GetUserInfoFromUsername(username)
  78. if err != nil {
  79. return nil, err
  80. }
  81. fsh, err := userinfo.GetFileSystemHandlerFromVirtualPath(vpath)
  82. if err != nil {
  83. return nil, err
  84. }
  85. realpath, err := fsh.FileSystemAbstraction.VirtualPathToRealPath(vpath, username)
  86. if err != nil {
  87. return nil, err
  88. }
  89. realpath = filepath.ToSlash(filepath.Clean(realpath))
  90. if utils.FileExists(realpath) && !arozfs.IsNetworkDrive(fsh.Filesystem) {
  91. //This is a local disk
  92. capinfo, err := dftool.GetCapacityInfoFromPath(realpath)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return &CapacityInfo{
  97. PhysicalDevice: capinfo.PhysicalDevice,
  98. FileSystemType: fsh.Filesystem,
  99. MountingHierarchy: fsh.Hierarchy,
  100. Used: capinfo.Used,
  101. Available: capinfo.Available,
  102. Total: capinfo.Total,
  103. }, nil
  104. } else {
  105. //This is a remote disk
  106. return &CapacityInfo{
  107. PhysicalDevice: fsh.Path,
  108. FileSystemType: fsh.Filesystem,
  109. MountingHierarchy: fsh.Hierarchy,
  110. Used: 0,
  111. Available: 0,
  112. Total: 0,
  113. }, nil
  114. }
  115. }