diff --git a/storage/minio.go b/storage/minio.go index 4eb3a83..d0f0922 100644 --- a/storage/minio.go +++ b/storage/minio.go @@ -154,7 +154,7 @@ func (s *Minio) Stat(objectName string) (*ObjectInfo, error) { return info, nil } -func (s *Minio) Fetch(urlStr, objectName string) error { +func (s *Minio) Fetch(urlStr, objectName string, local ...bool) error { if err := s.Init(); err != nil { return err } diff --git a/storage/qiniu.go b/storage/qiniu.go index a428850..97ab69c 100644 --- a/storage/qiniu.go +++ b/storage/qiniu.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "os" + "path" + "strings" "time" "github.com/qiniu/go-sdk/v7/auth/qbox" @@ -169,7 +171,7 @@ func (s *Qiniu) Url(objectName string, expire time.Duration) string { } domain := "https://" + domains[0].Domain - + if expire == 0 { return storage.MakePublicURLv2(domain, objectName) } else { @@ -198,7 +200,10 @@ func (s *Qiniu) Stat(objectName string) (*ObjectInfo, error) { return info, nil } -func (s *Qiniu) Fetch(url, objectName string) error { +func (s *Qiniu) Fetch(url, objectName string, local ...bool) error { + if len(local) > 0 && local[0] == true { + return s.fetchLocal(url, objectName) + } mac := qbox.NewMac(s.config.AK, s.config.SK) cfg := storage.Config{ UseHTTPS: false, @@ -213,3 +218,19 @@ func (s *Qiniu) Fetch(url, objectName string) error { return nil } + +func (s *Qiniu) fetchLocal(url, objectName string) error { + objectName = strings.TrimLeft(objectName, "/ ") + ext := strings.TrimLeft(path.Ext(objectName), ".") + + tmpFile := fmt.Sprintf("%d.%s", time.Now().UnixMilli(), ext) + if err := Download(url, tmpFile); err != nil { + return err + } + defer os.Remove(tmpFile) + + if err := s.Put(tmpFile, objectName, nil); err != nil { + return err + } + return nil +} diff --git a/storage/storage.go b/storage/storage.go index cf5e329..cd229ec 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -16,5 +16,5 @@ type Storage interface { Url(objectName string, expire time.Duration) string Stat(objectName string) (*ObjectInfo, error) List(objectPrefix string) ([]string, error) - Fetch(url, objectName string) error + Fetch(url, objectName string, local ...bool) error }