Skip to content

Commit 4cc3525

Browse files
committed
Fix: Provide clear error when filename is missing for data upload (fixes #220)
Signed-off-by: Deekshit S <deekshit1403@gmail.com>
1 parent 7342300 commit 4cc3525

File tree

1 file changed

+90
-87
lines changed

1 file changed

+90
-87
lines changed
Lines changed: 90 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,90 @@
1-
// Copyright © 2022 Kaleido, Inc.
2-
//
3-
// SPDX-License-Identifier: Apache-2.0
4-
//
5-
// Licensed under the Apache License, Version 2.0 (the "License");
6-
// you may not use this file except in compliance with the License.
7-
// You may obtain a copy of the License at
8-
//
9-
// http://www.apache.org/licenses/LICENSE-2.0
10-
//
11-
// Unless required by applicable law or agreed to in writing, software
12-
// distributed under the License is distributed on an "AS IS" BASIS,
13-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
// See the License for the specific language governing permissions and
15-
// limitations under the License.
16-
17-
package apiserver
18-
19-
import (
20-
"encoding/json"
21-
"fmt"
22-
"net/http"
23-
"strings"
24-
25-
"github.com/hyperledger/firefly-common/pkg/ffapi"
26-
"github.com/hyperledger/firefly-common/pkg/fftypes"
27-
"github.com/hyperledger/firefly-common/pkg/i18n"
28-
"github.com/hyperledger/firefly/internal/coremsgs"
29-
"github.com/hyperledger/firefly/internal/orchestrator"
30-
"github.com/hyperledger/firefly/pkg/core"
31-
)
32-
33-
var postData = &ffapi.Route{
34-
Name: "postData",
35-
Path: "data",
36-
Method: http.MethodPost,
37-
PathParams: nil,
38-
QueryParams: nil,
39-
FormParams: []*ffapi.FormParam{
40-
{Name: "autometa", Description: coremsgs.APIParamsAutometa},
41-
{Name: "metadata", Description: coremsgs.APIParamsMetadata},
42-
{Name: "validator", Description: coremsgs.APIParamsValidator},
43-
{Name: "datatype.name", Description: coremsgs.APIParamsDatatypeName},
44-
{Name: "datatype.version", Description: coremsgs.APIParamsDatatypeVersion},
45-
},
46-
Description: coremsgs.APIEndpointsPostData,
47-
JSONInputValue: func() interface{} { return &core.DataRefOrValue{} },
48-
JSONOutputValue: func() interface{} { return &core.Data{} },
49-
JSONOutputCodes: []int{http.StatusCreated},
50-
Extensions: &coreExtensions{
51-
EnabledIf: func(or orchestrator.Orchestrator) bool {
52-
return or.Data() != nil
53-
},
54-
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
55-
output, err = cr.or.Data().UploadJSON(cr.ctx, r.Input.(*core.DataRefOrValue))
56-
return output, err
57-
},
58-
CoreFormUploadHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
59-
if !cr.or.Data().BlobsEnabled() {
60-
return nil, i18n.NewError(r.Req.Context(), coremsgs.MsgActionNotSupported)
61-
}
62-
63-
data := &core.DataRefOrValue{}
64-
validator := r.FP["validator"]
65-
if len(validator) > 0 {
66-
data.Validator = core.ValidatorType(validator)
67-
}
68-
if r.FP["datatype.name"] != "" {
69-
data.Datatype = &core.DatatypeRef{
70-
Name: r.FP["datatype.name"],
71-
Version: r.FP["datatype.version"],
72-
}
73-
}
74-
metadata := r.FP["metadata"]
75-
if len(metadata) > 0 {
76-
// The metadata might be JSON, or just a simple string. Try to unmarshal and see
77-
var marshalCheck interface{}
78-
if err := json.Unmarshal([]byte(metadata), &marshalCheck); err != nil {
79-
metadata = fmt.Sprintf(`"%s"`, metadata)
80-
}
81-
data.Value = fftypes.JSONAnyPtr(metadata)
82-
}
83-
output, err = cr.or.Data().UploadBlob(cr.ctx, data, r.Part, strings.EqualFold(r.FP["autometa"], "true"))
84-
return output, err
85-
},
86-
},
87-
}
1+
// Copyright © 2022 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package apiserver
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"net/http"
23+
"strings"
24+
25+
"github.com/hyperledger/firefly-common/pkg/ffapi"
26+
"github.com/hyperledger/firefly-common/pkg/fftypes"
27+
"github.com/hyperledger/firefly-common/pkg/i18n"
28+
"github.com/hyperledger/firefly/internal/coremsgs"
29+
"github.com/hyperledger/firefly/internal/orchestrator"
30+
"github.com/hyperledger/firefly/pkg/core"
31+
)
32+
33+
var postData = &ffapi.Route{
34+
Name: "postData",
35+
Path: "data",
36+
Method: http.MethodPost,
37+
PathParams: nil,
38+
QueryParams: nil,
39+
FormParams: []*ffapi.FormParam{
40+
{Name: "autometa", Description: coremsgs.APIParamsAutometa},
41+
{Name: "metadata", Description: coremsgs.APIParamsMetadata},
42+
{Name: "validator", Description: coremsgs.APIParamsValidator},
43+
{Name: "datatype.name", Description: coremsgs.APIParamsDatatypeName},
44+
{Name: "datatype.version", Description: coremsgs.APIParamsDatatypeVersion},
45+
},
46+
Description: coremsgs.APIEndpointsPostData,
47+
JSONInputValue: func() interface{} { return &core.DataRefOrValue{} },
48+
JSONOutputValue: func() interface{} { return &core.Data{} },
49+
JSONOutputCodes: []int{http.StatusCreated},
50+
Extensions: &coreExtensions{
51+
EnabledIf: func(or orchestrator.Orchestrator) bool {
52+
return or.Data() != nil
53+
},
54+
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
55+
output, err = cr.or.Data().UploadJSON(cr.ctx, r.Input.(*core.DataRefOrValue))
56+
return output, err
57+
},
58+
CoreFormUploadHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
59+
if !cr.or.Data().BlobsEnabled() {
60+
return nil, i18n.NewError(r.Req.Context(), coremsgs.MsgActionNotSupported)
61+
}
62+
// Check for file upload
63+
if r.Part == nil || r.Part.FileName() == "" {
64+
return nil, i18n.NewError(r.Req.Context(), coremsgs.MsgMissingFileUpload)
65+
}
66+
data := &core.DataRefOrValue{}
67+
validator := r.FP["validator"]
68+
if len(validator) > 0 {
69+
data.Validator = core.ValidatorType(validator)
70+
}
71+
if r.FP["datatype.name"] != "" {
72+
data.Datatype = &core.DatatypeRef{
73+
Name: r.FP["datatype.name"],
74+
Version: r.FP["datatype.version"],
75+
}
76+
}
77+
metadata := r.FP["metadata"]
78+
if len(metadata) > 0 {
79+
// The metadata might be JSON, or just a simple string. Try to unmarshal and see
80+
var marshalCheck interface{}
81+
if err := json.Unmarshal([]byte(metadata), &marshalCheck); err != nil {
82+
metadata = fmt.Sprintf(`"%s"`, metadata)
83+
}
84+
data.Value = fftypes.JSONAnyPtr(metadata)
85+
}
86+
output, err = cr.or.Data().UploadBlob(cr.ctx, data, r.Part, strings.EqualFold(r.FP["autometa"], "true"))
87+
return output, err
88+
},
89+
},
90+
}

0 commit comments

Comments
 (0)