mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 10:12: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) {
|
func (s *FileToUploadStat) AddBytes(filesize int64) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.TotalBytes += filesize
|
s.TotalBytes += filesize
|
||||||
s.Progress.ChangeMax64(s.TotalBytes)
|
s.Progress.ChangeMax64(s.TotalBytes + 1)
|
||||||
s.Refresh()
|
s.Refresh()
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
}
|
}
|
||||||
@ -66,7 +66,6 @@ func (s *FileToUploadStat) Close() {
|
|||||||
func (s *FileToUploadStat) Fail() {
|
func (s *FileToUploadStat) Fail() {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.Failed++
|
s.Failed++
|
||||||
s.Checked++
|
|
||||||
s.Refresh()
|
s.Refresh()
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
}
|
}
|
||||||
@ -74,7 +73,6 @@ func (s *FileToUploadStat) Fail() {
|
|||||||
func (s *FileToUploadStat) Skip() {
|
func (s *FileToUploadStat) Skip() {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.Skipped++
|
s.Skipped++
|
||||||
s.Checked++
|
|
||||||
s.Refresh()
|
s.Refresh()
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
@ -28,11 +28,13 @@ func (p *Piwigo) CheckUploadFile(file *FileToUpload, stat *FileToUploadStat) err
|
|||||||
if !file.Checked() {
|
if !file.Checked() {
|
||||||
if file.MD5() == "" {
|
if file.MD5() == "" {
|
||||||
stat.Fail()
|
stat.Fail()
|
||||||
|
stat.Check()
|
||||||
return errors.New("checksum error")
|
return errors.New("checksum error")
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.FileExists(file.MD5()) {
|
if p.FileExists(file.MD5()) {
|
||||||
stat.Skip()
|
stat.Skip()
|
||||||
|
stat.Check()
|
||||||
return errors.New("file already exists")
|
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) {
|
func (p *Piwigo) CheckFiles(filesToCheck chan *FileToUpload, files chan *FileToUpload, stat *FileToUploadStat, nbJobs int) {
|
||||||
defer close(files)
|
defer close(files)
|
||||||
|
|
||||||
wgChecker := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
for i := 0; i < nbJobs; i++ {
|
for i := 0; i < nbJobs; i++ {
|
||||||
wgChecker.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wgChecker.Done()
|
defer wg.Done()
|
||||||
for file := range filesToCheck {
|
for file := range filesToCheck {
|
||||||
err := p.CheckUploadFile(file, stat)
|
err := p.CheckUploadFile(file, stat)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
continue
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
_, hasVideoJS := status.Plugins["piwigo-videojs"]
|
||||||
|
|
||||||
stat := &piwigo.FileToUploadStat{
|
stat := &piwigo.FileToUploadStat{
|
||||||
Progress: progressbar.DefaultBytes(1, "..."),
|
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.ScanTree(c.Dirname, c.CategoryId, 0, &status.UploadFileType, stat, filesToCheck)
|
||||||
go p.CheckFiles(filesToCheck, files, stat, 8)
|
go p.CheckFiles(filesToCheck, files, stat, 8)
|
||||||
for range files {
|
return p.UploadFiles(files, stat, hasVideoJS, 8)
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user