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

Hotfix/sharing: some more bugs with sharing and mirror file creation #161

Merged
merged 5 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 core/space/services/services_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func (s *Space) addFile(ctx context.Context, sourcePath string, targetPath strin
return domain.AddItemResult{}, err
}

if s.tc.IsBucketBackup(ctx, b.Slug()) && !s.tc.IsMirrorFile(ctx, targetPath, b.Slug()) {
if s.tc.IsBucketBackup(ctx, b.Slug()) && !s.tc.IsMirrorFile(ctx, targetPathBucket, b.Slug()) {
f.Seek(0, io.SeekStart)

_, _, err = s.tc.UploadFileToHub(ctx, b, targetPathBucket, f)
Expand All @@ -502,7 +502,7 @@ func (s *Space) addFile(ctx context.Context, sourcePath string, targetPath strin
return domain.AddItemResult{}, err
}

_, err = s.tc.MarkMirrorFileBackup(ctx, targetPath, b.Slug())
_, err = s.tc.MarkMirrorFileBackup(ctx, targetPathBucket, b.Slug())
if err != nil {
log.Error(fmt.Sprintf("error creating mirror file Path=%s BucketSlug=%s", targetPathBucket, b.Key()), err)
return domain.AddItemResult{}, err
Expand Down
32 changes: 17 additions & 15 deletions core/space/services/services_sharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

"github.com/FleekHQ/space-daemon/log"
crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/pkg/errors"

"github.com/textileio/dcrypto"

"github.com/pkg/errors"

"github.com/FleekHQ/space-daemon/core/space/domain"
t "github.com/FleekHQ/space-daemon/core/textile"
"github.com/ipfs/go-cid"
)

Expand Down Expand Up @@ -226,11 +226,12 @@ func (s *Space) OpenSharedFile(ctx context.Context, hash, password, filename str
}, nil
}

func (s *Space) ShareFilesViaPublicKey(ctx context.Context, paths []domain.FullPath, pubkeys []crypto.PubKey) error {
func (s *Space) ShareFilesViaPublicKey(ctx context.Context, paths *[]domain.FullPath, pubkeys []crypto.PubKey) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment on this function that paths would be modified. Personally, I think it would have been better if the function returned the modified paths instead of mutating the input value, but eitherways a comment should help make it more apparent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 26a4678.

m := s.tc.GetModel()

var enckeys [][]byte
for i, path := range paths {
enckeys := make([][]byte, len(*paths))
for i, path := range *paths {

// this handles personal bucket since for shared-with-me files
// the dbid will be preset
if path.DbId == "" {
Expand All @@ -239,14 +240,14 @@ func (s *Space) ShareFilesViaPublicKey(ctx context.Context, paths []domain.FullP
return err
}

bs, err := m.FindBucket(ctx, b.GetData().Name)
bs, err := m.FindBucket(ctx, b.Slug())
if err != nil {
return err
}
path.DbId = bs.RemoteBucketKey
(*paths)[i].DbId = bs.RemoteDbID
}

if path.Bucket == "" {
if path.Bucket == "" || path.Bucket == t.GetDefaultBucketSlug() {
b, err := s.tc.GetDefaultBucket(ctx)
if err != nil {
return err
Expand All @@ -255,28 +256,29 @@ func (s *Space) ShareFilesViaPublicKey(ctx context.Context, paths []domain.FullP
if err != nil {
return err
}
path.Bucket = b.Slug()
path.BucketKey = bs.RemoteBucketKey

enckeys[i] = bs.EncryptionKey
(*paths)[i].Bucket = b.Slug()
(*paths)[i].BucketKey = bs.RemoteBucketKey
enckeys = append(enckeys, bs.EncryptionKey)
} else {
r, err := m.FindReceivedFile(ctx, path)
if err != nil {
return err
}
enckeys[i] = r.EncryptionKey
(*paths)[i].Bucket = r.Bucket
(*paths)[i].BucketKey = r.BucketKey
enckeys = append(enckeys, r.EncryptionKey)
}
}

err := s.tc.ShareFilesViaPublicKey(ctx, paths, pubkeys, enckeys)
err := s.tc.ShareFilesViaPublicKey(ctx, *paths, pubkeys, enckeys)
if err != nil {
return err
}

for _, pk := range pubkeys {

d := &domain.Invitation{
ItemPaths: paths,
ItemPaths: *paths,
Keys: enckeys,
}

Expand Down
2 changes: 1 addition & 1 deletion core/space/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Service interface {
RecoverKeysByLocalBackup(ctx context.Context, pathToKeyBackup string) error
GetNotifications(ctx context.Context, seek string, limit int) ([]*domain.Notification, error)
ToggleBucketBackup(ctx context.Context, bucketName string, bucketBackup bool) error
ShareFilesViaPublicKey(ctx context.Context, paths []domain.FullPath, pubkeys []crypto.PubKey) error
ShareFilesViaPublicKey(ctx context.Context, paths *[]domain.FullPath, pubkeys []crypto.PubKey) error
HandleSharedFilesInvitation(ctx context.Context, invitationId string, accept bool) error
GetAPISessionTokens(ctx context.Context) (*domain.APISessionTokens, error)
AddRecentlySharedPublicKeys(ctx context.Context, pubkeys []crypto.PubKey) error
Expand Down
6 changes: 5 additions & 1 deletion core/textile/bucket_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (tc *textileClient) createBucket(ctx context.Context, bucketSlug string) (B

// We store the bucket in a meta thread so that we can later fetch a list of all buckets
log.Debug("Bucket " + bucketSlug + " created. Storing metadata.")
schema, err := m.CreateBucket(ctx, bucketSlug, dbID.String())
schema, err := m.CreateBucket(ctx, bucketSlug, utils.CastDbIDToString(*dbID))
jsonsivar marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -333,3 +333,7 @@ func (tc *textileClient) IsBucketBackup(ctx context.Context, bucketSlug string)

return bucketSchema.Backup
}

func GetDefaultBucketSlug() string {
return defaultPersonalBucketSlug
}
11 changes: 8 additions & 3 deletions core/textile/model/mirror_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type MirrorBucketSchema struct {
const mirrorFileModelName = "MirrorFile"

var errMirrorFileNotFound = errors.New("Mirror file not found")
var errMirrorFileAlreadyExists = errors.New("Mirror file already exists")

func (m *model) CreateMirrorBucket(ctx context.Context, bucketSlug string, mirrorBucket *MirrorBucketSchema) (*BucketSchema, error) {
metaCtx, metaDbID, err := m.initBucketModel(ctx)
Expand Down Expand Up @@ -71,12 +72,12 @@ func (m *model) FindMirrorFileByPathAndBucketSlug(ctx context.Context, path, buc
}

if rawMirrorFiles == nil {
return &MirrorFileSchema{}, nil
return nil, nil
}

mirror_files := rawMirrorFiles.([]*MirrorFileSchema)
if len(mirror_files) == 0 {
return &MirrorFileSchema{}, nil
return nil, nil
}

log.Debug("Model.FindMirrorFileByPathAndBucketSlug: returning mirror file with dbid " + mirror_files[0].DbID)
Expand All @@ -89,11 +90,15 @@ func (m *model) CreateMirrorFile(ctx context.Context, mirrorFile *domain.MirrorF
return nil, err
}

_, err = m.FindMirrorFileByPathAndBucketSlug(ctx, mirrorFile.Path, mirrorFile.BucketSlug)
mf, err := m.FindMirrorFileByPathAndBucketSlug(ctx, mirrorFile.Path, mirrorFile.BucketSlug)
if err != nil {
return nil, err
}

if mf != nil {
return nil, errMirrorFileAlreadyExists
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsonsivar

I wonder: should we return err or just return mf, nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will leave this as-is for now and can update later since it's working at least. If we need a create or get type usage if this later we can always update.

}

newInstance := &MirrorFileSchema{
Path: mirrorFile.Path,
BucketSlug: mirrorFile.BucketSlug,
Expand Down
2 changes: 1 addition & 1 deletion core/textile/sharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (tc *textileClient) ShareFilesViaPublicKey(ctx context.Context, paths []dom
}

log.Info("Adding roles for pth: " + pth.Path)
var roles map[string]buckets.Role
roles := make(map[string]buckets.Role)
for _, pk := range pubkeys {
pkb, err := pk.Bytes()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion grpc/handlers_sharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (srv *grpcServer) ShareFilesViaPublicKey(ctx context.Context, request *pb.S
return nil, err
}

err = srv.sv.ShareFilesViaPublicKey(ctx, cleanedPaths, pks)
err = srv.sv.ShareFilesViaPublicKey(ctx, &cleanedPaths, pks)
if err != nil {
return nil, err
}
Expand Down