Skip to content

Commit

Permalink
Package aws:
Browse files Browse the repository at this point in the history
- add new sub package pusher: pusher is an helper who's allow to push
  single put object or multipart upload and implement io.writer &
  io.readFrom interface

Other:
- bump dependencies
  • Loading branch information
nabbar committed Aug 10, 2024
1 parent b23addb commit adaab2a
Show file tree
Hide file tree
Showing 10 changed files with 1,551 additions and 0 deletions.
469 changes: 469 additions & 0 deletions aws/pusher/config.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions aws/pusher/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pusher

import "fmt"

var (
ErrInvalidInstance = fmt.Errorf("invalid instance")
ErrBytesWrote = fmt.Errorf("at least one byte is still in wrote")
ErrInvalidClient = fmt.Errorf("invalid aws S3 client")
ErrInvalidResponse = fmt.Errorf("invalid aws S3 response")
ErrInvalidUploadID = fmt.Errorf("invalid aws s3 MPU Upload ID")
ErrInvalidTMPFile = fmt.Errorf("invalid working or temporary file")
ErrEmptyContents = fmt.Errorf("no given contents")
ErrInvalidChecksum = fmt.Errorf("invalid checksum calculation for given data")
ErrWorkingPartFileExceedSize = fmt.Errorf("working or temporary file used exceed the aws S3 size limits")
)
97 changes: 97 additions & 0 deletions aws/pusher/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package pusher

import (
"io"
"os"

sdksss "github.com/aws/aws-sdk-go-v2/service/s3"
)

func (o *psh) getFile() (*os.File, error) {
if o == nil {
return nil, ErrInvalidInstance
} else if i := o.tmp.Load(); i != nil {
if v, k := i.(*os.File); !k {
return v, nil
}
}

if f, e := o.cfg.getWorkingFile(); f == nil || e != nil {
return nil, e
} else {
o.tmp.Store(f)
return f, nil
}
}

func (o *psh) fileReset() error {
if f, e := o.getFile(); e != nil {
return e
} else {
_, e = f.Seek(0, io.SeekStart)
o.tmp.Store(f)
return e
}
}

func (o *psh) fileTruncate() error {
if f, e := o.getFile(); e != nil {
return e
} else {
o.prtSize.Store(0)
e = f.Truncate(0)
o.tmp.Store(f)
return e
}
}

func (o *psh) fileClose() error {
if f, e := o.getFile(); e != nil {
return e
} else {
e = f.Close()
o.tmp.Store(f)
return e
}
}

func (o *psh) fileRemove() error {
if f, e := o.getFile(); e != nil {
return e
} else {
n := f.Name()
e = o.fileClose()

o.prtList.Store(make([]sdksss.UploadPartOutput, 0))
o.prtSize.Store(0)
o.objSize.Store(0)
o.nbrPart.Store(0)
o.updInfo.Store(&sdksss.CompleteMultipartUploadOutput{})
o.objInfo.Store(&sdksss.CreateMultipartUploadInput{})

_ = o.md5Reset()
_ = o.shaPartReset()

if er := os.Remove(n); er != nil && e != nil {
return e
} else if er != nil {
return er
}

return nil
}
}

func (o *psh) fileWrite(p []byte) (n int, err error) {
if f, e := o.getFile(); e != nil {
return 0, e
} else {
n, err = f.Write(p)

// updating size counters
o.prtSize.Add(int64(n))
o.objSize.Add(int64(n))

return n, err
}
}
198 changes: 198 additions & 0 deletions aws/pusher/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package pusher

import (
"crypto/md5"
"crypto/sha256"
"hash"
)

func (o *psh) md5Get() (hash.Hash, error) {
if o == nil {
return nil, ErrInvalidInstance
} else if i := o.prtMD5.Load(); i != nil {
if v, k := i.(hash.Hash); k {
return v, nil
}
}

return o.md5Init()
}

func (o *psh) md5Init() (hash.Hash, error) {
if o == nil {
return nil, ErrInvalidInstance
} else {
return md5.New(), nil
}
}

