123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package util
- import (
- "bytes"
- "context"
- "fmt"
- "io"
- "os"
- "path/filepath"
- "regexp"
- "strconv"
- "time"
- "github.com/minio/minio-go/v7"
- )
- func DrainChannelBuffer[T any](ch <-chan T) (ok bool) {
- for {
- select {
- case _, ok = <-ch:
- if !ok {
- return
- }
- default:
- return true
- }
- }
- }
- func FmtMyTime(t time.Time) string {
- return t.Format("2006-01-02 15:04:05.000")
- }
- func FmtMyDate(t time.Time) string {
- return t.Format("2006-01-02")
- }
- func MinioObjectExists(minioClient *minio.Client, bucketName string, filePath string) (bool, error) {
- _, err := minioClient.StatObject(context.Background(), bucketName, filePath, minio.StatObjectOptions{})
- if err != nil {
- if minio.ToErrorResponse(err).Code == "NoSuchKey" {
- return false, nil
- }
- return false, fmt.Errorf("stat object failed: %w", err)
- }
- return true, nil
- }
- func MinioCreateFolderIfNotExists(minioClient *minio.Client, bucketName string, filePath string) error {
- exists, err := MinioObjectExists(minioClient, bucketName, filePath)
- if err != nil {
- return fmt.Errorf("check object exists failed: %w", err)
- }
- if exists {
- return nil
- }
- _, err = minioClient.PutObject(context.Background(), bucketName, filePath, nil, 0, minio.PutObjectOptions{})
- if err != nil {
- return fmt.Errorf("put object failed: %w", err)
- }
- return nil
- }
- func MinioUploadFile(minioClient *minio.Client, bucketName string, objPath string, localPath string, overwrite bool) (uploaded bool, err error) {
- if !overwrite {
- // First check if the object exists
- exists, err := MinioObjectExists(minioClient, bucketName, objPath)
- if err != nil {
- return false, fmt.Errorf("check object exists failed: %w", err)
- }
- if exists {
- return false, nil
- }
- }
- // Then upload the file
- _, err = minioClient.FPutObject(context.Background(), bucketName, objPath, localPath,
- minio.PutObjectOptions{ContentType: "application/octet-stream"})
- if err != nil {
- return false, fmt.Errorf("fput object failed: %w", err)
- }
- return true, nil
- }
- // Should only be used for small files
- func MinioUploadFileIfChanged(minioClient *minio.Client, bucketName string, objPath string, localPath string) (uploaded bool, err error) {
- // First check if the object exists
- exists, err := MinioObjectExists(minioClient, bucketName, objPath)
- if err != nil {
- return false, fmt.Errorf("check object exists failed: %w", err)
- }
- // Then check if the file has changed by comparing the content
- if exists {
- isUnchanged, err := func() (bool, error) {
- // Get the object
- obj, err := minioClient.GetObject(context.Background(), bucketName, objPath, minio.GetObjectOptions{})
- if err != nil {
- return false, fmt.Errorf("get object failed: %w", err)
- }
- defer obj.Close()
- // Compare the content (io.ReadAll)
- localFile, err := os.Open(localPath)
- if err != nil {
- return false, fmt.Errorf("open local file failed: %w", err)
- }
- defer localFile.Close()
- objContent, err := io.ReadAll(obj)
- if err != nil {
- return false, fmt.Errorf("read object content failed: %w", err)
- }
- localContent, err := io.ReadAll(localFile)
- if err != nil {
- return false, fmt.Errorf("read local content failed: %w", err)
- }
- if bytes.Equal(objContent, localContent) {
- return true, nil
- }
- return false, nil
- }()
- if err != nil {
- return false, err
- }
- if isUnchanged {
- return false, nil
- }
- }
- // Then upload the file
- _, err = minioClient.FPutObject(context.Background(), bucketName, objPath, localPath,
- minio.PutObjectOptions{ContentType: "application/octet-stream"})
- if err != nil {
- return false, fmt.Errorf("fput object failed: %w", err)
- }
- return true, nil
- }
- func MinioUploadFolder(minioClient *minio.Client, bucketName string, objPath string, localPath string) error {
- // Walk the local folder
- err := filepath.WalkDir(localPath, func(path string, d os.DirEntry, err error) error {
- if err != nil {
- return fmt.Errorf("walk dir failed: %w", err)
- }
- if d.IsDir() {
- return nil
- }
- relPath, err := filepath.Rel(localPath, path)
- if err != nil {
- return fmt.Errorf("get relative path failed: %w", err)
- }
- targetPath := filepath.Join(objPath, relPath)
- _, err = minioClient.FPutObject(context.Background(), bucketName, targetPath, path,
- minio.PutObjectOptions{ContentType: "application/octet-stream"})
- if err != nil {
- return fmt.Errorf("fput object failed: %w", err)
- }
- return nil
- })
- return err
- }
- func GetCurrentNanoTimestampUTC() int64 {
- return time.Now().UnixNano()
- }
- func ExtractNumberFromString(filename string) (int64, error) {
- re := regexp.MustCompile(`(\d+)`)
- match := re.FindStringSubmatch(filename)
- if len(match) < 2 {
- return 0, fmt.Errorf("number not found in filename `%s`", filename)
- }
- return strconv.ParseInt(match[1], 10, 64)
- }
|