Skip to content

Commit

Permalink
🐛 修复空文件、重复文件上传
Browse files Browse the repository at this point in the history
  • Loading branch information
aoaostar committed Apr 3, 2022
1 parent b224372 commit 701b9b0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ jobs:
- name: compress
run: |
cp example.config.yaml linux_arm64/config.yaml
cp example.config.yaml linux_arm64/config.yaml
cp example.config.yaml linux_amd64/config.yaml
cp example.config.yaml linux_arm/config.yaml
cp example.config.yaml linux_386/config.yaml
cp example.config.yaml windows_amd64/config.yaml
tar -czvf alidrive_uploader_linux_amd64.tar.gz linux_amd64
tar -czvf alidrive_uploader_linux_arm64.tar.gz linux_arm64
tar -czvf alidrive_uploader_linux_arm.tar.gz linux_arm
tar -czvf alidrive_uploader_linux_386.tar.gz linux_386
zip alidrive_uploader_windows_amd64.zip windows_amd64
zip -r alidrive_uploader_windows_amd64.zip windows_amd64
- name: Build Changelog
id: github_release
uses: mikepenz/release-changelog-builder-action@main
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
test.*
logs
config.yaml
*.exe
*.exe
*.bat
main
3 changes: 1 addition & 2 deletions bootstrap/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TreeFolders(drive *alidrive.AliDrive, remotePath string, dirs map[string]st
return
}
for k := range dirs {
if k == "." {
if k == "." || k == "/" || k == "\\" {
dirs[k] = drive.Instance.ParentPath
continue
}
Expand All @@ -26,5 +26,4 @@ func TreeFolders(drive *alidrive.AliDrive, remotePath string, dirs map[string]st
return
}
}

}
7 changes: 3 additions & 4 deletions bootstrap/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"alidrive_uploader/conf"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"os"
)

