44 "bytes"
55 "encoding/json"
66 "fmt"
7+ "io/fs"
78 "io/ioutil"
89 "log"
910 "math/rand"
@@ -20,6 +21,7 @@ import (
2021 "github.com/arrebole/progressbar"
2122 "github.com/fatih/color"
2223 "github.com/upyun/go-sdk/v3/upyun"
24+ "github.com/upyun/upx/fsutil"
2325 "github.com/upyun/upx/partial"
2426)
2527
@@ -496,7 +498,6 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os.
496498 return err
497499 }
498500 defer fd .Close ()
499-
500501 cfg := & upyun.PutObjectConfig {
501502 Path : upPath ,
502503 Headers : map [string ]string {
@@ -601,20 +602,32 @@ func (sess *Session) putFilesWitchProgress(localFiles []*UploadedFile, workers i
601602 wg .Wait ()
602603}
603604
604- func (sess * Session ) putDir (localPath , upPath string , workers int ) {
605+ func (sess * Session ) putDir (localPath , upPath string , workers int , withIgnore bool ) {
606+ localAbsPath , err := filepath .Abs (localPath )
607+ if err != nil {
608+ PrintErrorAndExit (err .Error ())
609+ }
610+ // 如果上传的是目录,并且是隐藏的目录,则触发提示
611+ rootDirInfo , err := os .Stat (localAbsPath )
612+ if err != nil {
613+ PrintErrorAndExit (err .Error ())
614+ }
615+ if ! withIgnore && fsutil .IsIgnoreFile (localAbsPath , rootDirInfo ) {
616+ PrintErrorAndExit ("%s is a ignore dir, use `-all` to force put all files" , localAbsPath )
617+ }
618+
605619 type FileInfo struct {
606620 fpath string
607621 fInfo os.FileInfo
608622 }
609623 localFiles := make (chan * FileInfo , workers * 2 )
610624 var wg sync.WaitGroup
611- var err error
612625 wg .Add (workers )
613626 for w := 0 ; w < workers ; w ++ {
614627 go func () {
615628 defer wg .Done ()
616629 for info := range localFiles {
617- rel , _ := filepath .Rel (localPath , info .fpath )
630+ rel , _ := filepath .Rel (localAbsPath , info .fpath )
618631 desPath := path .Join (upPath , filepath .ToSlash (rel ))
619632 if fInfo , err := os .Stat (info .fpath ); err == nil && fInfo .IsDir () {
620633 err = sess .updriver .Mkdir (desPath )
@@ -628,21 +641,29 @@ func (sess *Session) putDir(localPath, upPath string, workers int) {
628641 }()
629642 }
630643
631- walk (localPath , func (fpath string , fInfo os.FileInfo , err error ) {
632- if err == nil {
644+ filepath .Walk (localAbsPath , func (path string , info fs.FileInfo , err error ) error {
645+ if err != nil {
646+ return err
647+ }
648+ if ! withIgnore && fsutil .IsIgnoreFile (path , info ) {
649+ if info .IsDir () {
650+ return filepath .SkipDir
651+ }
652+ } else {
633653 localFiles <- & FileInfo {
634- fpath : fpath ,
635- fInfo : fInfo ,
654+ fpath : path ,
655+ fInfo : info ,
636656 }
637657 }
658+ return nil
638659 })
639660
640661 close (localFiles )
641662 wg .Wait ()
642663}
643664
644665// / Put 上传单文件或单目录
645- func (sess * Session ) Put (localPath , upPath string , workers int ) {
666+ func (sess * Session ) Put (localPath , upPath string , workers int , withIgnore bool ) {
646667 upPath = sess .AbsPath (upPath )
647668
648669 exist , isDir := false , false
@@ -693,7 +714,7 @@ func (sess *Session) Put(localPath, upPath string, workers int) {
693714 upPath = path .Join (upPath , filepath .Base (localPath ))
694715 }
695716 }
696- sess .putDir (localPath , upPath , workers )
717+ sess .putDir (localPath , upPath , workers , withIgnore )
697718 } else {
698719 if isDir {
699720 upPath = path .Join (upPath , filepath .Base (localPath ))
@@ -703,7 +724,7 @@ func (sess *Session) Put(localPath, upPath string, workers int) {
703724}
704725
705726// put 的升级版命令, 支持多文件上传
706- func (sess * Session ) Upload (filenames []string , upPath string , workers int ) {
727+ func (sess * Session ) Upload (filenames []string , upPath string , workers int , withIgnore bool ) {
707728 upPath = sess .AbsPath (upPath )
708729
709730 // 检测云端的目的地目录
@@ -741,7 +762,12 @@ func (sess *Session) Upload(filenames []string, upPath string, workers int) {
741762
742763 // 上传目录
743764 for _ , localPath := range dirs {
744- sess .putDir (localPath , path .Join (upPath , filepath .Base (localPath )), workers )
765+ sess .putDir (
766+ localPath ,
767+ path .Join (upPath , filepath .Base (localPath )),
768+ workers ,
769+ withIgnore ,
770+ )
745771 }
746772
747773 // 上传文件
0 commit comments