mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 02:02:37 +02:00
parallel, progress upload chunks
This commit is contained in:
parent
be7b8e45ce
commit
359b8a69eb
19
go.mod
19
go.mod
@ -3,9 +3,18 @@ module github.com/celogeek/piwigo-cli
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/grokify/html-strip-tags-go v0.0.1 // indirect
|
||||
github.com/jedib0t/go-pretty/v6 v6.2.4 // indirect
|
||||
github.com/jessevdk/go-flags v1.5.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.0.1
|
||||
github.com/jedib0t/go-pretty/v6 v6.2.4
|
||||
github.com/jessevdk/go-flags v1.5.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/schollz/progressbar/v3 v3.8.5 // indirect
|
||||
github.com/stretchr/testify v1.3.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||
)
|
||||
|
32
go.sum
32
go.sum
@ -1,3 +1,5 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E=
|
||||
github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
|
||||
@ -6,11 +8,37 @@ github.com/jedib0t/go-pretty/v6 v6.2.4 h1:wdaj2KHD2W+mz8JgJ/Q6L/T5dB7kyqEFI16eLq
|
||||
github.com/jedib0t/go-pretty/v6 v6.2.4/go.mod h1:+nE9fyyHGil+PuISTCrp7avEdo6bqoMwqZnuiK2r2a0=
|
||||
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
|
||||
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
|
||||
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/schollz/progressbar/v3 v3.8.5 h1:VcmmNRO+eFN3B0m5dta6FXYXY+MEJmXdWoIS+jjssQM=
|
||||
github.com/schollz/progressbar/v3 v3.8.5/go.mod h1:ewO25kD7ZlaJFTvMeOItkOZa8kXu1UvFs379htE8HMQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
|
@ -5,6 +5,9 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
func (p *Piwigo) FileExists(md5 string) bool {
|
||||
@ -19,7 +22,7 @@ func (p *Piwigo) FileExists(md5 string) bool {
|
||||
return resp[md5] != nil
|
||||
}
|
||||
|
||||
func (p *Piwigo) UploadChunks(filename string) error {
|
||||
func (p *Piwigo) UploadChunks(filename string, nbJobs int) error {
|
||||
md5, err := Md5File(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -40,11 +43,50 @@ func (p *Piwigo) UploadChunks(filename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
in := make(chan int64)
|
||||
out := make(chan error)
|
||||
wg := &sync.WaitGroup{}
|
||||
bar := progressbar.DefaultBytes(
|
||||
st.Size(),
|
||||
"uploading",
|
||||
)
|
||||
|
||||
for j := 0; j < nbJobs; j++ {
|
||||
wg.Add(1)
|
||||
go p.UploadChunk(md5, f, in, out, wg, bar)
|
||||
}
|
||||
|
||||
go func() {
|
||||
nbChunks := st.Size()/CHUNK_SIZE + 1
|
||||
for position := int64(0); position < nbChunks; position++ {
|
||||
b64, err := Base64Chunk(f, int64(position))
|
||||
in <- position
|
||||
}
|
||||
close(in)
|
||||
wg.Wait()
|
||||
close(out)
|
||||
bar.Close()
|
||||
}()
|
||||
|
||||
var errString string
|
||||
for err := range out {
|
||||
errString += err.Error() + "\n"
|
||||
}
|
||||
if errString != "" {
|
||||
return errors.New(errString[:len(errString)-1])
|
||||
}
|
||||
|
||||
fmt.Println(md5)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Piwigo) UploadChunk(md5 string, f *os.File, in chan int64, out chan error, wg *sync.WaitGroup, bar *progressbar.ProgressBar) {
|
||||
defer wg.Done()
|
||||
for position := range in {
|
||||
n, b64, err := Base64Chunk(f, position)
|
||||
if err != nil {
|
||||
return err
|
||||
out <- fmt.Errorf("error on chunk %d: %v", position, err)
|
||||
continue
|
||||
}
|
||||
|
||||
err = p.Post("pwg.images.addChunk", &url.Values{
|
||||
@ -54,11 +96,9 @@ func (p *Piwigo) UploadChunks(filename string) error {
|
||||
"data": []string{b64},
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
out <- fmt.Errorf("error on chunk %d: %v", position, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Upload %d/%d ok\n", position+1, nbChunks)
|
||||
bar.Add(n)
|
||||
}
|
||||
fmt.Println(md5)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -48,14 +48,14 @@ func Md5File(filename string) (string, error) {
|
||||
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func Base64Chunk(file *os.File, position int64) (string, error) {
|
||||
func Base64Chunk(file *os.File, position int64) (int, string, error) {
|
||||
b := make([]byte, CHUNK_SIZE)
|
||||
n, err := file.ReadAt(b, position*CHUNK_SIZE)
|
||||
if err != nil && err != io.EOF {
|
||||
return "", err
|
||||
return 0, "", err
|
||||
}
|
||||
if n == 0 {
|
||||
return "", errors.New("position out of bound")
|
||||
return 0, "", errors.New("position out of bound")
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(b[:n]), nil
|
||||
return n, base64.StdEncoding.EncodeToString(b[:n]), nil
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
|
||||
type ImagesUploadCommand struct {
|
||||
Filename string `short:"f" long:"filename" description:"File to upload"`
|
||||
NBJobs int `short:"j" long:"jobs" description:"Number of jobs" default:"1"`
|
||||
}
|
||||
|
||||
func (c *ImagesUploadCommand) Execute(args []string) error {
|
||||
@ -19,7 +20,7 @@ func (c *ImagesUploadCommand) Execute(args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = p.UploadChunks(c.Filename)
|
||||
err = p.UploadChunks(c.Filename, c.NBJobs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user