You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package storage
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "net/url"
  11. "os"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. var (
  15. // ErrURLNotSupported represents url is not supported
  16. ErrURLNotSupported = errors.New("url method not supported")
  17. // ErrIterateObjectsNotSupported represents IterateObjects not supported
  18. ErrIterateObjectsNotSupported = errors.New("iterateObjects method not supported")
  19. )
  20. // ErrInvalidConfiguration is called when there is invalid configuration for a storage
  21. type ErrInvalidConfiguration struct {
  22. cfg interface{}
  23. err error
  24. }
  25. func (err ErrInvalidConfiguration) Error() string {
  26. if err.err != nil {
  27. return fmt.Sprintf("Invalid Configuration Argument: %v: Error: %v", err.cfg, err.err)
  28. }
  29. return fmt.Sprintf("Invalid Configuration Argument: %v", err.cfg)
  30. }
  31. // IsErrInvalidConfiguration checks if an error is an ErrInvalidConfiguration
  32. func IsErrInvalidConfiguration(err error) bool {
  33. _, ok := err.(ErrInvalidConfiguration)
  34. return ok
  35. }
  36. // Type is a type of Storage
  37. type Type string
  38. // NewStorageFunc is a function that creates a storage
  39. type NewStorageFunc func(ctx context.Context, cfg interface{}) (ObjectStorage, error)
  40. var storageMap = map[Type]NewStorageFunc{}
  41. // RegisterStorageType registers a provided storage type with a function to create it
  42. func RegisterStorageType(typ Type, fn func(ctx context.Context, cfg interface{}) (ObjectStorage, error)) {
  43. storageMap[typ] = fn
  44. }
  45. // Object represents the object on the storage
  46. type Object interface {
  47. io.ReadCloser
  48. io.Seeker
  49. Stat() (os.FileInfo, error)
  50. }
  51. // ObjectStorage represents an object storage to handle a bucket and files
  52. type ObjectStorage interface {
  53. Open(path string) (Object, error)
  54. Save(path string, r io.Reader) (int64, error)
  55. Stat(path string) (os.FileInfo, error)
  56. Delete(path string) error
  57. URL(path, name string) (*url.URL, error)
  58. IterateObjects(func(path string, obj Object) error) error
  59. }
  60. // Copy copys a file from source ObjectStorage to dest ObjectStorage
  61. func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, srcPath string) (int64, error) {
  62. f, err := srcStorage.Open(srcPath)
  63. if err != nil {
  64. return 0, err
  65. }
  66. defer f.Close()
  67. return dstStorage.Save(dstPath, f)
  68. }
  69. // SaveFrom saves data to the ObjectStorage with path p from the callback
  70. func SaveFrom(objStorage ObjectStorage, p string, callback func(w io.Writer) error) error {
  71. pr, pw := io.Pipe()
  72. defer pr.Close()
  73. go func() {
  74. defer pw.Close()
  75. if err := callback(pw); err != nil {
  76. _ = pw.CloseWithError(err)
  77. }
  78. }()
  79. _, err := objStorage.Save(p, pr)
  80. return err
  81. }
  82. var (
  83. // Attachments represents attachments storage
  84. Attachments ObjectStorage
  85. // LFS represents lfs storage
  86. LFS ObjectStorage
  87. // Avatars represents user avatars storage
  88. Avatars ObjectStorage
  89. // RepoAvatars represents repository avatars storage
  90. RepoAvatars ObjectStorage
  91. )
  92. // Init init the stoarge
  93. func Init() error {
  94. if err := initAttachments(); err != nil {
  95. return err
  96. }
  97. if err := initAvatars(); err != nil {
  98. return err
  99. }
  100. if err := initRepoAvatars(); err != nil {
  101. return err
  102. }
  103. return initLFS()
  104. }
  105. // NewStorage takes a storage type and some config and returns an ObjectStorage or an error
  106. func NewStorage(typStr string, cfg interface{}) (ObjectStorage, error) {
  107. if len(typStr) == 0 {
  108. typStr = string(LocalStorageType)
  109. }
  110. fn, ok := storageMap[Type(typStr)]
  111. if !ok {
  112. return nil, fmt.Errorf("Unsupported storage type: %s", typStr)
  113. }
  114. return fn(context.Background(), cfg)
  115. }
  116. func initAvatars() (err error) {
  117. Avatars, err = NewStorage(setting.Avatar.Storage.Type, setting.Avatar.Storage)
  118. return
  119. }
  120. func initAttachments() (err error) {
  121. Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage)
  122. return
  123. }
  124. func initLFS() (err error) {
  125. LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage)
  126. return
  127. }
  128. func initRepoAvatars() (err error) {
  129. RepoAvatars, err = NewStorage(setting.RepoAvatar.Storage.Type, setting.RepoAvatar.Storage)
  130. return
  131. }