quota.go 4.1 KB

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