folder.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package vfs
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strconv"
  6. "strings"
  7. "github.com/drakkan/sftpgo/utils"
  8. )
  9. // BaseVirtualFolder defines the path for the virtual folder and the used quota limits.
  10. // The same folder can be shared among multiple users and each user can have different
  11. // quota limits or a different virtual path.
  12. type BaseVirtualFolder struct {
  13. ID int64 `json:"id"`
  14. Name string `json:"name"`
  15. MappedPath string `json:"mapped_path,omitempty"`
  16. Description string `json:"description,omitempty"`
  17. UsedQuotaSize int64 `json:"used_quota_size"`
  18. // Used quota as number of files
  19. UsedQuotaFiles int `json:"used_quota_files"`
  20. // Last quota update as unix timestamp in milliseconds
  21. LastQuotaUpdate int64 `json:"last_quota_update"`
  22. // list of usernames associated with this virtual folder
  23. Users []string `json:"users,omitempty"`
  24. // Filesystem configuration details
  25. FsConfig Filesystem `json:"filesystem"`
  26. }
  27. // GetEncrytionAdditionalData returns the additional data to use for AEAD
  28. func (v *BaseVirtualFolder) GetEncrytionAdditionalData() string {
  29. return fmt.Sprintf("folder_%v", v.Name)
  30. }
  31. // GetGCSCredentialsFilePath returns the path for GCS credentials
  32. func (v *BaseVirtualFolder) GetGCSCredentialsFilePath() string {
  33. return filepath.Join(credentialsDirPath, "folders", fmt.Sprintf("%v_gcs_credentials.json", v.Name))
  34. }
  35. // GetACopy returns a copy
  36. func (v *BaseVirtualFolder) GetACopy() BaseVirtualFolder {
  37. users := make([]string, len(v.Users))
  38. copy(users, v.Users)
  39. return BaseVirtualFolder{
  40. ID: v.ID,
  41. Name: v.Name,
  42. Description: v.Description,
  43. MappedPath: v.MappedPath,
  44. UsedQuotaSize: v.UsedQuotaSize,
  45. UsedQuotaFiles: v.UsedQuotaFiles,
  46. LastQuotaUpdate: v.LastQuotaUpdate,
  47. Users: users,
  48. FsConfig: v.FsConfig.GetACopy(),
  49. }
  50. }
  51. // GetUsersAsString returns the list of users as comma separated string
  52. func (v *BaseVirtualFolder) GetUsersAsString() string {
  53. return strings.Join(v.Users, ",")
  54. }
  55. // GetQuotaSummary returns used quota and last update as string
  56. func (v *BaseVirtualFolder) GetQuotaSummary() string {
  57. var result string
  58. result = "Files: " + strconv.Itoa(v.UsedQuotaFiles)
  59. if v.UsedQuotaSize > 0 {
  60. result += ". Size: " + utils.ByteCountIEC(v.UsedQuotaSize)
  61. }
  62. if v.LastQuotaUpdate > 0 {
  63. t := utils.GetTimeFromMsecSinceEpoch(v.LastQuotaUpdate)
  64. result += fmt.Sprintf(". Last update: %v ", t.Format("2006-01-02 15:04:05")) // YYYY-MM-DD HH:MM:SS
  65. }
  66. return result
  67. }
  68. // IsLocalOrLocalCrypted returns true if the folder provider is local or local encrypted
  69. func (v *BaseVirtualFolder) IsLocalOrLocalCrypted() bool {
  70. return v.FsConfig.Provider == LocalFilesystemProvider || v.FsConfig.Provider == CryptedFilesystemProvider
  71. }
  72. // HideConfidentialData hides folder confidential data
  73. func (v *BaseVirtualFolder) HideConfidentialData() {
  74. switch v.FsConfig.Provider {
  75. case S3FilesystemProvider:
  76. v.FsConfig.S3Config.AccessSecret.Hide()
  77. case GCSFilesystemProvider:
  78. v.FsConfig.GCSConfig.Credentials.Hide()
  79. case AzureBlobFilesystemProvider:
  80. v.FsConfig.AzBlobConfig.AccountKey.Hide()
  81. case CryptedFilesystemProvider:
  82. v.FsConfig.CryptConfig.Passphrase.Hide()
  83. case SFTPFilesystemProvider:
  84. v.FsConfig.SFTPConfig.Password.Hide()
  85. v.FsConfig.SFTPConfig.PrivateKey.Hide()
  86. }
  87. }
  88. // HasRedactedSecret returns true if the folder has a redacted secret
  89. func (v *BaseVirtualFolder) HasRedactedSecret() bool {
  90. switch v.FsConfig.Provider {
  91. case S3FilesystemProvider:
  92. if v.FsConfig.S3Config.AccessSecret.IsRedacted() {
  93. return true
  94. }
  95. case GCSFilesystemProvider:
  96. if v.FsConfig.GCSConfig.Credentials.IsRedacted() {
  97. return true
  98. }
  99. case AzureBlobFilesystemProvider:
  100. if v.FsConfig.AzBlobConfig.AccountKey.IsRedacted() {
  101. return true
  102. }
  103. case CryptedFilesystemProvider:
  104. if v.FsConfig.CryptConfig.Passphrase.IsRedacted() {
  105. return true
  106. }
  107. case SFTPFilesystemProvider:
  108. if v.FsConfig.SFTPConfig.Password.IsRedacted() {
  109. return true
  110. }
  111. if v.FsConfig.SFTPConfig.PrivateKey.IsRedacted() {
  112. return true
  113. }
  114. }
  115. return false
  116. }
  117. // VirtualFolder defines a mapping between an SFTPGo exposed virtual path and a
  118. // filesystem path outside the user home directory.
  119. // The specified paths must be absolute and the virtual path cannot be "/",
  120. // it must be a sub directory. The parent directory for the specified virtual
  121. // path must exist. SFTPGo will try to automatically create any missing
  122. // parent directory for the configured virtual folders at user login.
  123. type VirtualFolder struct {
  124. BaseVirtualFolder
  125. VirtualPath string `json:"virtual_path"`
  126. // Maximum size allowed as bytes. 0 means unlimited, -1 included in user quota
  127. QuotaSize int64 `json:"quota_size"`
  128. // Maximum number of files allowed. 0 means unlimited, -1 included in user quota
  129. QuotaFiles int `json:"quota_files"`
  130. }
  131. func (v *VirtualFolder) GetFilesystem(connectionID string) (Fs, error) {
  132. switch v.FsConfig.Provider {
  133. case S3FilesystemProvider:
  134. return NewS3Fs(connectionID, v.MappedPath, v.VirtualPath, v.FsConfig.S3Config)
  135. case GCSFilesystemProvider:
  136. config := v.FsConfig.GCSConfig
  137. config.CredentialFile = v.GetGCSCredentialsFilePath()
  138. return NewGCSFs(connectionID, v.MappedPath, v.VirtualPath, config)
  139. case AzureBlobFilesystemProvider:
  140. return NewAzBlobFs(connectionID, v.MappedPath, v.VirtualPath, v.FsConfig.AzBlobConfig)
  141. case CryptedFilesystemProvider:
  142. return NewCryptFs(connectionID, v.MappedPath, v.VirtualPath, v.FsConfig.CryptConfig)
  143. case SFTPFilesystemProvider:
  144. return NewSFTPFs(connectionID, v.VirtualPath, v.FsConfig.SFTPConfig)
  145. default:
  146. return NewOsFs(connectionID, v.MappedPath, v.VirtualPath), nil
  147. }
  148. }
  149. // ScanQuota scans the folder and returns the number of files and their size
  150. func (v *VirtualFolder) ScanQuota() (int, int64, error) {
  151. fs, err := v.GetFilesystem("")
  152. if err != nil {
  153. return 0, 0, err
  154. }
  155. defer fs.Close()
  156. return fs.ScanRootDirContents()
  157. }
  158. // IsIncludedInUserQuota returns true if the virtual folder is included in user quota
  159. func (v *VirtualFolder) IsIncludedInUserQuota() bool {
  160. return v.QuotaFiles == -1 && v.QuotaSize == -1
  161. }
  162. // HasNoQuotaRestrictions returns true if no quota restrictions need to be applyed
  163. func (v *VirtualFolder) HasNoQuotaRestrictions(checkFiles bool) bool {
  164. if v.QuotaSize == 0 && (!checkFiles || v.QuotaFiles == 0) {
  165. return true
  166. }
  167. return false
  168. }
  169. // GetACopy returns a copy
  170. func (v *VirtualFolder) GetACopy() VirtualFolder {
  171. return VirtualFolder{
  172. BaseVirtualFolder: v.BaseVirtualFolder.GetACopy(),
  173. VirtualPath: v.VirtualPath,
  174. QuotaSize: v.QuotaSize,
  175. QuotaFiles: v.QuotaFiles,
  176. }
  177. }