Skip to content

Commit

Permalink
Package artifact:
Browse files Browse the repository at this point in the history
- replace file progress to generic interface extended from io reader / writer with progress function
- replace download function who's make the io copy, to a function that return the size + io.readCloser stream

Package ioutils/ioprogress:
- add a package to expose a generic interface based on io readcloser / writecloser
- add a instance to create a io readcloser / writecloser with progress function
- new instance based only on io readcloser/ writecloser and allow to register progress function or not if not wanted

Other:
- bump dependencies
- adjust format licence for context/gin
  • Loading branch information
nabbar committed Apr 15, 2024
1 parent 71c18b9 commit 24e4191
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 201 deletions.
4 changes: 2 additions & 2 deletions artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
package artifact

import (
"io"
"os"
"regexp"
"strings"

hscvrs "github.com/hashicorp/go-version"
artcli "github.com/nabbar/golib/artifact/client"
liberr "github.com/nabbar/golib/errors"
libfpg "github.com/nabbar/golib/file/progress"
)

const subUp = 20
Expand All @@ -50,7 +50,7 @@ type Client interface {

ListReleases() (releases hscvrs.Collection, err error)
GetArtifact(containName string, regexName string, release *hscvrs.Version) (link string, err error)
Download(dst libfpg.Progress, containName string, regexName string, release *hscvrs.Version) error
Download(containName string, regexName string, release *hscvrs.Version) (int64, io.ReadCloser, error)
}

func CheckRegex(name, regex string) bool {
Expand Down
29 changes: 8 additions & 21 deletions artifact/github/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
hscvrs "github.com/hashicorp/go-version"
libart "github.com/nabbar/golib/artifact"
artcli "github.com/nabbar/golib/artifact/client"
libfpg "github.com/nabbar/golib/file/progress"
)

