quota.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 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. }
  89. //Get all the users file and see how
  90. func system_disk_quota_handleFileDistributionView(w http.ResponseWriter, r *http.Request) {
  91. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  92. if err != nil {
  93. sendErrorResponse(w, "Unknown User")
  94. return
  95. }
  96. fileDist := map[string]int64{}
  97. userFileSystemHandlers := userinfo.GetAllFileSystemHandler()
  98. for _, thisHandler := range userFileSystemHandlers {
  99. if thisHandler.Hierarchy == "user" {
  100. thispath := filepath.ToSlash(filepath.Clean(thisHandler.Path)) + "/users/" + userinfo.Username + "/"
  101. filepath.Walk(thispath, func(filepath string, info os.FileInfo, err error) error {
  102. if err != nil {
  103. return err
  104. }
  105. if !info.IsDir() {
  106. mime, _, err := fs.GetMime(filepath)
  107. if err != nil {
  108. return err
  109. }
  110. mediaType := strings.SplitN(mime, "/", 2)[0]
  111. mediaType = strings.Title(mediaType)
  112. fileDist[mediaType] = fileDist[mediaType] + info.Size()
  113. }
  114. return err
  115. })
  116. }
  117. }
  118. //Sort the file according to the number of files in the
  119. type kv struct {
  120. Mime string
  121. Size int64
  122. }
  123. var ss []kv
  124. for k, v := range fileDist {
  125. ss = append(ss, kv{k, v})
  126. }
  127. sort.Slice(ss, func(i, j int) bool {
  128. return ss[i].Size > ss[j].Size
  129. })
  130. //Return the distrubution using json string
  131. jsonString, _ := json.Marshal(ss)
  132. sendJSONResponse(w, string(jsonString))
  133. }