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.6 kB

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