func InitLog() {
Expand All @@ -17,7 +15,8 @@ func InitLog() {
Compress: false, // 是否需要压缩滚动日志, 使用的 gzip 压缩
LocalTime: true,
}
logrus.SetOutput(io.MultiWriter(os.Stdout, logWriter))
//logrus.SetOutput(io.MultiWriter(os.Stdout, logWriter))
logrus.SetOutput(logWriter)
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
Expand All @@ -26,7 +25,7 @@ func InitLog() {

if *conf.Opt.Debug {
logrus.SetLevel(logrus.DebugLevel)
//logrus.SetReportCaller(true)
logrus.SetReportCaller(true)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
Expand Down
24 changes: 13 additions & 11 deletions bootstrap/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"alidrive_uploader/conf"
"alidrive_uploader/pkg/alidrive"
"alidrive_uploader/pkg/util"
"fmt"
"github.com/sirupsen/logrus"
"github.com/vbauerster/mpb/v7"
"math"
"os"
"path/filepath"
"sync"
"time"
)

var errors = map[string]string{}
Expand All @@ -34,7 +36,8 @@ func Run() {
return
}
} else {
allFiles = []string{conf.Opt.Positional.LocalPath}
allFiles = []string{filepath.Base(conf.Opt.Positional.LocalPath)}
conf.Opt.Positional.LocalPath = filepath.Dir(conf.Opt.Positional.LocalPath) + "/"
}

drive := alidrive.AliDrive{
Expand All @@ -45,21 +48,21 @@ func Run() {
ParentPath: "root",
},
}
logrus.Infof("正在获取AccessToken")
fmt.Println("正在获取AccessToken")
if err := drive.RefreshToken(); err != nil {
logrus.Panic(err)
return
}
conf.SaveConfig()

logrus.Infof("正在获取目录")
fmt.Println("正在生成目录")
var files []util.FileStream
//建立目录结构
var dirs = make(map[string]string, 0)
for _, fp := range allFiles {
//目录
dir := filepath.Dir(fp)
file, err := readFileInfo(fp)
file, err := readFileInfo(conf.Opt.Positional.LocalPath + fp)
if err != nil {
logrus.Panic(err)
return
Expand All @@ -75,7 +78,7 @@ func Run() {
TreeFolders(&drive, conf.Opt.Positional.RemotePath, dirs)

wg := sync.WaitGroup{}
p := mpb.New(mpb.WithWaitGroup(&wg))
p := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithRefreshRate(300*time.Millisecond))

//文件数量进度条
taskBar := util.NewMpbTask(p, "任务列表", int64(len(files)))
Expand All @@ -96,6 +99,7 @@ func Run() {
close(jobs)
p.Wait()
logrus.Infof("上传完毕!共计%d个文件,失败文件个数:%d个", len(files), len(errors))
fmt.Printf("上传完毕!共计%d个文件,失败文件个数:%d个", len(files), len(errors))
}

func transfer(jobs chan util.FileStream, taskBar *mpb.Bar, p *mpb.Progress, drive *alidrive.AliDrive, dirs map[string]string) {
Expand All @@ -107,10 +111,12 @@ func transfer(jobs chan util.FileStream, taskBar *mpb.Bar, p *mpb.Progress, driv
file.ParentPath = dirs[file.ParentPath]
err := drive.Upload(file)
if err != nil {
logrus.Errorf("[%v]上传失败", err)
logrus.Errorf("[%v]上传失败:%v", file.Name, err)
errors[file.ReadlPath] = err.Error()
bar.Abort(true)
} else {
logrus.Infof("[%v]上传成功", file.Name)
bar.Abort(true)
}
taskBar.Increment()
_ = file.Bar.Close()
Expand All @@ -128,12 +134,8 @@ func readFileInfo(fp string) (util.FileStream, error) {
if err != nil {
return fs, err
}
contentType, err := util.GetFileContentType(open)
if err != nil {
return fs, err
}
contentType := util.GetFileContentType(open)
abs, err := filepath.Abs(fp)

if err != nil {
return fs, err
}
Expand Down
17 changes: 12 additions & 5 deletions pkg/alidrive/alidrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"math"
"net/http"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -104,15 +105,19 @@ func (drive *AliDrive) Upload(file util.FileStream) error {
if err != nil {
return err
}
createWithFoldersBody["ContentHash"] = proofCode.Sha1
createWithFoldersBody["ProofCode"] = proofCode.ProofCode
createWithFoldersBody["ProofVersion"] = "v1"
delete(createWithFoldersBody, "pre_hash")
createWithFoldersBody["content_hash_name"] = "sha1"
createWithFoldersBody["content_hash"] = proofCode.Sha1
createWithFoldersBody["proof_code"] = proofCode.ProofCode
createWithFoldersBody["proof_version"] = "v1"
logrus.Debug("createWithFoldersBody", createWithFoldersBody)
_, err = client.R().
SetAuthToken(drive.Instance.AccessToken).
SetBody(&createWithFoldersBody).
SetResult(&resp).
SetError(&e).
Post(url)
logrus.Debugf("%+v,%+v", resp, e)
if err != nil {
return err
}
Expand All @@ -122,9 +127,11 @@ func (drive *AliDrive) Upload(file util.FileStream) error {
if resp.RapidUpload {
return nil
}
logrus.Debugf("%+v,%+v", resp, e)
}

if len(resp.PartInfoList) != int(total) {
return errors.New("上传地址为空,无法上传")
}
//正式上传
if _, err = file.File.Seek(0, 0); err != nil {
return err
Expand Down Expand Up @@ -203,7 +210,7 @@ func (drive *AliDrive) Upload(file util.FileStream) error {

func (drive *AliDrive) CreateFolders(path string, rootPath string) (string, error) {

path = strings.ReplaceAll(path, "\\", "/")
path = filepath.ToSlash(path)
split := strings.Split(path, "/")
var parentFileId = rootPath
for _, v := range split {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/mpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func NewMpb(p *mpb.Progress, name string, total int64) *mpb.Bar {

return p.New(total,
mpb.BarStyle().Filler("#"),
mpb.BarFillerClearOnComplete(),
mpb.PrependDecorators(
decor.Name(TruncateText(name, 25), decor.WCSyncSpace),
decor.CountersKibiByte(" % .2f / % .2f ", decor.WCSyncSpace),
Expand Down
31 changes: 23 additions & 8 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ func GetSha1Code(data string) string {
}

func GetProofCode(accessToken string, realpath string) (ProofCode, error) {
var proofCode ProofCode
var proofCode = ProofCode{
Sha1: "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709",
ProofCode: "",
}
stat, err := os.Stat(realpath)
if err != nil {
return proofCode, nil
}
filesize := stat.Size()
if filesize == 0 {
return proofCode, nil
}
file, err := os.Open(realpath)
if err != nil {
return proofCode, err
Expand All @@ -41,8 +52,6 @@ func GetProofCode(accessToken string, realpath string) (ProofCode, error) {
m := md5.New()
m.Write([]byte(buffa))
hashMd5 := hex.EncodeToString(m.Sum(nil))
stat, err := os.Stat(realpath)
filesize := stat.Size()
parseInt, err := strconv.ParseUint(hashMd5[0:16], 16, 64)
if err != nil {
return ProofCode{}, err
Expand All @@ -61,22 +70,28 @@ func GetProofCode(accessToken string, realpath string) (ProofCode, error) {
proofCode.ProofCode = encoding
return proofCode, nil
}
func GetFileContentType(out *os.File) (string, error) {
func GetFileContentType(out *os.File) string {

buffer := make([]byte, 512)
_, err := out.Read(buffer)
_, err := out.Seek(0, 0)
if err != nil {
return "plain/text"
}
_, err = out.Read(buffer)

defer func() { out.Seek(0, 0) }()
if err != nil {
return "", err
return "plain/text"
}
contentType := http.DetectContentType(buffer)
return contentType, nil
return contentType
}

func GetAllFiles(path string) ([]string, error) {
var files []string
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, p)
files = append(files, filepath.ToSlash(p)[len(path):])
}
return nil
})
Expand Down

0 comments on commit 701b9b0

Please sign in to comment.