const (
Expand Down Expand Up @@ -112,46 +111,34 @@ func (g *githubModel) GetArtifact(containName string, regexName string, release
return "", ErrorGithubNotFound.Error(nil)
}

func (g *githubModel) Download(dst libfpg.Progress, containName string, regexName string, release *hscvrs.Version) error {
func (g *githubModel) Download(containName string, regexName string, release *hscvrs.Version) (int64, io.ReadCloser, error) {
var (
uri string
rsp *github.Response
req *http.Request
err error
e error
n int64
)

defer func() {
if req != nil && req.Body != nil {
_ = req.Body.Close()
}
if rsp != nil && rsp.Body != nil {
_ = rsp.Body.Close()
}
}()

if uri, e = g.GetArtifact(containName, regexName, release); e != nil {
return e
return 0, nil, e
} else if req, err = g.c.NewRequest(http.MethodGet, uri, nil); err != nil {
return ErrorGithubRequestNew.Error(err)
return 0, nil, ErrorGithubRequestNew.Error(err)
} else if rsp, err = g.c.Do(g.x, req, nil); err != nil {
return ErrorGithubRequestRun.Error(err)
return 0, nil, ErrorGithubRequestRun.Error(err)
} else if rsp.StatusCode < 200 || rsp.StatusCode > 299 {
return ErrorGithubResponse.Error(errResponseCode)
return 0, nil, ErrorGithubResponse.Error(errResponseCode)
} else if rsp.ContentLength < 1 {
return ErrorGithubResponse.Error(errResponseContents)
return 0, nil, ErrorGithubResponse.Error(errResponseContents)
} else if rsp.Body == nil {
return ErrorGithubResponse.Error(errResponseBodyEmpty)
return 0, nil, ErrorGithubResponse.Error(errResponseBodyEmpty)
} else {
dst.Reset(rsp.ContentLength)
}

if n, err = io.Copy(dst, rsp.Body); err != nil {
return ErrorGithubIOCopy.Error(err)
} else if n != rsp.ContentLength {
return ErrorDestinationSize.Error(errMisMatchingSize)
return rsp.ContentLength, rsp.Body, nil
}

return nil
}
29 changes: 8 additions & 21 deletions artifact/gitlab/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
hscvrs "github.com/hashicorp/go-version"
libart "github.com/nabbar/golib/artifact"
artcli "github.com/nabbar/golib/artifact/client"
libfpg "github.com/nabbar/golib/file/progress"
gitlab "github.com/xanzy/go-gitlab"
)

Expand Down Expand Up @@ -111,46 +110,34 @@ func (g *gitlabModel) GetArtifact(containName string, regexName string, release
return "", ErrorGitlabNotFound.Error(nil)
}

func (g *gitlabModel) Download(dst libfpg.Progress, containName string, regexName string, release *hscvrs.Version) error {
func (g *gitlabModel) Download(containName string, regexName string, release *hscvrs.Version) (int64, io.ReadCloser, error) {
var (
uri string
rsp *gitlab.Response
req *hschtc.Request
err error
e error
n int64
)

defer func() {
if req != nil && req.Body != nil {
_ = req.Body.Close()
}
if rsp != nil && rsp.Body != nil {
_ = rsp.Body.Close()
}
}()

if uri, e = g.GetArtifact(containName, regexName, release); e != nil {
return e
return 0, nil, e
} else if req, err = g.c.NewRequest(http.MethodGet, uri, nil, nil); err != nil {
return ErrorGitlabRequestNew.Error(err)
return 0, nil, ErrorGitlabRequestNew.Error(err)
} else if rsp, err = g.c.Do(req, nil); err != nil {
return ErrorGitlabRequestRun.Error(err)
return 0, nil, ErrorGitlabRequestRun.Error(err)
} else if rsp.StatusCode < 200 || rsp.StatusCode > 299 {
return ErrorGitlabResponse.Error(errResponseCode)
return 0, nil, ErrorGitlabResponse.Error(errResponseCode)
} else if rsp.ContentLength < 1 {
return ErrorGitlabResponse.Error(errResponseContents)
return 0, nil, ErrorGitlabResponse.Error(errResponseContents)
} else if rsp.Body == nil {
return ErrorGitlabResponse.Error(errResponseBodyEmpty)
return 0, nil, ErrorGitlabResponse.Error(errResponseBodyEmpty)
} else {
dst.Reset(rsp.ContentLength)
}

if n, err = io.Copy(dst, rsp.Body); err != nil {
return ErrorGitlabIOCopy.Error(err)
} else if n != rsp.ContentLength {
return ErrorDestinationSize.Error(errMisMatchingSize)
return rsp.ContentLength, rsp.Body, nil
}

return nil
}
33 changes: 11 additions & 22 deletions artifact/jfrog/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
hscvrs "github.com/hashicorp/go-version"
libart "github.com/nabbar/golib/artifact"
artcli "github.com/nabbar/golib/artifact/client"
libfpg "github.com/nabbar/golib/file/progress"
)

type artifactoryModel struct {
Expand Down Expand Up @@ -319,10 +318,9 @@ func (a *artifactoryModel) GetArtifact(containName string, regexName string, rel
}
}

func (a *artifactoryModel) Download(dst libfpg.Progress, containName string, regexName string, release *hscvrs.Version) error {
func (a *artifactoryModel) Download(containName string, regexName string, release *hscvrs.Version) (int64, io.ReadCloser, error) {
var (
e error
n int64

art *ResponseStorage
err error
Expand All @@ -331,38 +329,29 @@ func (a *artifactoryModel) Download(dst libfpg.Progress, containName string, reg
)

defer func() {
if rsp != nil && rsp.Body != nil {
_ = rsp.Body.Close()
}

if req != nil && req.Body != nil {
_ = req.Body.Close()
}
}()

if art, err = a.getArtifact(containName, regexName, release); err != nil {
return err
} else {
dst.Reset(art.size)
return 0, nil, err
}

if req, e = http.NewRequestWithContext(a.ctx, http.MethodGet, art.DownloadUri, nil); e != nil {
return ErrorRequestInit.Error(e)
return 0, nil, ErrorRequestInit.Error(e)
} else if rsp, e = a.Do(req); e != nil {
return ErrorRequestDo.Error(e)
return 0, nil, ErrorRequestDo.Error(e)
} else if rsp.StatusCode >= http.StatusBadRequest {
//nolint #goerr113
return ErrorRequestResponse.Error(fmt.Errorf("status: %v", rsp.Status))
return 0, nil, ErrorRequestResponse.Error(fmt.Errorf("status: %v", rsp.Status))
} else if rsp.Body == nil {
//nolint #goerr113
return ErrorRequestResponseBodyEmpty.Error(fmt.Errorf("status: %v", rsp.Status))
} else if n, e = io.Copy(dst, rsp.Body); e != nil {
return ErrorArtifactoryDownload.Error(e)
} else if n != art.size {
return ErrorDestinationSize.Error(errMisMatchingSize)
} else if n != rsp.ContentLength {
return ErrorDestinationSize.Error(errMisMatchingSize)
return 0, nil, ErrorRequestResponseBodyEmpty.Error(fmt.Errorf("status: %v", rsp.Status))
} else if art.size != rsp.ContentLength {
_ = rsp.Body.Close()
return 0, nil, ErrorDestinationSize.Error(errMisMatchingSize)
} else {
return rsp.ContentLength, rsp.Body, nil
}

return nil
}
61 changes: 18 additions & 43 deletions artifact/s3aws/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
libart "github.com/nabbar/golib/artifact"
artcli "github.com/nabbar/golib/artifact/client"
libaws "github.com/nabbar/golib/aws"
libfpg "github.com/nabbar/golib/file/progress"
)

type s3awsModel struct {
Expand Down Expand Up @@ -145,7 +144,7 @@ func (s *s3awsModel) GetArtifact(containName string, regexName string, release *
return "", ErrorS3AWSNotFound.Error(getError(errVersRequest, release.String()))
}

func (s *s3awsModel) Download(dst libfpg.Progress, containName string, regexName string, release *hscvrs.Version) error {
func (s *s3awsModel) Download(containName string, regexName string, release *hscvrs.Version) (int64, io.ReadCloser, error) {
var (
e error
r *regexp.Regexp
Expand All @@ -157,11 +156,11 @@ func (s *s3awsModel) Download(dst libfpg.Progress, containName string, regexName
)

if s.regex == "" {
return ErrorParamEmpty.Error(nil)
return 0, nil, ErrorParamEmpty.Error(nil)
}

if l, err = s.c.Object().Find(s.regex); e != nil {
return ErrorS3AWSFind.Error(err)
if l, err = s.c.Object().Find(s.regex); err != nil {
return 0, nil, ErrorS3AWSFind.Error(err)
}

r = regexp.MustCompile(s.regex)
Expand All @@ -170,68 +169,44 @@ func (s *s3awsModel) Download(dst libfpg.Progress, containName string, regexName
grp := r.FindStringSubmatch(o)

if len(grp) < s.group {
return ErrorS3AWSRegex.Error(getError(errRegexGroup, s.regex, len(grp), s.group))
return 0, nil, ErrorS3AWSRegex.Error(getError(errRegexGroup, s.regex, len(grp), s.group))
}

if v, e = hscvrs.NewVersion(grp[s.group]); e != nil {
return ErrorS3AWSNewVers.Error(getError(errVersion, grp[s.group]), e)
return 0, nil, ErrorS3AWSNewVers.Error(getError(errVersion, grp[s.group]), e)
} else if v.Equal(release) {
if containName != "" && strings.Contains(o, containName) {
return s.downloadObject(dst, o)
return s.downloadObject(o)
}

if regexName != "" {
if k, e = regexp.MatchString(regexName, o); e == nil && k {
return s.downloadObject(dst, o)
return s.downloadObject(o)
}
}

if containName == "" && regexName == "" {
return s.downloadObject(dst, o)
return s.downloadObject(o)
}
}
}

return ErrorS3AWSNotFound.Error(getError(errVersRequest, release.String()))
return 0, nil, ErrorS3AWSNotFound.Error(getError(errVersRequest, release.String()))
}

func (s *s3awsModel) downloadObject(dst libfpg.Progress, object string) error {
func (s *s3awsModel) downloadObject(object string) (int64, io.ReadCloser, error) {
var (
r *sdksss.GetObjectOutput
e error
j int64
n int64

r *sdksss.GetObjectOutput
err error
)

defer func() {
if r != nil && r.Body != nil {
_ = r.Body.Close()
}
}()

if j, err = s.c.Object().Size(object); err != nil {
er := ErrorS3AWSDownloadError.Error(getError(errObject, object))
er.Add(err)
return er
} else if j < 1 {
return ErrorS3AWSDownloadError.Error(getError(errObjectEmpty, object))
} else {
dst.Reset(j)
}

if r, err = s.c.Object().Get(object); err != nil {
er := ErrorS3AWSDownloadError.Error(getError(errObject, object))
er.Add(err)
return er
return 0, nil, ErrorS3AWSDownloadError.Error(getError(errObject, object), err)
} else if r.ContentLength == nil || *r.ContentLength < 1 {
return 0, nil, ErrorS3AWSDownloadError.Error(getError(errObjectEmpty, object))
} else if r.Body == nil {
return ErrorS3AWSIOReaderError.Error(getError(errObject, object))
} else if n, e = io.Copy(dst, r.Body); e != nil {
return ErrorS3AWSDownloadError.Error(getError(errObject, object), e)
} else if n != j {
return ErrorS3AWSDownloadError.Error(getError(errObjectSize, object))
return 0, nil, ErrorS3AWSIOReaderError.Error(getError(errObject, object))
} else {
return *r.ContentLength, r.Body, nil
}

return nil
}
39 changes: 19 additions & 20 deletions context/gin/interface.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
/***********************************************************************************************************************
/*
* MIT License
*
* MIT License
* Copyright (c) 2019 Nicolas JUHEL
*
* Copyright (c) 2022 Nicolas JUHEL
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
**********************************************************************************************************************/
*/

package gin

Expand Down
Loading

0 comments on commit 24e4191

Please sign in to comment.