Skip to content

Commit

Permalink
fix: 404 errors on backups (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning authored Feb 20, 2022
1 parent 3d635b5 commit bbfe419
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/handlers/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package handlers

import (
"github.com/getfider/fider/app/pkg/backup"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/web"
)

Expand All @@ -11,6 +13,7 @@ func ExportBackupZip() web.HandlerFunc {

file, err := backup.Create(c)
if err != nil {
log.Error(c, errors.Wrap(err, "failed to create backup"))
return c.Failure(err)
}

Expand Down
2 changes: 1 addition & 1 deletion app/pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Create(ctx context.Context) (*bytes.Buffer, error) {
func addBlobToZipFile(ctx context.Context, zipWriter *zip.Writer, bkey string) error {
getBlob := &query.GetBlobByKey{Key: bkey}
if err := bus.Dispatch(ctx, getBlob); err != nil {
return err
return errors.Wrap(err, "failed to get blob with key %s", bkey)
}

fileName := fmt.Sprintf("blobs/%s", bkey)
Expand Down
8 changes: 4 additions & 4 deletions app/services/blob/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func AllOperations(ctx context.Context) {
}
err = bus.Dispatch(ctx, q)
Expect(q.Result).IsNil()
Expect(err).Equals(blob.ErrNotFound)
Expect(errors.Cause(err)).Equals(blob.ErrNotFound)
}
}

Expand Down Expand Up @@ -163,12 +163,12 @@ func SameKey_DifferentTenant(ctx context.Context) {

q = &query.GetBlobByKey{Key: key}
err = bus.Dispatch(ctxWithTenant2, q)
Expect(err).Equals(blob.ErrNotFound)
Expect(errors.Cause(err)).Equals(blob.ErrNotFound)
Expect(q.Result).IsNil()

q = &query.GetBlobByKey{Key: key}
err = bus.Dispatch(ctx, q)
Expect(err).Equals(blob.ErrNotFound)
Expect(errors.Cause(err)).Equals(blob.ErrNotFound)
Expect(q.Result).IsNil()

err = bus.Dispatch(ctxWithTenant1, &cmd.DeleteBlob{Key: key})
Expand Down Expand Up @@ -217,7 +217,7 @@ func SameKey_DifferentTenant_Delete(ctx context.Context) {

q = &query.GetBlobByKey{Key: key}
err = bus.Dispatch(ctx, q)
Expect(err).Equals(blob.ErrNotFound)
Expect(errors.Cause(err)).Equals(blob.ErrNotFound)
Expect(q.Result).IsNil()

err = bus.Dispatch(ctxWithTenant2, &cmd.DeleteBlob{Key: key})
Expand Down
11 changes: 8 additions & 3 deletions app/services/blob/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package s3
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"path"
"sort"
Expand Down Expand Up @@ -76,13 +77,17 @@ func listBlobs(ctx context.Context, q *query.ListBlobs) error {
prefix := basePath(ctx, q.Prefix)
response, err := DefaultClient.ListObjectsWithContext(ctx, &s3.ListObjectsInput{
Bucket: aws.String(env.Config.BlobStorage.S3.BucketName),
MaxKeys: aws.Int64(3000),
MaxKeys: aws.Int64(1000),
Prefix: aws.String(prefix),
})
if err != nil {
return wrap(err, "failed to list blobs from S3")
}

if response.IsTruncated != nil && *response.IsTruncated {
return wrap(err, "failed to return list of blobs because it was truncated")
}

files := make([]string, 0)
for _, item := range response.Contents {
key := *item.Key
Expand All @@ -108,7 +113,7 @@ func getBlobByKey(ctx context.Context, q *query.GetBlobByKey) error {
})
if err != nil {
if isNotFound(err) {
return blob.ErrNotFound
return wrap(blob.ErrNotFound, "unable to find blob '%s' on S3", q.Key)
}
return wrap(err, "failed to get blob '%s' from S3", q.Key)
}
Expand Down Expand Up @@ -166,7 +171,7 @@ func basePath(ctx context.Context, segment string) string {

tenant, ok := ctx.Value(app.TenantCtxKey).(*entity.Tenant)
if ok {
return path.Join("tenants", strconv.Itoa(tenant.ID), segment)
return fmt.Sprintf("tenants/%s/%s", strconv.Itoa(tenant.ID), segment)
}
return segment
}
Expand Down

0 comments on commit bbfe419

Please sign in to comment.