diskcapacity.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. //Create a new Capacity Resolver with the given user handler
  20. func NewCapacityResolver(u *user.UserHandler) *Resolver {
  21. return &Resolver{
  22. UserHandler: u,
  23. }
  24. }
  25. func (cr *Resolver) HandleCapacityResolving(w http.ResponseWriter, r *http.Request) {
  26. //Check if the request user is authenticated
  27. userinfo, err := cr.UserHandler.GetUserInfoFromRequest(w, r)
  28. if err != nil {
  29. common.SendErrorResponse(w, "User not logged in")
  30. return
  31. }
  32. //Get vpath from paramter
  33. vpath, err := common.Mv(r, "path", true)
  34. if err != nil {
  35. common.SendErrorResponse(w, "Vpath is not defined")
  36. return
  37. }
  38. capinfo, err := cr.ResolveCapacityInfo(userinfo.Username, vpath)
  39. if err != nil {
  40. common.SendErrorResponse(w, "Unable to resolve path capacity information: "+err.Error())
  41. return
  42. }
  43. //Get Storage Hierarcy
  44. fsh, err := userinfo.GetFileSystemHandlerFromVirtualPath(vpath)
  45. if err != nil {
  46. capinfo.MountingHierarchy = "Unknown"
  47. } else {
  48. capinfo.MountingHierarchy = fsh.Hierarchy
  49. }
  50. //Send the requested path capacity information
  51. js, _ := json.Marshal(capinfo)
  52. common.SendJSONResponse(w, string(js))
  53. }
  54. func (cr *Resolver) ResolveCapacityInfo(username string, vpath string) (*dftool.Capacity, error) {
  55. //Resolve the vpath for this user
  56. userinfo, err := cr.UserHandler.GetUserInfoFromUsername(username)
  57. if err != nil {
  58. return nil, err
  59. }
  60. realpath, err := userinfo.VirtualPathToRealPath(vpath)
  61. if err != nil {
  62. return nil, err
  63. }
  64. realpath = filepath.ToSlash(filepath.Clean(realpath))
  65. return dftool.GetCapacityInfoFromPath(realpath)
  66. }