Skip to content

Commit

Permalink
refactor: simplified upload sync code
Browse files Browse the repository at this point in the history
  • Loading branch information
52funny authored and 52funny committed Jan 9, 2024
1 parent c49ced8 commit e770f92
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 30 deletions.
39 changes: 9 additions & 30 deletions cmd/upload/upload.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package upload

import (
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -119,30 +118,14 @@ func handleUploadFolder(p *pikpak.PikPak, path string) {
return
}

var f *os.File

// sync mode
if sync {
file, err := os.OpenFile(filepath.Join(".", ".pikpaksync.txt"), os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
logrus.Error(err)
os.Exit(1)
}
f = file
bs, err := io.ReadAll(f)
if err != nil {
logrus.Error("read file error: ", err)
os.Exit(1)
}
alreadySyncFiles := strings.Split(string(bs), "\n")
files := make([]string, 0)
for _, f := range uploadFilePath {
if !utils.Contains(alreadySyncFiles, f) {
files = append(files, f)
}
}
uploadFilePath = files
syncTxt, err := utils.NewSyncTxt(".pikpaksync.txt", sync)
if err != nil {
logrus.Errorln(err)
return
}
defer syncTxt.Close()

uploadFilePath = syncTxt.UnSync(uploadFilePath)

logrus.Info("upload file list:")
for _, f := range uploadFilePath {
Expand Down Expand Up @@ -185,18 +168,14 @@ func handleUploadFolder(p *pikpak.PikPak, path string) {
if err != nil {
logrus.Error(err)
}
if sync {
f.WriteString(v + "\n")
}
syncTxt.WriteString(v + "\n")
logrus.Infof("%s upload success!\n", v)
} else {
err = p.UploadFile(parentId, filepath.Join(path, v))
if err != nil {
logrus.Error(err)
}
if sync {
f.WriteString(v + "\n")
}
syncTxt.WriteString(v + "\n")
}
}
}
96 changes: 96 additions & 0 deletions internal/utils/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package utils

import (
"errors"
"io"
"os"
"slices"
"strings"
"unsafe"

"github.com/sirupsen/logrus"
)

var ErrSyncTxtNotEnable = errors.New("sync txt is not enable")

type SyncTxt struct {
Enable bool
FileName string
alreadySynced []string
f *os.File
}

func NewSyncTxt(fileName string, enable bool) (sync *SyncTxt, err error) {
var f *os.File = nil
var alreadySynced []string
if enable {
f, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
return nil, err
}
bs, err := io.ReadAll(f)
if err != nil {
logrus.Error("read file error: ", err)
os.Exit(1)
}
// avoid end with "\n"
alreadySynced = strings.Split(
strings.TrimRight(unsafe.String(unsafe.SliceData(bs), len(bs)), "\n"),
"\n",
)
}
return &SyncTxt{
Enable: enable,
FileName: fileName,
f: f,
alreadySynced: alreadySynced,
}, nil
}

// impl Writer
func (s *SyncTxt) Write(b []byte) (n int, err error) {
if !s.Enable {
return 0, ErrSyncTxtNotEnable
}
if b[len(b)-1] != '\n' {
b = append(b, '\n')
}
// add to alreadySynced
s.alreadySynced = append(s.alreadySynced, strings.TrimRight(string(b), "\n"))
return s.f.Write(b)
}

// impl Closer
func (s *SyncTxt) Close() error {
if !s.Enable {
return ErrSyncTxtNotEnable
}
return s.f.Close()
}

// impl StringWriter
func (s *SyncTxt) WriteString(str string) (n int, err error) {
if !s.Enable {
return 0, ErrSyncTxtNotEnable
}
if str[len(str)-1] != '\n' {
str += "\n"
}
// add to alreadySynced
s.alreadySynced = append(s.alreadySynced, strings.TrimRight(str, "\n"))
return s.f.WriteString(str)
}

func (s *SyncTxt) UnSync(files []string) []string {
if !s.Enable {
return files
}
res := make([]string, 0)
for _, f := range files {
if slices.Contains(s.alreadySynced, f) {
continue
}
res = append(res, f)
}
return res
}

0 comments on commit e770f92

Please sign in to comment.