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

130 lines
3.0 KiB

  1. package space
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "strings"
  8. "github.com/minio/minio-go"
  9. )
  10. var ErrBucketNotFound = errors.New("bucket not found")
  11. var ErrFileTooBig = errors.New("file is too big")
  12. var ErrNotEnabled = errors.New("s3 connection not enabled")
  13. var ErrDifferentURLRoot = errors.New("url root in path is different from current")
  14. type Space struct {
  15. bucket string
  16. urlRoot string
  17. root string
  18. client *minio.Client
  19. maxSize int64
  20. }
  21. func Connect(host, accessKey, secretKey, bucket string, secure bool, maxSize int64, rootDirectory, urlRoot string) (*Space, error) {
  22. client, err := minio.New(host, accessKey, secretKey, secure)
  23. if err != nil {
  24. return nil, err
  25. }
  26. exists, err := client.BucketExists(bucket)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if !exists {
  31. return nil, ErrBucketNotFound
  32. }
  33. if urlRoot == "" {
  34. urlRoot = fmt.Sprintf("https://%s.%s/%s/", bucket, host, rootDirectory)
  35. } else if !strings.HasSuffix(urlRoot, "/") {
  36. urlRoot += "/"
  37. }
  38. space := &Space{
  39. client: client,
  40. bucket: bucket,
  41. urlRoot: urlRoot,
  42. maxSize: maxSize,
  43. root: rootDirectory,
  44. }
  45. return space, nil
  46. }
  47. // UploadFile uploads the file to the space. This does not do any checks on it, so the endpoints should
  48. // ensure that's all okay.
  49. func (space *Space) UploadFile(ctx context.Context, name string, mimeType string, reader io.Reader, size int64) error {
  50. if space == nil {
  51. return ErrNotEnabled
  52. }
  53. if size > space.maxSize {
  54. return ErrFileTooBig
  55. }
  56. _, err := space.client.PutObjectWithContext(ctx, space.bucket, space.root+"/"+name, reader, size, minio.PutObjectOptions{
  57. ContentType: mimeType,
  58. UserMetadata: map[string]string{
  59. "x-amz-acl": "public-read",
  60. },
  61. })
  62. if err != nil {
  63. return err
  64. }
  65. _, err = space.client.StatObject(space.bucket, space.root+"/"+name, minio.StatObjectOptions{})
  66. if err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. // RemoveFile removes a file from the space
  72. func (space *Space) RemoveFile(ctx context.Context, name string) error {
  73. if space == nil {
  74. return ErrNotEnabled
  75. }
  76. ch := make(chan string, 2)
  77. ch <- space.root + "/" + name
  78. close(ch)
  79. err := <-space.client.RemoveObjectsWithContext(ctx, space.bucket, ch)
  80. return err.Err
  81. }
  82. // DownloadFile opens a file for download, using the same path format as the UploadFile function. Remember to Close it!
  83. func (space *Space) DownloadFile(ctx context.Context, path string) (io.ReadCloser, error) {
  84. if space == nil {
  85. return nil, ErrNotEnabled
  86. }
  87. return space.client.GetObjectWithContext(ctx, space.bucket, space.root+"/"+path, minio.GetObjectOptions{})
  88. }
  89. // URLFromPath gets the URL from the path returned by UploadFile
  90. func (space *Space) URLFromPath(path string) string {
  91. if space == nil {
  92. return ""
  93. }
  94. return space.urlRoot + path
  95. }
  96. // URLFromPath gets the URL from the path returned by UploadFile
  97. func (space *Space) PathFromURL(url string) (string, error) {
  98. if space == nil {
  99. return "", ErrNotEnabled
  100. }
  101. if !strings.HasPrefix(url, space.urlRoot) {
  102. return "", ErrDifferentURLRoot
  103. }
  104. return url[len(space.urlRoot):], nil
  105. }