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

storage.go 1.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "fmt"
  7. "io"
  8. "code.gitea.io/gitea/modules/setting"
  9. )
  10. // ObjectStorage represents an object storage to handle a bucket and files
  11. type ObjectStorage interface {
  12. Save(path string, r io.Reader) (int64, error)
  13. Open(path string) (io.ReadCloser, error)
  14. Delete(path string) error
  15. }
  16. // Copy copys a file from source ObjectStorage to dest ObjectStorage
  17. func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, srcPath string) (int64, error) {
  18. f, err := srcStorage.Open(srcPath)
  19. if err != nil {
  20. return 0, err
  21. }
  22. defer f.Close()
  23. return dstStorage.Save(dstPath, f)
  24. }
  25. var (
  26. // Attachments represents attachments storage
  27. Attachments ObjectStorage
  28. )
  29. // Init init the stoarge
  30. func Init() error {
  31. var err error
  32. switch setting.Attachment.StoreType {
  33. case "local":
  34. Attachments, err = NewLocalStorage(setting.Attachment.Path)
  35. case "minio":
  36. minio := setting.Attachment.Minio
  37. Attachments, err = NewMinioStorage(
  38. minio.Endpoint,
  39. minio.AccessKeyID,
  40. minio.SecretAccessKey,
  41. minio.Bucket,
  42. minio.Location,
  43. minio.BasePath,
  44. minio.UseSSL,
  45. )
  46. default:
  47. return fmt.Errorf("Unsupported attachment store type: %s", setting.Attachment.StoreType)
  48. }
  49. if err != nil {
  50. return err
  51. }
  52. return nil
  53. }