GraphQL API and utilities for the rpdata project
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.

97 lines
2.4 KiB

  1. package store
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. minio "github.com/minio/minio-go"
  8. )
  9. var spaceBucket string
  10. var spaceURLRoot string
  11. var spaceRoot string
  12. var spaceClient *minio.Client
  13. var spaceMaxSize int64
  14. // ConnectSpace connects to a S3 space.
  15. func ConnectSpace(host, accessKey, secretKey, bucket string, maxSize int64, rootDirectory string) error {
  16. client, err := minio.New(host, accessKey, secretKey, true)
  17. if err != nil {
  18. return err
  19. }
  20. exists, err := client.BucketExists(bucket)
  21. if err != nil {
  22. return err
  23. }
  24. if !exists {
  25. return errors.New("Bucket not found")
  26. }
  27. spaceClient = client
  28. spaceBucket = bucket
  29. spaceURLRoot = fmt.Sprintf("https://%s.%s/%s/", bucket, host, rootDirectory)
  30. spaceMaxSize = maxSize
  31. spaceRoot = rootDirectory
  32. return nil
  33. }
  34. // UploadFile uploads the file to the space. This does not do any checks on it, so the endpoints should
  35. // ensure that's all okay.
  36. func UploadFile(ctx context.Context, folder string, name string, mimeType string, reader io.Reader, size int64) (string, error) {
  37. if spaceClient == nil {
  38. return "", errors.New("This functionality is not enabled")
  39. }
  40. path := folder + "/" + name
  41. if size > spaceMaxSize {
  42. return "", errors.New("File is too big")
  43. }
  44. _, err := spaceClient.PutObjectWithContext(ctx, spaceBucket, spaceRoot+"/"+path, reader, size, minio.PutObjectOptions{
  45. ContentType: mimeType,
  46. UserMetadata: map[string]string{
  47. "x-amz-acl": "public-read",
  48. },
  49. })
  50. if err != nil {
  51. return "", err
  52. }
  53. _, err = spaceClient.StatObject(spaceBucket, spaceRoot+"/"+path, minio.StatObjectOptions{})
  54. if err != nil {
  55. return "", err
  56. }
  57. return path, nil
  58. }
  59. // RemoveFile removes a file from the space
  60. func RemoveFile(folder string, name string) error {
  61. if spaceClient == nil {
  62. return errors.New("This functionality is not enabled")
  63. }
  64. path := folder + "/" + name
  65. return spaceClient.RemoveObject(spaceBucket, spaceRoot+"/"+path)
  66. }
  67. // DownloadFile opens a file for download, using the same path format as the UploadFile function. Remember to Close it!
  68. func DownloadFile(ctx context.Context, path string) (io.ReadCloser, error) {
  69. if spaceClient == nil {
  70. return nil, errors.New("This functionality is not enabled")
  71. }
  72. return spaceClient.GetObjectWithContext(ctx, spaceBucket, spaceRoot+"/"+path, minio.GetObjectOptions{})
  73. }
  74. // URLFromPath gets the URL from the path returned by UploadFile
  75. func URLFromPath(path string) string {
  76. return spaceURLRoot + path
  77. }