mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 02:02:37 +02:00
add lock and retry
This commit is contained in:
parent
d6a6280c59
commit
f60d6f843f
@ -68,6 +68,8 @@ func (p *Piwigo) Upload(file *FileToUpload, stat *FileToUploadStat, nbJobs int,
|
||||
return
|
||||
}
|
||||
|
||||
// lock this process for committing the file
|
||||
|
||||
exif, _ := Exif(file.FullPath())
|
||||
var resp *FileUploadResult
|
||||
data := &url.Values{}
|
||||
@ -80,10 +82,20 @@ func (p *Piwigo) Upload(file *FileToUpload, stat *FileToUploadStat, nbJobs int,
|
||||
if file.CategoryId > 0 {
|
||||
data.Set("categories", fmt.Sprint(file.CategoryId))
|
||||
}
|
||||
err = p.Post("pwg.images.add", data, &resp)
|
||||
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
for i := 0; i < 3; i++ {
|
||||
err = p.Post("pwg.images.add", data, &resp)
|
||||
if err == nil || err.Error() == "[Error 500] file already exists" {
|
||||
err = nil
|
||||
break
|
||||
}
|
||||
stat.Error(fmt.Sprintf("Upload %d", i), file.FullPath(), err)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
stat.Fail()
|
||||
stat.Error("Upload", file.FullPath(), err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -161,10 +173,12 @@ func (p *Piwigo) ScanTree(
|
||||
var resp struct {
|
||||
Id int `json:"id"`
|
||||
}
|
||||
p.mu.Lock()
|
||||
err = p.Post("pwg.categories.add", &url.Values{
|
||||
"name": []string{strings.ReplaceAll(dirname, "'", `\'`)},
|
||||
"parent": []string{fmt.Sprint(parentCategoryId)},
|
||||
}, &resp)
|
||||
p.mu.Unlock()
|
||||
if err != nil {
|
||||
stat.Error("ScanTree Categories Add", rootPath, err)
|
||||
return
|
||||
@ -209,9 +223,24 @@ func (p *Piwigo) CheckFiles(filesToCheck chan *FileToUpload, files chan *FileToU
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (p *Piwigo) UploadFiles(files chan *FileToUpload, stat *FileToUploadStat, hasVideoJS bool, nbJobs int) {
|
||||
func (p *Piwigo) UploadFiles(
|
||||
files chan *FileToUpload,
|
||||
stat *FileToUploadStat,
|
||||
hasVideoJS bool,
|
||||
nbJobs int,
|
||||
nbJobsChunk int,
|
||||
) {
|
||||
defer stat.Close()
|
||||
for file := range files {
|
||||
p.Upload(file, stat, nbJobs, hasVideoJS)
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
for i := 0; i < nbJobs; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for file := range files {
|
||||
p.Upload(file, stat, nbJobsChunk, hasVideoJS)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
@ -1,10 +1,14 @@
|
||||
package piwigo
|
||||
|
||||
import "sync"
|
||||
|
||||
type Piwigo struct {
|
||||
Url string `json:"url"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Token string `json:"token"`
|
||||
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type PiwigoResult struct {
|
||||
|
@ -33,7 +33,7 @@ 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)
|
||||
p.UploadFiles(files, stat, hasVideoJS, 4)
|
||||
p.UploadFiles(files, stat, hasVideoJS, 4, 2)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user