@@ -19,6 +19,7 @@ package handlers
19
19
import (
20
20
"encoding/json"
21
21
"io"
22
+ "mime/multipart"
22
23
"net/http"
23
24
"path/filepath"
24
25
"strings"
@@ -30,53 +31,82 @@ import (
30
31
)
31
32
32
33
// HandleCreateProjectInput is the handler for creating a project input
33
- func HandleCreateProjectInput (w http.ResponseWriter , r * http.Request ) {
34
+ // isCommon: if true the input will be available for all the projects in the workspace
35
+ func HandleCreateProjectInput (w http.ResponseWriter , r * http.Request , isCommon bool ) {
34
36
logrus := GetLogger (r )
35
37
logrus .Trace ("HandleCreateProjectInput start" )
36
38
defer logrus .Trace ("HandleCreateProjectInput end" )
37
39
workspaceId := mux .Vars (r )[WORKSPACE_ID_ROUTE_VAR ]
38
- projectId := mux .Vars (r )[PROJECT_ID_ROUTE_VAR ]
39
- if ! common .IsValidId (workspaceId ) || ! common .IsValidId (projectId ) {
40
- logrus .Errorf ("invalid id. Actual: %s %s" , workspaceId , projectId )
41
- sendErrorJSON (w , "invalid id" , http .StatusBadRequest )
40
+ if ! common .IsValidId (workspaceId ) {
41
+ logrus .Errorf ("invalid workspace id. Actual: %s" , workspaceId )
42
+ sendErrorJSON (w , "invalid workspace id" , http .StatusBadRequest )
42
43
return
43
44
}
45
+ projectId := ""
46
+ if ! isCommon {
47
+ projectId = mux .Vars (r )[PROJECT_ID_ROUTE_VAR ]
48
+ if ! common .IsValidId (projectId ) {
49
+ logrus .Errorf ("invalid project id. Actual: %s" , projectId )
50
+ sendErrorJSON (w , "invalid project id" , http .StatusBadRequest )
51
+ return
52
+ }
53
+ }
44
54
r .Body = http .MaxBytesReader (w , r .Body , common .Config .MaxUploadSize )
45
55
if err := r .ParseMultipartForm (common .Config .MaxUploadSize ); err != nil {
46
56
logrus .Errorf ("failed to parse the request body as multipart/form-data. Error: %q" , err )
47
57
sendErrorJSON (w , "failed to parse the request body as multipart/form-data" , http .StatusBadRequest )
48
58
return
49
59
}
50
- file , handler , err := r . FormFile ( "file" )
60
+ timestamp , _ , err := common . GetTimestamp ( )
51
61
if err != nil {
52
- logrus .Errorf ("failed to get the file from the request body . Error: %q" , err )
53
- sendErrorJSON ( w , "failed to get the file from the request body" , http .StatusBadRequest )
62
+ logrus .Errorf ("failed to get the timestamp . Error: %q" , err )
63
+ w . WriteHeader ( http .StatusInternalServerError )
54
64
return
55
65
}
56
- defer file .Close ()
57
66
projType , err := types .ParseProjectInputType (r .FormValue ("type" ))
58
67
if err != nil {
59
68
logrus .Errorf ("failed to parse the project input type. Error: %q" , err )
60
69
sendErrorJSON (w , "the input type is invalid" , http .StatusBadRequest )
61
70
return
62
71
}
63
- timestamp , _ , err := common .GetTimestamp ()
64
- if err != nil {
65
- logrus .Errorf ("failed to get the timestamp. Error: %q" , err )
66
- w .WriteHeader (http .StatusInternalServerError )
67
- return
68
- }
69
- normName := filepath .Base (filepath .Clean (handler .Filename ))
70
- normName = strings .TrimSuffix (normName , filepath .Ext (normName ))
71
- normName , err = common .NormalizeName (normName )
72
- if err != nil {
73
- logrus .Errorf ("failed to normalize the filename '%s'. Error: %q" , handler .Filename , err )
74
- sendErrorJSON (w , "failed to normalize the filename. Please use a filename that has only alphanumeric and hyphen characters." , http .StatusBadRequest )
75
- return
72
+ var file io.ReadCloser
73
+ filename := ""
74
+ normName := ""
75
+ projInputId := uuid .NewString ()
76
+ if projType == types .ProjectInputReference {
77
+ if isCommon {
78
+ logrus .Errorf ("cannot upload reference type input for workspaces" )
79
+ sendErrorJSON (w , "cannot upload reference type input for workspaces" , http .StatusBadRequest )
80
+ return
81
+ }
82
+ projInputId = r .FormValue ("id" )
83
+ if ! common .IsValidId (projInputId ) {
84
+ logrus .Errorf ("the reference input id is invalid. Actual: %s" , projInputId )
85
+ sendErrorJSON (w , "the reference input id is invalid" , http .StatusBadRequest )
86
+ return
87
+ }
88
+ } else {
89
+ var fileHeader * multipart.FileHeader
90
+ file , fileHeader , err = r .FormFile ("file" )
91
+ if err != nil {
92
+ logrus .Errorf ("failed to get the file from the request body. Error: %q" , err )
93
+ sendErrorJSON (w , "failed to get the file from the request body" , http .StatusBadRequest )
94
+ return
95
+ }
96
+ filename = fileHeader .Filename
97
+ defer file .Close ()
98
+ normName = filepath .Base (filepath .Clean (filename ))
99
+ normName = strings .TrimSuffix (normName , filepath .Ext (normName ))
100
+ normName , err = common .NormalizeName (normName )
101
+ if err != nil {
102
+ logrus .Errorf ("failed to normalize the filename '%s'. Error: %q" , filename , err )
103
+ sendErrorJSON (w , "failed to normalize the filename. Please use a filename that has only alphanumeric and hyphen characters." , http .StatusBadRequest )
104
+ return
105
+ }
76
106
}
77
- projInput := types.ProjectInput {Metadata : types.Metadata {Id : uuid . NewString () , Name : handler . Filename , Description : r .FormValue ("description" ), Timestamp : timestamp }, Type : projType , NormalizedName : normName }
107
+ projInput := types.ProjectInput {Metadata : types.Metadata {Id : projInputId , Name : filename , Description : r .FormValue ("description" ), Timestamp : timestamp }, Type : projType , NormalizedName : normName }
78
108
logrus .Debug ("trying to create a new input for the project" , projectId , " with the details:" , projInput )
79
- if err := m2kFS .CreateProjectInput (workspaceId , projectId , projInput , file ); err != nil {
109
+ if err := m2kFS .CreateProjectInput (workspaceId , projectId , projInput , file , isCommon ); err != nil {
80
110
logrus .Errorf ("failed to create the project input. Error: %q" , err )
81
111
if _ , ok := err .(types.ErrorDoesNotExist ); ok {
82
112
w .WriteHeader (http .StatusNotFound )
@@ -99,19 +129,26 @@ func HandleCreateProjectInput(w http.ResponseWriter, r *http.Request) {
99
129
}
100
130
101
131
// HandleReadProjectInput is the handler for reading a project input
102
- func HandleReadProjectInput (w http.ResponseWriter , r * http.Request ) {
132
+ func HandleReadProjectInput (w http.ResponseWriter , r * http.Request , isCommon bool ) {
103
133
logrus := GetLogger (r )
104
134
logrus .Trace ("HandleReadProjectInput start" )
105
135
defer logrus .Trace ("HandleReadProjectInput end" )
106
136
workspaceId := mux .Vars (r )[WORKSPACE_ID_ROUTE_VAR ]
107
137
projectId := mux .Vars (r )[PROJECT_ID_ROUTE_VAR ]
108
138
projInputId := mux .Vars (r )[PROJECT_INPUT_ID_ROUTE_VAR ]
109
- if ! common .IsValidId (workspaceId ) || ! common .IsValidId (projectId ) || ! common . IsValidId ( projInputId ) {
110
- logrus .Errorf ("invalid id. Actual: %s %s %s " , workspaceId , projectId , projInputId )
111
- sendErrorJSON (w , "invalid id" , http .StatusBadRequest )
139
+ if ! common .IsValidId (workspaceId ) || ! common .IsValidId (projInputId ) {
140
+ logrus .Errorf ("invalid workspace and/or project input id. Actual: %s %s" , workspaceId , projInputId )
141
+ sendErrorJSON (w , "invalid workspace and/or project input id" , http .StatusBadRequest )
112
142
return
113
143
}
114
- projInput , file , err := m2kFS .ReadProjectInput (workspaceId , projectId , projInputId )
144
+ if ! isCommon {
145
+ if ! common .IsValidId (projectId ) {
146
+ logrus .Errorf ("invalid project id. Actual: %s" , projectId )
147
+ sendErrorJSON (w , "invalid project id" , http .StatusBadRequest )
148
+ return
149
+ }
150
+ }
151
+ projInput , file , err := m2kFS .ReadProjectInput (workspaceId , projectId , projInputId , isCommon )
115
152
if err != nil {
116
153
logrus .Errorf ("failed to get the input with id %s for the project %s . Error: %q" , projInputId , projectId , err )
117
154
if _ , ok := err .(types.ErrorDoesNotExist ); ok {
@@ -132,19 +169,26 @@ func HandleReadProjectInput(w http.ResponseWriter, r *http.Request) {
132
169
}
133
170
134
171
// HandleDeleteProjectInput is the handler for deleting a project input
135
- func HandleDeleteProjectInput (w http.ResponseWriter , r * http.Request ) {
172
+ func HandleDeleteProjectInput (w http.ResponseWriter , r * http.Request , isCommon bool ) {
136
173
logrus := GetLogger (r )
137
174
logrus .Trace ("HandleDeleteProjectInput start" )
138
175
defer logrus .Trace ("HandleDeleteProjectInput end" )
139
176
workspaceId := mux .Vars (r )[WORKSPACE_ID_ROUTE_VAR ]
140
177
projectId := mux .Vars (r )[PROJECT_ID_ROUTE_VAR ]
141
178
projInputId := mux .Vars (r )[PROJECT_INPUT_ID_ROUTE_VAR ]
142
- if ! common .IsValidId (workspaceId ) || ! common .IsValidId (projectId ) || ! common . IsValidId ( projInputId ) {
143
- logrus .Errorf ("invalid id. Actual: %s %s %s " , workspaceId , projectId , projInputId )
144
- sendErrorJSON (w , "invalid id" , http .StatusBadRequest )
179
+ if ! common .IsValidId (workspaceId ) || ! common .IsValidId (projInputId ) {
180
+ logrus .Errorf ("invalid workspace and/or project input id. Actual: %s %s" , workspaceId , projInputId )
181
+ sendErrorJSON (w , "invalid workspace and/or project input id" , http .StatusBadRequest )
145
182
return
146
183
}
147
- if err := m2kFS .DeleteProjectInput (workspaceId , projectId , projInputId ); err != nil {
184
+ if ! isCommon {
185
+ if ! common .IsValidId (projectId ) {
186
+ logrus .Errorf ("invalid project id. Actual: %s" , projectId )
187
+ sendErrorJSON (w , "invalid project id" , http .StatusBadRequest )
188
+ return
189
+ }
190
+ }
191
+ if err := m2kFS .DeleteProjectInput (workspaceId , projectId , projInputId , isCommon ); err != nil {
148
192
logrus .Errorf ("failed to delete the input %s of the project %s . Error: %q" , projInputId , projectId , err )
149
193
if _ , ok := err .(types.ErrorDoesNotExist ); ok {
150
194
w .WriteHeader (http .StatusNotFound )
0 commit comments