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

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