Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
Fixup storage.go
Browse files Browse the repository at this point in the history
Add file writer and file size functions to posix backend
  • Loading branch information
jbygdell committed Jun 11, 2020
1 parent c44e2af commit c87bbfc
Showing 1 changed file with 48 additions and 32 deletions.
80 changes: 48 additions & 32 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ import (

// Backend defines methods to be implemented by PosixBackend and S3Backend
type Backend interface {
ReadFile(filePath string) io.Reader
WriteFile(filePath string, f io.Reader)
GetSize(filepath string) int64
GetFileSize(filePath string) (int64, error)
ReadFile(filePath string) (io.Reader, error)
WriteFile(filePath string) (io.Writer, error)
}

// PosixBackend encapsulates an io.Reader instance
type PosixBackend struct {
FileReader io.Reader
FileWriter io.Writer
Location string
Size int64
}

// PosixConf stores information about the POSIX storage backend
Expand All @@ -42,26 +44,36 @@ func NewPosixBackend(c PosixConf) *PosixBackend {
}

// ReadFile returns an io.Reader instance
func (pb *PosixBackend) ReadFile(filePath string) io.Reader {
f := filepath.Join(filepath.Clean(pb.Location), filePath)
file, err := os.Open(f)
func (pb *PosixBackend) ReadFile(filePath string) (io.Reader, error) {
file, err := os.Open(filepath.Join(filepath.Clean(pb.Location), filePath))
if err != nil {
log.Error(err)
return nil, err
}
pb.FileReader = file

return pb.FileReader
return file, nil
}

// WriteFile writes an io.Reader to a specific location
func (pb *PosixBackend) WriteFile(filePath string, f io.Reader) {
// TODO
// WriteFile returns an io.Writer instance
func (pb *PosixBackend) WriteFile(filePath string) (io.Writer, error) {
file, err := os.OpenFile(filepath.Join(filepath.Clean(pb.Location), filePath), os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0640)
if err != nil {
log.Error(err)
return nil, err
}

return file, nil
}

// GetSize returns the size of a specific file
func (pb *PosixBackend) GetSize(filePath string) int64 {
// TODO
return 0
// GetFileSize returns the size of the file
func (pb *PosixBackend) GetFileSize(filePath string) (int64, error) {
stat, err := os.Stat(filepath.Join(filepath.Clean(pb.Location), filePath))
if err != nil {
log.Error(err)
return 0, err
}

return stat.Size(), nil
}

// S3Backend encapsulates a S3 client instance
Expand Down Expand Up @@ -99,42 +111,46 @@ func NewS3Backend(c S3Conf, trConf http.RoundTripper) *S3Backend {
}

// ReadFile returns an io.Reader instance
func (sb *S3Backend) ReadFile(filePath string) io.Reader {
func (sb *S3Backend) ReadFile(filePath string) (io.Reader, error) {
r, err := sb.Client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(sb.Bucket),
Key: aws.String(filePath)})

if err != nil {
log.Println(err)
return nil, err
}

return r.Body
return r.Body, nil
}

// WriteFile uploads the contents of an io.Reader to a S3 bucket
func (sb *S3Backend) WriteFile(filePath string, f io.Reader) {
result, err := sb.Uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(sb.Bucket),
Key: aws.String(filePath),
Body: f,
})

if err != nil {
log.Println("failed to upload file", err)
} else {
log.Println("File uploaded to", result.Location)
}
func (sb *S3Backend) WriteFile(filePath string) (io.Writer, error) {
// result, err := sb.Uploader.Upload(&s3manager.UploadInput{
// Bucket: aws.String(sb.Bucket),
// Key: aws.String(filePath),
// Body: bytes.NewReader(buffer),
// })

// if err != nil {
// log.Println("failed to upload file", err)
// } else {
// log.Println("File uploaded to", result.Location)
// }

return nil, nil
}

// GetSize returns the size of a specific object
func (sb *S3Backend) GetSize(filePath string) int64 {
// GetFileSize returns the size of a specific object
func (sb *S3Backend) GetFileSize(filePath string) (int64, error) {
r, err := sb.Client.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(sb.Bucket),
Key: aws.String(filePath)})

if err != nil {
log.Println(err)
return 0, err
}

return *r.ContentLength
return *r.ContentLength, nil
}

0 comments on commit c87bbfc

Please sign in to comment.