Skip to content

Commit

Permalink
Merge pull request #22 from ulule/feat/add-open-with-stat
Browse files Browse the repository at this point in the history
feat: add open with stat
  • Loading branch information
thoas authored Jul 10, 2023
2 parents 11134a4 + d429b21 commit bfbe526
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
19 changes: 17 additions & 2 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package fs

import (
"context"
"github.com/ulule/gostorages"
"io"
"os"
"path/filepath"

"github.com/ulule/gostorages"
)

// Storage is a filesystem storage.
Expand Down Expand Up @@ -75,3 +74,19 @@ func (fs *Storage) Open(ctx context.Context, path string) (io.ReadCloser, error)
func (fs *Storage) Delete(ctx context.Context, path string) error {
return os.Remove(fs.abs(path))
}

// OpenWithStat opens path for reading with file stats.
func (fs *Storage) OpenWithStat(ctx context.Context, path string) (io.ReadCloser, *gostorages.Stat, error) {
f, err := os.Open(fs.abs(path))
if os.IsNotExist(err) {
return nil, nil, gostorages.ErrNotExist
}
stat, err := f.Stat()
if err != nil {
return nil, nil, err
}
return f, &gostorages.Stat{
ModifiedTime: stat.ModTime(),
Size: stat.Size(),
}, nil
}
20 changes: 16 additions & 4 deletions gcs/gcs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package gcs

import (
"cloud.google.com/go/storage"
"context"
"github.com/ulule/gostorages"
"google.golang.org/api/option"
"io"
"mime"
"path/filepath"

"cloud.google.com/go/storage"
"github.com/ulule/gostorages"
"google.golang.org/api/option"
)

// Storage is a gcs storage.
Expand Down Expand Up @@ -65,10 +64,23 @@ func (g *Storage) Open(ctx context.Context, path string) (io.ReadCloser, error)
if err == storage.ErrObjectNotExist {
return nil, gostorages.ErrNotExist
}

return r, err
}

// Delete deletes path.
func (g *Storage) Delete(ctx context.Context, path string) error {
return g.bucket.Object(path).Delete(ctx)
}

// OpenWithStat opens path for reading with file stats.
func (g *Storage) OpenWithStat(ctx context.Context, path string) (io.ReadCloser, *gostorages.Stat, error) {
r, err := g.bucket.Object(path).NewReader(ctx)
if err == storage.ErrObjectNotExist {
return nil, nil, gostorages.ErrNotExist
}
return r, &gostorages.Stat{
ModifiedTime: r.Attrs.LastModified,
Size: r.Attrs.Size,
}, err
}
5 changes: 4 additions & 1 deletion noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ type noop struct{}
func (noop) Save(ctx context.Context, content io.Reader, path string) error { return nil }
func (noop) Stat(ctx context.Context, path string) (*Stat, error) { return nil, nil }
func (noop) Open(ctx context.Context, path string) (io.ReadCloser, error) { return nil, nil }
func (noop) Delete(ctx context.Context, path string) error { return nil }
func (noop) OpenWithStat(ctx context.Context, path string) (io.ReadCloser, *Stat, error) {
return nil, nil, nil
}
func (noop) Delete(ctx context.Context, path string) error { return nil }
27 changes: 22 additions & 5 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package s3
import (
"bytes"
"context"
"io"
"mime"
"net/http"
"path/filepath"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/ulule/gostorages"
"io"
"mime"
"net/http"
"path/filepath"
)

// Storage is a s3 storage.
Expand Down Expand Up @@ -128,3 +127,21 @@ func (s *Storage) Delete(ctx context.Context, path string) error {
_, err := s.s3.DeleteObjectWithContext(ctx, input)
return err
}

// OpenWithStat opens path for reading with file stats.
func (s *Storage) OpenWithStat(ctx context.Context, path string) (io.ReadCloser, *gostorages.Stat, error) {
input := &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
out, err := s.s3.GetObjectWithContext(ctx, input)
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == s3.ErrCodeNoSuchKey {
return nil, nil, gostorages.ErrNotExist
} else if err != nil {
return nil, nil, err
}
return out.Body, &gostorages.Stat{
ModifiedTime: *out.LastModified,
Size: *out.ContentLength,
}, nil
}
1 change: 1 addition & 0 deletions storages.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Storage interface {
Save(ctx context.Context, content io.Reader, path string) error
Stat(ctx context.Context, path string) (*Stat, error)
Open(ctx context.Context, path string) (io.ReadCloser, error)
OpenWithStat(ctx context.Context, path string) (io.ReadCloser, *Stat, error)
Delete(ctx context.Context, path string) error
}

Expand Down

0 comments on commit bfbe526

Please sign in to comment.