-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrestapiTasksetting.go
258 lines (252 loc) · 6.47 KB
/
restapiTasksetting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"bytes"
"context"
"encoding/json"
"html/template"
"net/http"
"gopkg.in/mgo.v2"
)
// handleAPITasksetting 함수는 Tasksetting 리소스의 restAPI 이다.
func handleAPITasksetting(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Post Only", http.StatusMethodNotAllowed)
return
}
type Recipe struct {
Project string `json:"project"`
Name string `json:"name"`
Task string `json:"task"`
Type string `json:"type"`
Assettype string `json:"assettype"`
OS string `json:"os"`
Seq string `json:"seq"`
Cut string `json:"cut"`
Path string `json:"path"`
UserID string `json:"userid"`
Error string `json:"error"`
}
rcp := Recipe{}
session, err := mgo.Dial(*flagDBIP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer session.Close()
rcp.UserID, _, err = TokenHandler(r, session)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
r.ParseForm()
for key, values := range r.PostForm {
switch key {
case "userid":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if rcp.UserID == "unknown" && v != "" {
rcp.UserID = v
}
case "project":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.Project = v
case "name":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.Name = v
case "task":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.Task = v
case "seq":
if len(values) == 1 {
rcp.Seq = values[0]
}
case "cut":
if len(values) == 1 {
rcp.Cut = values[0]
}
case "os":
if len(values) == 1 {
rcp.OS = values[0]
}
case "assettype":
if len(values) == 1 {
rcp.Assettype = values[0]
}
case "type":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.Type = v
}
}
typ := "shot"
if rcp.Type == "asset" {
typ = "asset"
}
t, err := getTaskSetting(session, rcp.Task+typ)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
switch rcp.OS {
case "macos":
rcp.Path = t.MacOSPath
case "linux":
rcp.Path = t.LinuxPath
case "windows":
rcp.Path = t.WindowPath
default:
rcp.Path = t.WFSPath // 기본적으로 WFS 경로를 사용한다.
}
// OS 경로를 불러와서 경로를 대입한다.
tmpl, err := template.New("test").Parse(rcp.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Path = tpl.String()
// json 으로 결과 전송
data, _ := json.Marshal(rcp)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// handleAPIShotTasksetting 함수는 Shot Task 항목을 반환하는 restAPI 이다.
func handleAPIShotTasksetting(w http.ResponseWriter, r *http.Request) {
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
_, _, err = TokenHandlerV2(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
type Recipe struct {
Tasksettings []Tasksetting `json:"tasksettings"`
}
rcp := Recipe{}
rcp.Tasksettings, err = getShotTaskSettingV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// handleAPIAssetTasksetting 함수는 Shot Task 항목을 반환하는 restAPI 이다.
func handleAPIAssetTasksetting(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Get Only", http.StatusMethodNotAllowed)
return
}
session, err := mgo.Dial(*flagDBIP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
type Recipe struct {
Tasksettings []Tasksetting `json:"tasksettings"`
}
rcp := Recipe{}
rcp.Tasksettings, err = getAssetTaskSetting(session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// handleAPICategoryTasksettings 함수는 Category 문자를 입력받아 해당 Category Task항목을 반환하는 restAPI 이다.
func handleAPICategoryTasksettings(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Post Only", http.StatusMethodNotAllowed)
return
}
type Recipe struct {
Category string `json:"category"`
Tasksettings []Tasksetting `json:"tasksettings"`
}
rcp := Recipe{}
session, err := mgo.Dial(*flagDBIP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
r.ParseForm()
for key, values := range r.PostForm {
switch key {
case "category":
if len(values) != 1 {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.Category = values[0]
}
}
rcp.Tasksettings, err = getCategoryTaskSettings(session, rcp.Category)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}