func (o *psh) md5Reset() error {
if o == nil {
return ErrInvalidInstance
} else if h, e := o.md5Init(); e != nil {
return e
} else if i := o.prtMD5.Swap(h); i == nil {
return nil
} else if v, k := i.(hash.Hash); !k {
return nil
} else {
v.Reset()
return nil
}
}

func (o *psh) md5Write(p []byte) (n int, err error) {
var (
e error
h hash.Hash
)

if o == nil {
return 0, ErrInvalidInstance
} else if h, e = o.md5Get(); e != nil {
return 0, e
} else {
n, e = h.Write(p)
o.prtMD5.Store(h)
return n, e
}
}

func (o *psh) md5Checksum() ([]byte, error) {
if o == nil {
return make([]byte, 0), ErrInvalidInstance
} else if i := o.prtMD5.Load(); i == nil {
return make([]byte, 0), ErrInvalidInstance
} else if v, k := i.(hash.Hash); !k {
return make([]byte, 0), ErrInvalidInstance
} else {
return v.Sum(nil), nil
}
}

func (o *psh) shaPartGet() (hash.Hash, error) {
if o == nil {
return nil, ErrInvalidInstance
} else if !o.cfg.isCheckSum() {
return nil, nil
} else if i := o.prtSha2.Load(); i != nil {
if v, k := i.(hash.Hash); k {
return v, nil
}
}

return o.shaPartInit()
}

func (o *psh) shaPartInit() (hash.Hash, error) {
if o == nil {
return nil, ErrInvalidInstance
} else {
return sha256.New(), nil
}
}

func (o *psh) shaPartReset() error {
if o == nil {
return ErrInvalidInstance
} else if !o.cfg.isCheckSum() {
return nil
} else if h, e := o.shaPartInit(); e != nil {
return e
} else if i := o.prtSha2.Swap(h); i == nil {
return nil
} else if v, k := i.(hash.Hash); !k {
return nil
} else {
v.Reset()
return nil
}
}

func (o *psh) shaPartWrite(p []byte) (n int, err error) {
var (
e error
h hash.Hash
)

if o == nil {
return 0, ErrInvalidInstance
} else if !o.cfg.isCheckSum() {
return len(p), nil
} else if h, e = o.shaPartGet(); e != nil {
return 0, e
} else {
n, e = h.Write(p)
o.prtSha2.Store(h)
return n, e
}
}

func (o *psh) shaPartChecksum() ([]byte, error) {
if o == nil {
return nil, ErrInvalidInstance
} else if !o.cfg.isCheckSum() {
return nil, nil
} else if i := o.prtSha2.Load(); i == nil {
return nil, ErrInvalidInstance
} else if v, k := i.(hash.Hash); !k {
return nil, ErrInvalidInstance
} else {
return v.Sum(nil), nil
}
}

func (o *psh) shaObjGet() (hash.Hash, error) {
if o == nil {
return nil, ErrInvalidInstance
} else if !o.cfg.isCheckSum() {
return nil, nil
} else if i := o.objSha2.Load(); i != nil {
if v, k := i.(hash.Hash); k {
return v, nil
}
}

return o.shaObjInit()
}

func (o *psh) shaObjInit() (hash.Hash, error) {
if o == nil {
return nil, ErrInvalidInstance
} else {
return sha256.New(), nil
}
}

func (o *psh) shaObjWrite(p []byte) (n int, err error) {
var (
e error
h hash.Hash
)

if o == nil {
return 0, ErrInvalidInstance
} else if !o.cfg.isCheckSum() {
return len(p), nil
} else if h, e = o.shaObjGet(); e != nil {
return 0, e
} else {
n, e = h.Write(p)
o.objSha2.Store(h)
return n, e
}
}

func (o *psh) shaObjChecksum() ([]byte, error) {
if o == nil {
return nil, ErrInvalidInstance
} else if !o.cfg.isCheckSum() {
return nil, nil
} else if i := o.objSha2.Load(); i == nil {
return nil, ErrInvalidInstance
} else if v, k := i.(hash.Hash); !k {
return nil, ErrInvalidInstance
} else {
return v.Sum(nil), nil
}
}
Loading

0 comments on commit adaab2a

Please sign in to comment.