folder.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. MappedPath string `json:"mapped_path"`
  14. UsedQuotaSize int64 `json:"used_quota_size"`
  15. // Used quota as number of files
  16. UsedQuotaFiles int `json:"used_quota_files"`
  17. // Last quota update as unix timestamp in milliseconds
  18. LastQuotaUpdate int64 `json:"last_quota_update"`
  19. // list of usernames associated with this virtual folder
  20. Users []string `json:"users,omitempty"`
  21. }
  22. // GetUsersAsString returns the list of users as comma separated string
  23. func (v *BaseVirtualFolder) GetUsersAsString() string {
  24. return strings.Join(v.Users, ",")
  25. }
  26. // GetQuotaSummary returns used quota and last update as string
  27. func (v *BaseVirtualFolder) GetQuotaSummary() string {
  28. var result string
  29. result = "Files: " + strconv.Itoa(v.UsedQuotaFiles)
  30. if v.UsedQuotaSize > 0 {
  31. result += ". Size: " + utils.ByteCountSI(v.UsedQuotaSize)
  32. }
  33. if v.LastQuotaUpdate > 0 {
  34. t := utils.GetTimeFromMsecSinceEpoch(v.LastQuotaUpdate)
  35. result += fmt.Sprintf(". Last update: %v ", t.Format("2006-01-02 15:04:05")) // YYYY-MM-DD HH:MM:SS
  36. }
  37. return result
  38. }
  39. // VirtualFolder defines a mapping between a SFTP/SCP virtual path and a
  40. // filesystem path outside the user home directory.
  41. // The specified paths must be absolute and the virtual path cannot be "/",
  42. // it must be a sub directory. The parent directory for the specified virtual
  43. // path must exist. SFTPGo will try to automatically create any missing
  44. // parent directory for the configured virtual folders at user login.
  45. type VirtualFolder struct {
  46. BaseVirtualFolder
  47. VirtualPath string `json:"virtual_path"`
  48. // Maximum size allowed as bytes. 0 means unlimited, -1 included in user quota
  49. QuotaSize int64 `json:"quota_size"`
  50. // Maximum number of files allowed. 0 means unlimited, -1 included in user quota
  51. QuotaFiles int `json:"quota_files"`
  52. }
  53. // IsIncludedInUserQuota returns true if the virtual folder is included in user quota
  54. func (v *VirtualFolder) IsIncludedInUserQuota() bool {
  55. return v.QuotaFiles == -1 && v.QuotaSize == -1
  56. }
  57. // HasNoQuotaRestrictions returns true if no quota restrictions need to be applyed
  58. func (v *VirtualFolder) HasNoQuotaRestrictions(checkFiles bool) bool {
  59. if v.QuotaSize == 0 && (!checkFiles || v.QuotaFiles == 0) {
  60. return true
  61. }
  62. return false
  63. }