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

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