Skip to content

Commit 89a6ab5

Browse files
authored
Fix/delete requests (#672)
1 parent cb46c97 commit 89a6ab5

File tree

12 files changed

+184
-114
lines changed

12 files changed

+184
-114
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414

1515
// FileCommand execute command for a file operation
1616
type FileCommand interface {
17+
18+
// GetExistingFileRef get file ref if it exists
19+
GetExistingFileRef() *reference.Ref
20+
1721
// IsValidated validate request, and try build ChangeProcesser instance
1822
IsValidated(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, clientID string) error
1923

@@ -35,7 +39,7 @@ func createFileCommand(req *http.Request) FileCommand {
3539
case http.MethodPut:
3640
return &UpdateFileCommand{}
3741
case http.MethodDelete:
38-
return &FileCommandDelete{}
42+
return &DeleteFileCommand{}
3943

4044
default:
4145
return &UploadFileCommand{}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ import (
1414
"github.com/0chain/blobber/code/go/0chain.net/core/common"
1515
)
1616

17-
// FileCommandDelete command for deleting file
18-
type FileCommandDelete struct {
17+
// DeleteFileCommand command for deleting file
18+
type DeleteFileCommand struct {
1919
existingFileRef *reference.Ref
2020
changeProcessor *allocation.DeleteFileChange
2121
allocationChange *allocation.AllocationChange
2222
}
2323

24+
func (cmd *DeleteFileCommand) GetExistingFileRef() *reference.Ref {
25+
return cmd.existingFileRef
26+
}
27+
2428
// IsValidated validate request.
25-
func (cmd *FileCommandDelete) IsValidated(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, clientID string) error {
29+
func (cmd *DeleteFileCommand) IsValidated(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, clientID string) error {
2630
if allocationObj.OwnerID != clientID && allocationObj.RepairerID != clientID {
2731
return common.NewError("invalid_operation", "Operation needs to be performed by the owner or the payer of the allocation")
2832
}
2933

30-
path := req.FormValue("path")
31-
if path == "" {
34+
path, ok := GetField(req, "path")
35+
if !ok {
3236
return common.NewError("invalid_parameters", "Invalid path")
3337
}
3438
var err error
@@ -43,14 +47,14 @@ func (cmd *FileCommandDelete) IsValidated(ctx context.Context, req *http.Request
4347
}
4448

4549
// UpdateChange add DeleteFileChange in db
46-
func (cmd *FileCommandDelete) UpdateChange(ctx context.Context, connectionObj *allocation.AllocationChangeCollector) error {
50+
func (cmd *DeleteFileCommand) UpdateChange(ctx context.Context, connectionObj *allocation.AllocationChangeCollector) error {
4751
connectionObj.AddChange(cmd.allocationChange, cmd.changeProcessor)
4852

4953
return connectionObj.Save(ctx)
5054
}
5155

5256
// ProcessContent flush file to FileStorage
53-
func (cmd *FileCommandDelete) ProcessContent(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, connectionObj *allocation.AllocationChangeCollector) (blobberhttp.UploadResult, error) {
57+
func (cmd *DeleteFileCommand) ProcessContent(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, connectionObj *allocation.AllocationChangeCollector) (blobberhttp.UploadResult, error) {
5458
deleteSize := cmd.existingFileRef.Size
5559

5660
cmd.changeProcessor = &allocation.DeleteFileChange{ConnectionID: connectionObj.ID,
@@ -74,7 +78,7 @@ func (cmd *FileCommandDelete) ProcessContent(ctx context.Context, req *http.Requ
7478
}
7579

7680
// ProcessThumbnail no thumbnail should be processed for delete. A deffered delete command has been added on ProcessContent
77-
func (cmd *FileCommandDelete) ProcessThumbnail(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, connectionObj *allocation.AllocationChangeCollector) error {
81+
func (cmd *DeleteFileCommand) ProcessThumbnail(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, connectionObj *allocation.AllocationChangeCollector) error {
7882
//DO NOTHING
7983
return nil
8084
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ type UpdateFileCommand struct {
2828
allocationChange *allocation.AllocationChange
2929
}
3030

31+
func (cmd *UpdateFileCommand) GetExistingFileRef() *reference.Ref {
32+
return cmd.existingFileRef
33+
}
34+
3135
// IsValidated validate request.
3236
func (cmd *UpdateFileCommand) IsValidated(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, clientID string) error {
3337
uploadMetaString := req.FormValue(UploadMeta)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type UploadFileCommand struct {
3232
fileChanger *allocation.UploadFileChanger
3333
}
3434

35+
func (cmd *UploadFileCommand) GetExistingFileRef() *reference.Ref {
36+
return nil
37+
}
38+
3539
// IsValidated validate request.
3640
func (cmd *UploadFileCommand) IsValidated(ctx context.Context, req *http.Request, allocationObj *allocation.Allocation, clientID string) error {
3741
if allocationObj.OwnerID != clientID && allocationObj.RepairerID != clientID {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,17 @@ func (b *blobberGRPCService) Collaborator(ctx context.Context, req *blobbergrpc.
201201
"collab_id": {req.CollabId},
202202
}
203203

204-
resp, err := CollaboratorHandler(ctx, r)
204+
var resp interface{}
205+
206+
switch req.Method {
207+
case http.MethodPost:
208+
resp, err = AddCollaboratorHandler(ctx, r)
209+
case http.MethodGet:
210+
resp, err = GetCollaboratorHandler(ctx, r)
211+
case http.MethodDelete:
212+
resp, err = RemoveCollaboratorHandler(ctx, r)
213+
}
214+
205215
if err != nil {
206216
return nil, err
207217
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ func SetupHandlers(r *mux.Router) {
4747

4848
r.HandleFunc("/v1/connection/commit/{allocation}", common.ToStatusCode(WithStatusConnection(CommitHandler)))
4949
r.HandleFunc("/v1/file/commitmetatxn/{allocation}", common.ToJSONResponse(WithConnection(CommitMetaTxnHandler)))
50-
r.HandleFunc("/v1/file/collaborator/{allocation}", common.ToJSONResponse(WithConnection(CollaboratorHandler)))
50+
5151
r.HandleFunc("/v1/file/calculatehash/{allocation}", common.ToJSONResponse(WithConnection(CalculateHashHandler)))
5252

53+
// collaborator
54+
r.HandleFunc("/v1/file/collaborator/{allocation}", common.ToJSONResponse(WithConnection(AddCollaboratorHandler))).Methods(http.MethodOptions, http.MethodPost)
55+
r.HandleFunc("/v1/file/collaborator/{allocation}", common.ToJSONResponse(WithConnection(GetCollaboratorHandler))).Methods(http.MethodOptions, http.MethodGet)
56+
r.HandleFunc("/v1/file/collaborator/{allocation}", common.ToJSONResponse(WithConnection(RemoveCollaboratorHandler))).Methods(http.MethodOptions, http.MethodDelete)
57+
5358
//object info related apis
5459
r.HandleFunc("/allocation", common.ToJSONResponse(WithConnection(AllocationHandler)))
5560
r.HandleFunc("/v1/file/meta/{allocation}", common.ToJSONResponse(WithReadOnlyConnection(FileMetaHandler)))
@@ -290,7 +295,7 @@ func UpdateAttributesHandler(ctx context.Context, r *http.Request) (interface{},
290295
return response, nil
291296
}
292297

293-
func writeResponse (w http.ResponseWriter, resp []byte) {
298+
func writeResponse(w http.ResponseWriter, resp []byte) {
294299
_, err := w.Write(resp)
295300

296301
if err != nil {
@@ -392,8 +397,8 @@ func RevokeShare(ctx context.Context, r *http.Request) (interface{}, error) {
392397
return nil, common.NewError("invalid_signature", "Invalid signature")
393398
}
394399

395-
path := r.FormValue("path")
396-
refereeClientID := r.FormValue("refereeClientID")
400+
path, _ := GetField(r, "path")
401+
refereeClientID, _ := GetField(r, "refereeClientID")
397402
filePathHash := fileref.GetReferenceLookup(allocationID, path)
398403
//_, err = reference.GetReferenceByLookupHashForAddCollaborator(ctx, allocationID, filePathHash)
399404
_, err = reference.GetLimitedRefFieldsByLookupHash(ctx, allocationID, filePathHash, []string{"id", "type"})

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ func CalculateHashHandler(ctx context.Context, r *http.Request) (interface{}, er
5858
return response, nil
5959
}
6060

61-
func CollaboratorHandler(ctx context.Context, r *http.Request) (interface{}, error) {
61+
func AddCollaboratorHandler(ctx context.Context, r *http.Request) (interface{}, error) {
62+
return storageHandler.AddCollaborator(setupHandlerContext(ctx, r), r)
63+
}
6264

63-
ctx = setupHandlerContext(ctx, r)
65+
func GetCollaboratorHandler(ctx context.Context, r *http.Request) (interface{}, error) {
66+
return storageHandler.GetCollaborator(setupHandlerContext(ctx, r), r)
67+
}
6468

65-
response, err := storageHandler.AddCollaborator(ctx, r)
66-
if err != nil {
67-
return nil, err
68-
}
69-
return response, nil
69+
func RemoveCollaboratorHandler(ctx context.Context, r *http.Request) (interface{}, error) {
70+
return storageHandler.RemoveCollaborator(setupHandlerContext(ctx, r), r)
7071
}
7172

7273
func HomepageHandler(w http.ResponseWriter, r *http.Request) {
@@ -95,21 +96,21 @@ func HomepageHandler(w http.ResponseWriter, r *http.Request) {
9596
}
9697

9798
type BlobberInfo struct {
98-
ChainId string `json:"chain_id"`
99-
BlobberId string `json:"blobber_id"`
100-
BlobberPublicKey string `json:"public_key"`
101-
BuildTag string `json:"build_tag"`
102-
Stats interface{} `json:"stats"`
99+
ChainId string `json:"chain_id"`
100+
BlobberId string `json:"blobber_id"`
101+
BlobberPublicKey string `json:"public_key"`
102+
BuildTag string `json:"build_tag"`
103+
Stats interface{} `json:"stats"`
103104
}
104105

105106
func GetBlobberInfoJson() BlobberInfo {
106107
mc := chain.GetServerChain()
107108

108109
blobberInfo := BlobberInfo{
109-
ChainId: mc.ID,
110-
BlobberId: node.Self.ID,
110+
ChainId: mc.ID,
111+
BlobberId: node.Self.ID,
111112
BlobberPublicKey: node.Self.PublicKey,
112-
BuildTag: build.BuildTag,
113+
BuildTag: build.BuildTag,
113114
}
114115

115116
return blobberInfo

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"mime/multipart"
1010
"net/http"
1111
"net/http/httptest"
12+
"net/url"
1213
"os"
1314
"regexp"
1415
"testing"
@@ -375,23 +376,21 @@ func TestHandlers_Share(t *testing.T) {
375376
args: args{
376377
w: httptest.NewRecorder(),
377378
r: func() *http.Request {
378-
url, err := router.Get(revokeShare).URL("allocation", alloc.Tx)
379+
u, err := router.Get(revokeShare).URL("allocation", alloc.Tx)
379380
if err != nil {
380381
t.Fatal()
381382
}
382383

383-
body := bytes.NewBuffer(nil)
384-
formWriter := multipart.NewWriter(body)
384+
query := &url.Values{}
385385
shareClientID := "da4b54d934890aa415bb043ce1126f2e30a96faf63a4c65c25bbddcb32824d77"
386386
remotePath := "/file.txt"
387387

388-
require.NoError(t, formWriter.WriteField("refereeClientID", shareClientID))
389-
require.NoError(t, formWriter.WriteField("path", remotePath))
390-
if err := formWriter.Close(); err != nil {
391-
t.Fatal(err)
392-
}
393-
r, err := http.NewRequest(http.MethodDelete, url.String(), body)
394-
r.Header.Add("Content-Type", formWriter.FormDataContentType())
388+
query.Add("refereeClientID", shareClientID)
389+
query.Add("path", remotePath)
390+
391+
u.RawQuery = query.Encode()
392+
r, err := http.NewRequest(http.MethodDelete, u.String(), nil)
393+
395394
if err != nil {
396395
t.Fatal(err)
397396
}
@@ -402,7 +401,6 @@ func TestHandlers_Share(t *testing.T) {
402401
t.Fatal(err)
403402
}
404403

405-
r.Header.Set("Content-Type", formWriter.FormDataContentType())
406404
r.Header.Set(common.ClientSignatureHeader, sign)
407405
r.Header.Set(common.ClientHeader, alloc.OwnerID)
408406

@@ -454,34 +452,29 @@ func TestHandlers_Share(t *testing.T) {
454452
args: args{
455453
w: httptest.NewRecorder(),
456454
r: func() *http.Request {
457-
url, err := router.Get(revokeShare).URL("allocation", alloc.Tx)
455+
u, err := router.Get(revokeShare).URL("allocation", alloc.Tx)
458456
if err != nil {
459457
t.Fatal()
460458
}
461459

462-
body := bytes.NewBuffer(nil)
463-
formWriter := multipart.NewWriter(body)
460+
query := &url.Values{}
464461
shareClientID := "da4b54d934890aa415bb043ce1126f2e30a96faf63a4c65c25bbddcb32824d77"
465462
remotePath := "/file.txt"
466463

467-
require.NoError(t, formWriter.WriteField("refereeClientID", shareClientID))
468-
require.NoError(t, formWriter.WriteField("path", remotePath))
469-
if err := formWriter.Close(); err != nil {
470-
t.Fatal(err)
471-
}
472-
r, err := http.NewRequest(http.MethodDelete, url.String(), body)
473-
r.Header.Add("Content-Type", formWriter.FormDataContentType())
464+
query.Add("refereeClientID", shareClientID)
465+
query.Add("path", remotePath)
466+
467+
u.RawQuery = query.Encode()
468+
r, err := http.NewRequest(http.MethodDelete, u.String(), nil)
474469
if err != nil {
475470
t.Fatal(err)
476471
}
477-
478472
hash := encryption.Hash(alloc.Tx)
479473
sign, err := sch.Sign(hash)
480474
if err != nil {
481475
t.Fatal(err)
482476
}
483477

484-
r.Header.Set("Content-Type", formWriter.FormDataContentType())
485478
r.Header.Set(common.ClientSignatureHeader, sign)
486479
r.Header.Set(common.ClientHeader, alloc.OwnerID)
487480

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func setupHandlers() (*mux.Router, map[string]string) {
118118
collName := "Collaborator"
119119
router.HandleFunc(collPath, common.UserRateLimit(
120120
common.ToJSONResponse(
121-
WithReadOnlyConnection(CollaboratorHandler),
121+
WithReadOnlyConnection(GetCollaboratorHandler),
122122
),
123123
),
124124
).Name(collName)
@@ -1106,6 +1106,7 @@ func TestHandlers_Requiring_Signature(t *testing.T) {
11061106
}
11071107
tests := append(positiveTests, negativeTests...)
11081108
tests = append(tests, uploadNegativeTests...)
1109+
11091110
for _, test := range tests {
11101111
t.Run(test.name, func(t *testing.T) {
11111112
mock := datastore.MockTheStore(t)

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,20 @@ func TryParseForm(r *http.Request) {
2828
}
2929
}
3030
}
31+
32+
// GetField get field from form or query
33+
func GetField(r *http.Request, key string) (string, bool) {
34+
TryParseForm(r)
35+
36+
v, ok := r.Form[key]
37+
if ok {
38+
return v[0], true
39+
}
40+
41+
v, ok = r.URL.Query()[key]
42+
if ok {
43+
return v[0], true
44+
}
45+
46+
return "", false
47+
}

0 commit comments

Comments
 (0)