dataDownloadRepo.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package minio
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "path"
  8. "strings"
  9. "time"
  10. minioUtil "fwh-toolbox-server/utils/minio"
  11. )
  12. const (
  13. renderPrefix = "DataDownloadRenderCache/"
  14. packagePrefix = "DataDownloadPackage/"
  15. binaryPrefix = "DataDownloadDocument/"
  16. )
  17. func PutRenderArtifact(ctx context.Context, documentID string, generation int64, format string, data []byte, contentType string) (string, error) {
  18. if strings.Contains(format, "/") || strings.Contains(format, "\\") {
  19. return "", fmt.Errorf("invalid format")
  20. }
  21. key := fmt.Sprintf("%s%s/%d/%d.%s", renderPrefix, documentID, generation, time.Now().UnixNano(), format)
  22. if err := minioUtil.UploadFromReader(bytes.NewReader(data), int64(len(data)), key, contentType); err != nil {
  23. return "", err
  24. }
  25. return key, nil
  26. }
  27. func PutPackage(ctx context.Context, name string, data []byte) (string, error) {
  28. key := fmt.Sprintf("%s%d-%s", packagePrefix, time.Now().UnixNano(), path.Base(name))
  29. if err := minioUtil.UploadFromReader(bytes.NewReader(data), int64(len(data)), key, "application/zip"); err != nil {
  30. return "", err
  31. }
  32. return key, nil
  33. }
  34. func PutBinaryDocument(ctx context.Context, documentID, filename string, reader io.Reader, size int64, contentType string) (string, error) {
  35. key := fmt.Sprintf("%s%s/%d-%s", binaryPrefix, documentID, time.Now().UnixNano(), path.Base(filename))
  36. if err := minioUtil.UploadFromReader(reader, size, key, contentType); err != nil {
  37. return "", err
  38. }
  39. return key, nil
  40. }
  41. func Open(ctx context.Context, objectKey string) (io.ReadCloser, error) {
  42. return minioUtil.GetObject(objectKey)
  43. }
  44. func Delete(ctx context.Context, objectKey string) error { return minioUtil.DeleteFile(objectKey) }