quota.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "strings"
  10. fs "imuslab.com/arozos/mod/filesystem"
  11. //user "imuslab.com/arozos/mod/user"
  12. )
  13. func DiskQuotaInit() {
  14. //Register Endpoints
  15. http.HandleFunc("/system/disk/quota/setQuota", system_disk_quota_setQuota)
  16. http.HandleFunc("/system/disk/quota/quotaInfo", system_disk_quota_handleQuotaInfo)
  17. http.HandleFunc("/system/disk/quota/quotaDist", system_disk_quota_handleFileDistributionView)
  18. //Register Setting Interfaces
  19. registerSetting(settingModule{
  20. Name: "Storage Quota",
  21. Desc: "User Remaining Space",
  22. IconPath: "SystemAO/disk/quota/img/small_icon.png",
  23. Group: "Disk",
  24. StartDir: "SystemAO/disk/quota/quota.system",
  25. })
  26. //Register the timer for running the global user quota recalculation
  27. nightlyManager.RegisterNightlyTask(system_disk_quota_updateAllUserQuotaEstimation)
  28. }
  29. //Register the handler for automatically updating all user storage quota
  30. func system_disk_quota_updateAllUserQuotaEstimation() {
  31. registeredUsers := authAgent.ListUsers()
  32. for _, username := range registeredUsers {
  33. //For each user, update their current quota usage
  34. userinfo, _ := userHandler.GetUserInfoFromUsername(username)
  35. userinfo.StorageQuota.CalculateQuotaUsage()
  36. }
  37. }
  38. //Set the storage quota of the particular user
  39. func system_disk_quota_setQuota(w http.ResponseWriter, r *http.Request) {
  40. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  41. if err != nil {
  42. sendErrorResponse(w, "Unknown User")
  43. return
  44. }
  45. //Check if admin
  46. if !userinfo.IsAdmin() {
  47. sendErrorResponse(w, "Permission Denied")
  48. return
  49. }
  50. groupname, err := mv(r, "groupname", true)
  51. if err != nil {
  52. sendErrorResponse(w, "Group name not defned")
  53. return
  54. }
  55. quotaSizeString, err := mv(r, "quota", true)
  56. if err != nil {
  57. sendErrorResponse(w, "Quota not defined")
  58. return
  59. }
  60. quotaSize, err := StringToInt64(quotaSizeString)
  61. if err != nil || quotaSize < 0 {
  62. sendErrorResponse(w, "Invalid quota size given")
  63. return
  64. }
  65. //Qutasize unit is in MB
  66. quotaSize = quotaSize << 20
  67. log.Println("Updating "+groupname+" to ", quotaSize, "WIP")
  68. sendOK(w)
  69. }
  70. func system_disk_quota_handleQuotaInfo(w http.ResponseWriter, r *http.Request) {
  71. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  72. if err != nil {
  73. sendErrorResponse(w, "Unknown User")
  74. return
  75. }
  76. //Get quota information
  77. type quotaInformation struct {
  78. Remaining int64
  79. Used int64
  80. Total int64
  81. }
  82. jsonString, _ := json.Marshal(quotaInformation{
  83. Remaining: userinfo.StorageQuota.TotalStorageQuota - userinfo.StorageQuota.UsedStorageQuota,
  84. Used: userinfo.StorageQuota.UsedStorageQuota,
  85. Total: userinfo.StorageQuota.TotalStorageQuota,
  86. })
  87. sendJSONResponse(w, string(jsonString))
  88. go func() {
  89. //Update this user's quota estimation in go routine
  90. userinfo.StorageQuota.CalculateQuotaUsage()
  91. }()
  92. }
  93. //Get all the users file and see how
  94. func system_disk_quota_handleFileDistributionView(w http.ResponseWriter, r *http.Request) {
  95. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  96. if err != nil {
  97. sendErrorResponse(w, "Unknown User")
  98. return
  99. }
  100. fileDist := map[string]int64{}
  101. userFileSystemHandlers := userinfo.GetAllFileSystemHandler()
  102. for _, thisHandler := range userFileSystemHandlers {
  103. if thisHandler.Hierarchy == "user" {
  104. thispath := filepath.ToSlash(filepath.Clean(thisHandler.Path)) + "/users/" + userinfo.Username + "/"
  105. filepath.Walk(thispath, func(filepath string, info os.FileInfo, err error) error {
  106. if err != nil {
  107. return err
  108. }
  109. if !info.IsDir() {
  110. mime, _, err := fs.GetMime(filepath)
  111. if err != nil {
  112. return err
  113. }
  114. mediaType := strings.SplitN(mime, "/", 2)[0]
  115. mediaType = strings.Title(mediaType)
  116. fileDist[mediaType] = fileDist[mediaType] + info.Size()
  117. }
  118. return err
  119. })
  120. }
  121. }
  122. //Sort the file according to the number of files in the
  123. type kv struct {
  124. Mime string
  125. Size int64
  126. }
  127. var ss []kv
  128. for k, v := range fileDist {
  129. ss = append(ss, kv{k, v})
  130. }
  131. sort.Slice(ss, func(i, j int) bool {
  132. return ss[i].Size > ss[j].Size
  133. })
  134. //Return the distrubution using json string
  135. jsonString, _ := json.Marshal(ss)
  136. sendJSONResponse(w, string(jsonString))
  137. }