util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package util
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "time"
  12. "github.com/minio/minio-go/v7"
  13. )
  14. func DrainChannelBuffer[T any](ch <-chan T) (ok bool) {
  15. for {
  16. select {
  17. case _, ok = <-ch:
  18. if !ok {
  19. return
  20. }
  21. default:
  22. return true
  23. }
  24. }
  25. }
  26. func FmtMyTime(t time.Time) string {
  27. return t.Format("2006-01-02 15:04:05.000")
  28. }
  29. func FmtMyDate(t time.Time) string {
  30. return t.Format("2006-01-02")
  31. }
  32. func MinioObjectExists(minioClient *minio.Client, bucketName string, filePath string) (bool, error) {
  33. _, err := minioClient.StatObject(context.Background(), bucketName, filePath, minio.StatObjectOptions{})
  34. if err != nil {
  35. if minio.ToErrorResponse(err).Code == "NoSuchKey" {
  36. return false, nil
  37. }
  38. return false, fmt.Errorf("stat object failed: %w", err)
  39. }
  40. return true, nil
  41. }
  42. func MinioCreateFolderIfNotExists(minioClient *minio.Client, bucketName string, filePath string) error {
  43. exists, err := MinioObjectExists(minioClient, bucketName, filePath)
  44. if err != nil {
  45. return fmt.Errorf("check object exists failed: %w", err)
  46. }
  47. if exists {
  48. return nil
  49. }
  50. _, err = minioClient.PutObject(context.Background(), bucketName, filePath, nil, 0, minio.PutObjectOptions{})
  51. if err != nil {
  52. return fmt.Errorf("put object failed: %w", err)
  53. }
  54. return nil
  55. }
  56. func MinioUploadFile(minioClient *minio.Client, bucketName string, objPath string, localPath string, overwrite bool) (uploaded bool, err error) {
  57. if !overwrite {
  58. // First check if the object exists
  59. exists, err := MinioObjectExists(minioClient, bucketName, objPath)
  60. if err != nil {
  61. return false, fmt.Errorf("check object exists failed: %w", err)
  62. }
  63. if exists {
  64. return false, nil
  65. }
  66. }
  67. // Then upload the file
  68. _, err = minioClient.FPutObject(context.Background(), bucketName, objPath, localPath,
  69. minio.PutObjectOptions{ContentType: "application/octet-stream"})
  70. if err != nil {
  71. return false, fmt.Errorf("fput object failed: %w", err)
  72. }
  73. return true, nil
  74. }
  75. // Should only be used for small files
  76. func MinioUploadFileIfChanged(minioClient *minio.Client, bucketName string, objPath string, localPath string) (uploaded bool, err error) {
  77. // First check if the object exists
  78. exists, err := MinioObjectExists(minioClient, bucketName, objPath)
  79. if err != nil {
  80. return false, fmt.Errorf("check object exists failed: %w", err)
  81. }
  82. // Then check if the file has changed by comparing the content
  83. if exists {
  84. isUnchanged, err := func() (bool, error) {
  85. // Get the object
  86. obj, err := minioClient.GetObject(context.Background(), bucketName, objPath, minio.GetObjectOptions{})
  87. if err != nil {
  88. return false, fmt.Errorf("get object failed: %w", err)
  89. }
  90. defer obj.Close()
  91. // Compare the content (io.ReadAll)
  92. localFile, err := os.Open(localPath)
  93. if err != nil {
  94. return false, fmt.Errorf("open local file failed: %w", err)
  95. }
  96. defer localFile.Close()
  97. objContent, err := io.ReadAll(obj)
  98. if err != nil {
  99. return false, fmt.Errorf("read object content failed: %w", err)
  100. }
  101. localContent, err := io.ReadAll(localFile)
  102. if err != nil {
  103. return false, fmt.Errorf("read local content failed: %w", err)
  104. }
  105. if bytes.Equal(objContent, localContent) {
  106. return true, nil
  107. }
  108. return false, nil
  109. }()
  110. if err != nil {
  111. return false, err
  112. }
  113. if isUnchanged {
  114. return false, nil
  115. }
  116. }
  117. // Then upload the file
  118. _, err = minioClient.FPutObject(context.Background(), bucketName, objPath, localPath,
  119. minio.PutObjectOptions{ContentType: "application/octet-stream"})
  120. if err != nil {
  121. return false, fmt.Errorf("fput object failed: %w", err)
  122. }
  123. return true, nil
  124. }
  125. func MinioUploadFolder(minioClient *minio.Client, bucketName string, objPath string, localPath string) error {
  126. // Walk the local folder
  127. err := filepath.WalkDir(localPath, func(path string, d os.DirEntry, err error) error {
  128. if err != nil {
  129. return fmt.Errorf("walk dir failed: %w", err)
  130. }
  131. if d.IsDir() {
  132. return nil
  133. }
  134. relPath, err := filepath.Rel(localPath, path)
  135. if err != nil {
  136. return fmt.Errorf("get relative path failed: %w", err)
  137. }
  138. targetPath := filepath.Join(objPath, relPath)
  139. _, err = minioClient.FPutObject(context.Background(), bucketName, targetPath, path,
  140. minio.PutObjectOptions{ContentType: "application/octet-stream"})
  141. if err != nil {
  142. return fmt.Errorf("fput object failed: %w", err)
  143. }
  144. return nil
  145. })
  146. return err
  147. }
  148. func GetCurrentNanoTimestampUTC() int64 {
  149. return time.Now().UnixNano()
  150. }
  151. func ExtractNumberFromString(filename string) (int64, error) {
  152. re := regexp.MustCompile(`(\d+)`)
  153. match := re.FindStringSubmatch(filename)
  154. if len(match) < 2 {
  155. return 0, fmt.Errorf("number not found in filename `%s`", filename)
  156. }
  157. return strconv.ParseInt(match[1], 10, 64)
  158. }