mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 02:02:37 +02:00
upload tree
This commit is contained in:
parent
131f7038a9
commit
c3321dfc42
@ -33,7 +33,7 @@ func (s *FileToUploadStat) Check() {
|
||||
func (s *FileToUploadStat) AddBytes(filesize int64) {
|
||||
s.mu.Lock()
|
||||
s.TotalBytes += filesize
|
||||
s.Progress.ChangeMax64(s.TotalBytes)
|
||||
s.Progress.ChangeMax64(s.TotalBytes + 1)
|
||||
s.Refresh()
|
||||
s.mu.Unlock()
|
||||
}
|
||||
@ -66,7 +66,6 @@ func (s *FileToUploadStat) Close() {
|
||||
func (s *FileToUploadStat) Fail() {
|
||||
s.mu.Lock()
|
||||
s.Failed++
|
||||
s.Checked++
|
||||
s.Refresh()
|
||||
s.mu.Unlock()
|
||||
}
|
||||
@ -74,7 +73,6 @@ func (s *FileToUploadStat) Fail() {
|
||||
func (s *FileToUploadStat) Skip() {
|
||||
s.mu.Lock()
|
||||
s.Skipped++
|
||||
s.Checked++
|
||||
s.Refresh()
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
@ -28,11 +28,13 @@ func (p *Piwigo) CheckUploadFile(file *FileToUpload, stat *FileToUploadStat) err
|
||||
if !file.Checked() {
|
||||
if file.MD5() == "" {
|
||||
stat.Fail()
|
||||
stat.Check()
|
||||
return errors.New("checksum error")
|
||||
}
|
||||
|
||||
if p.FileExists(file.MD5()) {
|
||||
stat.Skip()
|
||||
stat.Check()
|
||||
return errors.New("file already exists")
|
||||
}
|
||||
|
||||
@ -193,20 +195,52 @@ func (p *Piwigo) ScanTree(
|
||||
func (p *Piwigo) CheckFiles(filesToCheck chan *FileToUpload, files chan *FileToUpload, stat *FileToUploadStat, nbJobs int) {
|
||||
defer close(files)
|
||||
|
||||
wgChecker := &sync.WaitGroup{}
|
||||
wg := &sync.WaitGroup{}
|
||||
for i := 0; i < nbJobs; i++ {
|
||||
wgChecker.Add(1)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wgChecker.Done()
|
||||
defer wg.Done()
|
||||
for file := range filesToCheck {
|
||||
err := p.CheckUploadFile(file, stat)
|
||||
if err != nil {
|
||||
continue
|
||||
if err == nil {
|
||||
files <- file
|
||||
}
|
||||
files <- file
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wgChecker.Wait()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (p *Piwigo) UploadFiles(files chan *FileToUpload, stat *FileToUploadStat, hasVideoJS bool, nbJobs int) error {
|
||||
defer stat.Close()
|
||||
errchan := make(chan error)
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
for i := 0; i < nbJobs; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for file := range files {
|
||||
err := p.Upload(file, stat, 1, hasVideoJS)
|
||||
if err != nil {
|
||||
errchan <- fmt.Errorf("%s: %s", file.FullPath(), err.Error())
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(errchan)
|
||||
}()
|
||||
|
||||
errstring := ""
|
||||
for err := range errchan {
|
||||
errstring += err.Error()
|
||||
}
|
||||
if errstring != "" {
|
||||
return errors.New(errstring)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ func (c *ImagesUploadTreeCommand) Execute(args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, hasVideoJS := status.Plugins["piwigo-videojs"]
|
||||
|
||||
stat := &piwigo.FileToUploadStat{
|
||||
Progress: progressbar.DefaultBytes(1, "..."),
|
||||
@ -32,8 +33,5 @@ func (c *ImagesUploadTreeCommand) Execute(args []string) error {
|
||||
|
||||
go p.ScanTree(c.Dirname, c.CategoryId, 0, &status.UploadFileType, stat, filesToCheck)
|
||||
go p.CheckFiles(filesToCheck, files, stat, 8)
|
||||
for range files {
|
||||
}
|
||||
|
||||
return nil
|
||||
return p.UploadFiles(files, stat, hasVideoJS, 8)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user