Skip to content

Commit d870386

Browse files
committed
list share
1 parent ccd9e2f commit d870386

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

code/go/0chain.net/blobbercore/handler/handler.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ func setupHandlers(r *mux.Router) {
194194
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(RevokeShare)))).
195195
Methods(http.MethodOptions, http.MethodDelete)
196196

197+
// list files shared in this allocation
198+
r.HandleFunc("/v1/marketplace/shareinfo/{allocation}",
199+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(ListShare)))).
200+
Methods(http.MethodOptions, http.MethodGet)
201+
197202
// lightweight http handler without heavy postgres transaction to improve performance
198203

199204
r.HandleFunc("/v1/writemarker/lock/{allocation}",
@@ -648,7 +653,43 @@ func InsertShare(ctx context.Context, r *http.Request) (interface{}, error) {
648653
return map[string]interface{}{"message": "Share info added successfully"}, nil
649654
}
650655

651-
//PrintCSS - print the common css elements
656+
// ListShare a list of files that clientID has shared
657+
func ListShare(ctx context.Context, r *http.Request) (interface{}, error) {
658+
659+
ctx = setupHandlerContext(ctx, r)
660+
661+
var (
662+
allocationID = ctx.Value(constants.ContextKeyAllocation).(string)
663+
clientID = ctx.Value(constants.ContextKeyClient).(string)
664+
)
665+
666+
allocationObj, err := storageHandler.verifyAllocation(ctx, allocationID, true)
667+
if err != nil {
668+
return nil, common.NewError("invalid_parameters", "Invalid allocation id passed."+err.Error())
669+
}
670+
671+
sign := r.Header.Get(common.ClientSignatureHeader)
672+
673+
valid, err := verifySignatureFromRequest(allocationID, sign, allocationObj.OwnerPublicKey)
674+
if !valid || err != nil {
675+
return nil, common.NewError("invalid_signature", "Invalid signature")
676+
}
677+
678+
if clientID != allocationObj.OwnerID {
679+
return nil, common.NewError("invalid_client", "Client has no access to share file")
680+
}
681+
682+
shares, err := reference.ListShareInfoClientID(ctx, clientID)
683+
if err != nil {
684+
Logger.Error("failed_to_list_share", zap.Error(err))
685+
return nil, common.NewError("failed_to_list_share", "failed to list file share")
686+
}
687+
688+
// get the files shared in that allocation
689+
return shares, nil
690+
}
691+
692+
// PrintCSS - print the common css elements
652693
func PrintCSS(w http.ResponseWriter) {
653694
fmt.Fprintf(w, "<style>\n")
654695
fmt.Fprintf(w, ".number { text-align: right; }\n")

code/go/0chain.net/blobbercore/handler/handler_share_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
const (
3232
insertShare = "InsertShare"
3333
revokeShare = "RevokeShare"
34+
listShare = "ListShare"
3435
)
3536

3637
func setupShareHandlers() (*mux.Router, map[string]string) {
@@ -45,10 +46,15 @@ func setupShareHandlers() (*mux.Router, map[string]string) {
4546
common.ToJSONResponse(
4647
WithReadOnlyConnection(RevokeShare))).Name(revokeShare).Methods(http.MethodDelete)
4748

49+
router.HandleFunc(sharePath,
50+
common.ToJSONResponse(
51+
WithReadOnlyConnection(ListShare))).Name(revokeShare).Methods(http.MethodGet)
52+
4853
return router,
4954
map[string]string{
5055
insertShare: http.MethodPost,
5156
revokeShare: http.MethodDelete,
57+
listShare: http.MethodGet,
5258
}
5359
}
5460

code/go/0chain.net/blobbercore/reference/shareinfo.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ func AddShareInfo(ctx context.Context, shareInfo *ShareInfo) error {
3030
return db.Model(&ShareInfo{}).Create(shareInfo).Error
3131
}
3232

33+
// ListShareInfo returns list of files I shared?? or list of files shared with??
34+
func ListShareInfoOwnerID(ctx context.Context, ownerID string) error {
35+
db := datastore.GetStore().GetTransaction(ctx)
36+
return db.Model(&ShareInfo{}).Where("owner_id=?", ownerID).Error // todo: fix
37+
}
38+
39+
// ListShareInfo returns list of files I shared?? or list of files shared with??
40+
func ListShareInfoClientID(ctx context.Context, clientID string) ([]ShareInfo, error) {
41+
db := datastore.GetStore().GetTransaction(ctx)
42+
var shares []ShareInfo
43+
err := db.Where("client_id <> ?", clientID).Find(&shares).Error
44+
return shares, err
45+
}
46+
3347
func DeleteShareInfo(ctx context.Context, shareInfo *ShareInfo) error {
3448
db := datastore.GetStore().GetTransaction(ctx)
3549

0 commit comments

Comments
 (0)