diskcapacity.go 2.9 KB

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