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

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