diskcapacity.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/filesystem"
  9. "imuslab.com/arozos/mod/user"
  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. common.SendErrorResponse(w, "User not logged in")
  39. return
  40. }
  41. //Get vpath from paramter
  42. vpath, err := common.Mv(r, "path", true)
  43. if err != nil {
  44. common.SendErrorResponse(w, "Vpath is not defined")
  45. return
  46. }
  47. capinfo, err := cr.ResolveCapacityInfo(userinfo.Username, vpath)
  48. if err != nil {
  49. common.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. common.SendJSONResponse(w, string(js))
  62. }
  63. func (cr *Resolver) ResolveCapacityInfo(username string, vpath string) (*CapacityInfo, error) {
  64. //Resolve the vpath for this user
  65. userinfo, err := cr.UserHandler.GetUserInfoFromUsername(username)
  66. if err != nil {
  67. return nil, err
  68. }
  69. fsh, err := userinfo.GetFileSystemHandlerFromVirtualPath(vpath)
  70. if err != nil {
  71. return nil, err
  72. }
  73. realpath, err := fsh.FileSystemAbstraction.VirtualPathToRealPath(vpath, username)
  74. if err != nil {
  75. return nil, err
  76. }
  77. realpath = filepath.ToSlash(filepath.Clean(realpath))
  78. if filesystem.FileExists(realpath) && !fsh.RequireBuffer {
  79. //This is a local disk
  80. capinfo, err := dftool.GetCapacityInfoFromPath(realpath)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &CapacityInfo{
  85. PhysicalDevice: capinfo.PhysicalDevice,
  86. FileSystemType: fsh.Filesystem,
  87. MountingHierarchy: fsh.Hierarchy,
  88. Used: capinfo.Used,
  89. Available: capinfo.Available,
  90. Total: capinfo.Total,
  91. }, nil
  92. } else {
  93. //This is a remote disk
  94. return &CapacityInfo{
  95. PhysicalDevice: fsh.Path,
  96. FileSystemType: fsh.Filesystem,
  97. MountingHierarchy: fsh.Hierarchy,
  98. Used: 0,
  99. Available: 0,
  100. Total: 0,
  101. }, nil
  102. }
  103. }