Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Don't error out if storage key already exists #840

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/media/processingemoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (p *ProcessingEmoji) loadStatic(ctx context.Context) error {
}

// put the static in storage
if err := p.storage.Put(ctx, p.emoji.ImageStaticPath, static.small); err != nil {
if err := p.storage.Put(ctx, p.emoji.ImageStaticPath, static.small); err != nil && err != storage.ErrAlreadyExists {
p.err = fmt.Errorf("loadStatic: error storing static: %s", err)
atomic.StoreInt32(&p.staticState, int32(errored))
return p.err
Expand Down Expand Up @@ -217,7 +217,7 @@ func (p *ProcessingEmoji) store(ctx context.Context) error {
multiReader := io.MultiReader(bytes.NewBuffer(firstBytes), reader)

// store this for now -- other processes can pull it out of storage as they please
if err := p.storage.PutStream(ctx, p.emoji.ImagePath, multiReader); err != nil {
if err := p.storage.PutStream(ctx, p.emoji.ImagePath, multiReader); err != nil && err != storage.ErrAlreadyExists {
return fmt.Errorf("store: error storing stream: %s", err)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/media/processingmedia.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (p *ProcessingMedia) loadThumb(ctx context.Context) error {

// put the thumbnail in storage
log.Tracef("loadThumb: storing new thumbnail %s", p.attachment.URL)
if err := p.storage.Put(ctx, p.attachment.Thumbnail.Path, thumb.small); err != nil {
if err := p.storage.Put(ctx, p.attachment.Thumbnail.Path, thumb.small); err != nil && err != storage.ErrAlreadyExists {
p.err = fmt.Errorf("loadThumb: error storing thumbnail: %s", err)
atomic.StoreInt32(&p.thumbState, int32(errored))
return p.err
Expand Down Expand Up @@ -341,7 +341,7 @@ func (p *ProcessingMedia) store(ctx context.Context) error {
p.attachment.File.FileSize = fileSize

// store this for now -- other processes can pull it out of storage as they please
if err := p.storage.PutStream(ctx, p.attachment.File.Path, clean); err != nil {
if err := p.storage.PutStream(ctx, p.attachment.File.Path, clean); err != nil && err != storage.ErrAlreadyExists {
return fmt.Errorf("store: error storing stream: %s", err)
}

Expand Down
13 changes: 11 additions & 2 deletions internal/storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/url"

"codeberg.org/gruf/go-store/kv"
"codeberg.org/gruf/go-store/storage"
)

type Local struct {
Expand All @@ -39,11 +40,19 @@ func (l *Local) GetStream(ctx context.Context, key string) (io.ReadCloser, error
}

func (l *Local) PutStream(ctx context.Context, key string, r io.Reader) error {
return l.KVStore.PutStream(key, r)
err := l.KVStore.PutStream(key, r)
if err == storage.ErrAlreadyExists {
return ErrAlreadyExists
}
return err
}

func (l *Local) Put(ctx context.Context, key string, value []byte) error {
return l.KVStore.Put(key, value)
err := l.KVStore.Put(key, value)
if err == storage.ErrAlreadyExists {
return ErrAlreadyExists
}
return err
}

func (l *Local) Delete(ctx context.Context, key string) error {
Expand Down
1 change: 1 addition & 0 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
)

var ErrNotSupported = errors.New("driver does not suppport functionality")
var ErrAlreadyExists = errors.New("storage key already exists")

// Driver implements the functionality to store and retrieve blobs
// (images,video,audio)
Expand Down