folder.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package vfs
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/drakkan/sftpgo/utils"
  7. )
  8. // BaseVirtualFolder defines the path for the virtual folder and the used quota limits.
  9. // The same folder can be shared among multiple users and each user can have different
  10. // quota limits or a different virtual path.
  11. type BaseVirtualFolder struct {
  12. ID int64 `json:"id"`
  13. Name string `json:"name"`
  14. MappedPath string `json:"mapped_path,omitempty"`
  15. UsedQuotaSize int64 `json:"used_quota_size"`
  16. // Used quota as number of files
  17. UsedQuotaFiles int `json:"used_quota_files"`
  18. // Last quota update as unix timestamp in milliseconds
  19. LastQuotaUpdate int64 `json:"last_quota_update"`
  20. // list of usernames associated with this virtual folder
  21. Users []string `json:"users,omitempty"`
  22. }
  23. // GetACopy returns a copy
  24. func (v *BaseVirtualFolder) GetACopy() BaseVirtualFolder {
  25. users := make([]string, len(v.Users))
  26. copy(users, v.Users)
  27. return BaseVirtualFolder{
  28. ID: v.ID,
  29. Name: v.Name,
  30. MappedPath: v.MappedPath,
  31. UsedQuotaSize: v.UsedQuotaSize,
  32. UsedQuotaFiles: v.UsedQuotaFiles,
  33. LastQuotaUpdate: v.LastQuotaUpdate,
  34. Users: users,
  35. }
  36. }
  37. // GetUsersAsString returns the list of users as comma separated string
  38. func (v *BaseVirtualFolder) GetUsersAsString() string {
  39. return strings.Join(v.Users, ",")
  40. }
  41. // GetQuotaSummary returns used quota and last update as string
  42. func (v *BaseVirtualFolder) GetQuotaSummary() string {
  43. var result string
  44. result = "Files: " + strconv.Itoa(v.UsedQuotaFiles)
  45. if v.UsedQuotaSize > 0 {
  46. result += ". Size: " + utils.ByteCountIEC(v.UsedQuotaSize)
  47. }
  48. if v.LastQuotaUpdate > 0 {
  49. t := utils.GetTimeFromMsecSinceEpoch(v.LastQuotaUpdate)
  50. result += fmt.Sprintf(". Last update: %v ", t.Format("2006-01-02 15:04:05")) // YYYY-MM-DD HH:MM:SS
  51. }
  52. return result
  53. }
  54. // VirtualFolder defines a mapping between an SFTPGo exposed virtual path and a
  55. // filesystem path outside the user home directory.
  56. // The specified paths must be absolute and the virtual path cannot be "/",
  57. // it must be a sub directory. The parent directory for the specified virtual
  58. // path must exist. SFTPGo will try to automatically create any missing
  59. // parent directory for the configured virtual folders at user login.
  60. type VirtualFolder struct {
  61. BaseVirtualFolder
  62. VirtualPath string `json:"virtual_path"`
  63. // Maximum size allowed as bytes. 0 means unlimited, -1 included in user quota
  64. QuotaSize int64 `json:"quota_size"`
  65. // Maximum number of files allowed. 0 means unlimited, -1 included in user quota
  66. QuotaFiles int `json:"quota_files"`
  67. }
  68. // IsIncludedInUserQuota returns true if the virtual folder is included in user quota
  69. func (v *VirtualFolder) IsIncludedInUserQuota() bool {
  70. return v.QuotaFiles == -1 && v.QuotaSize == -1
  71. }
  72. // HasNoQuotaRestrictions returns true if no quota restrictions need to be applyed
  73. func (v *VirtualFolder) HasNoQuotaRestrictions(checkFiles bool) bool {
  74. if v.QuotaSize == 0 && (!checkFiles || v.QuotaFiles == 0) {
  75. return true
  76. }
  77. return false
  78. }