Skip to content

Commit

Permalink
feat(qn): support upload to dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Feb 13, 2023
1 parent 12a98f9 commit dd5a800
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions cmd/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/qiniu/go-sdk/v7/auth/qbox"
Expand All @@ -16,6 +17,8 @@ import (
var (
overwrite bool
defaultProtocol = "http"

Dir string
)

type PutRet struct {
Expand Down Expand Up @@ -74,9 +77,8 @@ func getFinalKey(path string) string {

func putRemoteFile(path string, mac *qbox.Mac, bucket string, cfg storage.Config) {
finalKey := getFinalKey(path)
fmt.Println(finalKey)
bm := storage.NewBucketManager(mac, &cfg)
res, err := bm.Fetch(path, bucket, finalKey)
res, err := bm.Fetch(path, bucket, filepath.Join(Dir, finalKey))
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -111,11 +113,15 @@ func putLocalFile(path string, mac *qbox.Mac, bucket string, cfg storage.Config)
}
}
putPolicy = storage.PutPolicy{
Scope: fmt.Sprintf("%s:%s", bucket, path),
Scope: fmt.Sprintf("%s:%s", bucket, path),
ForceSaveKey: true,
SaveKey: filepath.Join(Dir, path),
}
} else {
putPolicy = storage.PutPolicy{
Scope: bucket,
Scope: bucket,
ForceSaveKey: true,
SaveKey: filepath.Join(Dir, path),
}

}
Expand All @@ -139,15 +145,15 @@ func putLocalFile(path string, mac *qbox.Mac, bucket string, cfg storage.Config)
return
}
urlsToRefresh := []string{
"https://" + domains[0].Domain + "/" + upload,
"https://" + domains[0].Domain + "/" + filepath.Join(Dir, upload),
}
cdnManager := cdn.NewCdnManager(mac)
_, err = cdnManager.RefreshUrls(urlsToRefresh)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(getProtocol() + "://" + domains[0].Domain + "/" + upload)
fmt.Println(getProtocol() + "://" + domains[0].Domain + "/" + filepath.Join(Dir, upload))
}

func getProtocol() string {
Expand All @@ -169,5 +175,13 @@ func init() {
false,
"when you use -w options, you can replace the same file...",
)

putCmd.Flags().StringVarP(
&Dir,
"dir",
"d",
"",
"put to dir",
)
rootCmd.AddCommand(putCmd)
}

0 comments on commit dd5a800

Please sign in to comment.