Skip to content

Commit

Permalink
fix push "trailing data" error
Browse files Browse the repository at this point in the history
Signed-off-by: Adphi <[email protected]>
  • Loading branch information
Adphi committed May 6, 2024
1 parent 685b636 commit d33a3bb
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/packages/apk/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type FileMetadata struct {
}

// NewPackage parses the Alpine package file
func NewPackage(r io.Reader, branch, repository string, size int64) (*Package, error) {
func NewPackage(r io.Reader, branch, repository string) (*Package, error) {
// Alpine packages are concated .tar.gz streams. Usually the first stream contains the package metadata.
reader, err := buffer.CreateHashedBufferFromReader(r)
if err != nil {
Expand Down Expand Up @@ -161,7 +161,7 @@ func NewPackage(r io.Reader, branch, repository string, size int64) (*Package, e
_, _, sha256, _ := reader.Sums()
p.reader = reader
p.PkgDigest = hex.EncodeToString(sha256)
p.PkgSize = size
p.PkgSize = reader.Size()
p.FilePath = fmt.Sprintf("%s/%s/%s/%s-%s.apk", p.Branch, p.Repo, p.FileMetadata.Architecture, p.PkgName, p.PkgVersion)
_, err = reader.Seek(0, io.SeekStart)
return p, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/packages/apk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func (p *provider) Routes() []*packages.Route {
{
Method: http.MethodPut,
Path: "/{branch}/{repository}/push",
Handler: packages.Push(func(r *http.Request, reader io.Reader, size int64, key string) (storage.Artifact, error) {
Handler: packages.Push(func(r *http.Request, reader io.Reader, key string) (storage.Artifact, error) {
branch, repo := mux.Vars(r)["branch"], mux.Vars(r)["repository"]
return NewPackage(reader, branch, repo, size)
return NewPackage(reader, branch, repo)
}),
},
{
Expand Down
3 changes: 2 additions & 1 deletion pkg/packages/deb/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ type Metadata struct {

// NewPackage parses the Debian package file
// https://manpages.debian.org/bullseye/dpkg-dev/deb.5.en.html
func NewPackage(r io.Reader, distribution, component string, size int64) (*Package, error) {
func NewPackage(r io.Reader, distribution, component string) (*Package, error) {
reader, err := buffer.CreateHashedBufferFromReader(r)
if err != nil {
return nil, err
}
size := reader.Size()
pkg, err := parsePackage(reader)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/packages/deb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ func (p *provider) Routes() []*packages.Route {
{
Method: http.MethodPut,
Path: "/pool/{distribution}/{component}/push",
Handler: packages.Push(func(r *http.Request, reader io.Reader, size int64, key string) (storage.Artifact, error) {
Handler: packages.Push(func(r *http.Request, reader io.Reader, key string) (storage.Artifact, error) {
distribution, component := mux.Vars(r)["distribution"], mux.Vars(r)["component"]
return NewPackage(reader, distribution, component, size)
return NewPackage(reader, distribution, component)
}),
},
{
Expand Down
16 changes: 7 additions & 9 deletions pkg/packages/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ import (
"go.linka.cloud/artifact-registry/pkg/storage"
)

type ArtifactFactory func(r *http.Request, reader io.Reader, size int64, key string) (storage.Artifact, error)
type ArtifactFactory func(r *http.Request, reader io.Reader, key string) (storage.Artifact, error)

func Push(fn ArtifactFactory) HandlerFunc {
return func(_ string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var (
reader io.ReadCloser
size int64
)
if file, header, err := r.FormFile("file"); err == nil {
reader, size = file, header.Size

var reader io.ReadCloser
if file, _, err := r.FormFile("file"); err == nil {
reader = file
} else {
reader, size = r.Body, r.ContentLength
reader = r.Body
}
defer reader.Close()
logger.C(ctx).Debugf("ensuring storage is initialized")
Expand All @@ -46,7 +44,7 @@ func Push(fn ArtifactFactory) HandlerFunc {
return
}
logger.C(ctx).Debugf("parsing artifact")
pkg, err := fn(r, reader, size, s.Key())
pkg, err := fn(r, reader, s.Key())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/packages/helm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (p *provider) Routes() []*packages.Route {
{
Path: "/push",
Method: http.MethodPut,
Handler: packages.Push(func(r *http.Request, reader io.Reader, size int64, key string) (storage.Artifact, error) {
Handler: packages.Push(func(r *http.Request, reader io.Reader, key string) (storage.Artifact, error) {
return NewPackage(reader)
}),
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/packages/rpm/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type Changelog struct {
Text string `json:"text,omitempty" xml:",chardata"`
}

func NewPackage(r io.Reader, size int64, key string) (*Package, error) {
func NewPackage(r io.Reader, key string) (*Package, error) {
// sign
buf, err := buffer.CreateHashedBufferFromReader(r)
if err != nil {
Expand All @@ -160,7 +160,7 @@ func NewPackage(r io.Reader, size int64, key string) (*Package, error) {
if err != nil {
return nil, err
}
pkg.FileSize = size + ssize - seek
pkg.FileSize = buf.Size() + ssize - seek

_, _, sha256, _ := reader.Sums()
pkg.HashSHA256 = hex.EncodeToString(sha256)
Expand Down
4 changes: 2 additions & 2 deletions pkg/packages/rpm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func (p *provider) Routes() []*packages.Route {
{
Method: http.MethodPut,
Path: "/push",
Handler: packages.Push(func(r *http.Request, reader io.Reader, size int64, key string) (storage.Artifact, error) {
return NewPackage(reader, size, key)
Handler: packages.Push(func(r *http.Request, reader io.Reader, key string) (storage.Artifact, error) {
return NewPackage(reader, key)
}),
},
{
Expand Down

0 comments on commit d33a3bb

Please sign in to comment.