-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
restapiItem_mgo.go
executable file
·8533 lines (8377 loc) · 237 KB
/
restapiItem_mgo.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/digital-idea/dilog"
"github.com/digital-idea/dipath"
"gopkg.in/mgo.v2"
)
// handleAPI2Item 함수는 아이템 자료구조를 불러온다.
func handleAPI2GetItem(w http.ResponseWriter, r *http.Request) {
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
}
q := r.URL.Query()
project := q.Get("project")
if project == "" {
http.Error(w, "project를 설정해주세요", http.StatusBadRequest)
return
}
id := q.Get("id")
name := q.Get("name")
if id == "" && name == "" {
http.Error(w, "id(SS_0010_org) 또는 name(SS_0010)을 설정해주세요", http.StatusBadRequest)
return
}
typ := q.Get("type")
if typ == "" || id == "" {
if name == "" && regexpID.MatchString(id) && typ != "" {
name = strings.Split(id, "_")[0] + "_" + strings.Split(id, "_")[1]
typ, err = Type(session, project, name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else if name != "" && typ == "" {
typ, err = Type(session, project, name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
if id == "" {
id = name + "_" + typ
}
item, err := getItem(session, project, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(item)
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)
}
// handleAPIItemV2 함수는 아이템 자료구조를 불러온다.
func handleAPITimeinfo(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Post 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
}
r.ParseForm() // 받은 문자를 파싱합니다. 파싱되면 map이 됩니다.
var project string
var id string
for key, values := range r.PostForm {
switch key {
case "project":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
project = v
case "id":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id = v
}
}
item, err := getItem(session, project, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
type recipe struct {
ScanIn int `json:"scanin"`
ScanOut int `json:"scanout"`
ScanFrame int `json:"scanframe"`
ScanTimecodeIn string `json:"scantimecodein"`
ScanTimecodeOut string `json:"scantimecodeout"`
PlateIn int `json:"platein"`
PlateOut int `json:"plateout"`
HandleIn int `json:"handlein"`
HandleOut int `json:"handleout"`
JustIn int `json:"justin"`
JustOut int `json:"justout"`
JustTimecodeIn string `json:"justtimecodein"`
JustTimecodeOut string `json:"justtimecodeout"`
}
rcp := recipe{}
rcp.ScanIn = item.ScanIn
rcp.ScanOut = item.ScanOut
rcp.ScanFrame = item.ScanFrame
rcp.ScanTimecodeIn = item.ScanTimecodeIn
rcp.ScanTimecodeOut = item.ScanTimecodeOut
rcp.PlateIn = item.PlateIn
rcp.PlateOut = item.PlateOut
rcp.HandleIn = item.HandleIn
rcp.HandleOut = item.HandleOut
rcp.JustIn = item.JustIn
rcp.JustOut = item.JustOut
rcp.JustTimecodeIn = item.JustTimecodeIn
rcp.JustTimecodeOut = item.JustTimecodeOut
// 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)
}
// handleAPIRmItem 함수는 아이템을 삭제한다.
func handleAPIRmItem(w http.ResponseWriter, r *http.Request) {
session, err := mgo.Dial(*flagDBIP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer session.Close()
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
type Recipe struct {
Project string `json:"project"`
Name string `json:"name"`
Type string `json:"typ"`
UserID string `json:"userid"`
}
rcp := Recipe{}
userID, accessLevel, err := TokenHandler(r, session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if PmAccessLevel > accessLevel {
http.Error(w, "need permission", http.StatusUnauthorized)
return
}
rcp.UserID = userID
r.ParseForm() // 받은 문자를 파싱합니다. 파싱되면 map이 됩니다.
project := r.FormValue("project")
if project == "" {
http.Error(w, "need project", http.StatusBadRequest)
return
}
rcp.Project = project
name := r.FormValue("name")
if name == "" {
http.Error(w, "need name", http.StatusBadRequest)
return
}
rcp.Name = name
typ := r.FormValue("type")
if project == "" {
http.Error(w, "need project", http.StatusBadRequest)
return
}
rcp.Type = typ
err = rmItem(session, project, name, typ)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// log
err = dilog.Add(*flagDBIP, host, fmt.Sprintf("Remove Item: Project: %s, ID: %s_%s", project, name, typ), project, name, "csi3", rcp.UserID, 180)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// slack log
err = slacklog(session, project, fmt.Sprintf("Remove Item: \nProject: %s, ID: %s_%s, Author: %s", project, name, typ, rcp.UserID))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
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)
}
// handleAPIRmItemID 함수는 아이템을 삭제한다.
func handleAPIRmItemID(w http.ResponseWriter, r *http.Request) {
session, err := mgo.Dial(*flagDBIP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer session.Close()
userID, level, err := TokenHandler(r, session)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if PmAccessLevel > level {
http.Error(w, "need permission", http.StatusUnauthorized)
return
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
type Recipe struct {
Project string `json:"project"`
ID string `json:"id"`
UserID string `json:"userid"`
}
rcp := Recipe{}
rcp.UserID = userID
r.ParseForm() // 받은 문자를 파싱합니다. 파싱되면 map이 됩니다.
project := r.FormValue("project")
if project == "" {
http.Error(w, "need project", http.StatusBadRequest)
return
}
rcp.Project = project
id := r.FormValue("id")
if id == "" {
http.Error(w, "need id", http.StatusBadRequest)
return
}
rcp.ID = id
err = rmItemID(session, project, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// log
err = dilog.Add(*flagDBIP, host, fmt.Sprintf("Remove Item: Project: %s, ID: %s", project, rcp.ID), rcp.Project, rcp.ID, "csi3", rcp.UserID, 180)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// slack log
err = slacklog(session, project, fmt.Sprintf("Remove Item: \nProject: %s, ID: %s, Author: %s", rcp.Project, rcp.ID, rcp.UserID))
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)
}
// handleAPISearchname 함수는 입력 문자열을 포함하는 샷,에셋 정보를 검색한다.
func handleAPISearchname(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Get Only", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
session, err := mgo.Dial(*flagDBIP)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
q := r.URL.Query()
project := q.Get("project")
name := q.Get("name")
items, err := SearchName(session, project, name)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
type recipe struct {
Data []Item `json:"data"`
}
rcp := recipe{}
rcp.Data = items
err = json.NewEncoder(w).Encode(rcp)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
}
// handleAPI2Items 함수는 아이템을 검색한다. // legacy
func handleAPI2Items(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Get Only", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
session, err := mgo.Dial(*flagDBIP)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
type recipe struct {
Data []Item `json:"data"`
}
rcp := recipe{}
q, err := URLUnescape(r.URL)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
op := SearchOption{
Project: q.Get("project"),
Searchword: q.Get("searchword"),
Sortkey: q.Get("sortkey"),
Assign: str2bool(q.Get("assign")),
Ready: str2bool(q.Get("ready")),
Wip: str2bool(q.Get("wip")),
Confirm: str2bool(q.Get("confirm")),
Done: str2bool(q.Get("done")),
Omit: str2bool(q.Get("omit")),
Hold: str2bool(q.Get("hold")),
Out: str2bool(q.Get("out")),
None: str2bool(q.Get("none")),
Shot: str2bool(q.Get("shot")),
Assets: str2bool(q.Get("asset")),
Type3d: str2bool(q.Get("type3d")),
Type2d: str2bool(q.Get("type2d")),
}
result, err := Search(session, op)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
rcp.Data = result
err = json.NewEncoder(w).Encode(rcp)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
}
// handleAPI3Items 함수는 아이템을 검색한다.
func handleAPI3Items(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Get Only", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
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.StatusInternalServerError)
return
}
q, err := URLUnescape(r.URL)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if q.Get("project") == "" {
http.Error(w, "project 문자가 비어있습니다", http.StatusInternalServerError)
return
}
op := SearchOption{
Project: q.Get("project"),
Searchword: q.Get("searchword"),
Sortkey: "id",
Assign: str2bool(q.Get("assign")),
Ready: str2bool(q.Get("ready")),
Wip: str2bool(q.Get("wip")),
Confirm: str2bool(q.Get("confirm")),
Done: str2bool(q.Get("done")),
Omit: str2bool(q.Get("omit")),
Hold: str2bool(q.Get("hold")),
Out: str2bool(q.Get("out")),
None: str2bool(q.Get("none")),
Type3d: str2bool(q.Get("type3d")),
Type2d: str2bool(q.Get("type2d")),
TrueStatus: strings.Split(q.Get("truestatus"), ","),
SearchbarTemplate: "searchbarV1",
}
if q.Get("sortkey") != "" {
op.Sortkey = "id"
}
if q.Get("searchbartemplate") == "searchbarV2" {
op.SearchbarTemplate = "searchbarV2"
}
items, err := Search(session, op)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(items)
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)
}
// handleAPIShot 함수는 project, name을 받아서 shot을 반환한다.
func handleAPIGetShot(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
session, err := mgo.Dial(*flagDBIP)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
q := r.URL.Query()
project := q.Get("project")
name := q.Get("name")
type recipe struct {
Data Item `json:"data"`
}
rcp := recipe{}
result, err := Shot(session, project, name)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
rcp.Data = result
err = json.NewEncoder(w).Encode(rcp)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
}
// handleAPIAsset 함수는 project, name을 받아서 asset을 반환한다.
func handleAPIGetAsset(w http.ResponseWriter, r *http.Request) {
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.StatusInternalServerError)
return
}
q := r.URL.Query()
project := q.Get("project")
if project == "" {
http.Error(w, "project 가 빈 문자열 입니다", http.StatusBadRequest)
return
}
name := q.Get("name")
if name == "" {
http.Error(w, "name 이 빈 문자열 입니다", http.StatusBadRequest)
return
}
item, err := Asset(session, project, name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(item)
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)
}
// handleAPISeqs 함수는 프로젝트의 시퀀스를 가져온다.
func handleAPISeqs(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Get Only", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
session, err := mgo.Dial(*flagDBIP)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
defer session.Close()
_, _, err = TokenHandler(r, session)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
q := r.URL.Query()
project := q.Get("project")
if project == "" {
fmt.Fprintln(w, "{\"error\":\"project 정보가 없습니다.\"}")
return
}
seqs, err := Seqs(session, project)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
type recipe struct {
Data []string `json:"data"`
}
rcp := recipe{}
rcp.Data = seqs
err = json.NewEncoder(w).Encode(rcp)
if err != nil {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
}
// handleAPIAssets 함수는 project 정보를 입력받아서 모든 에셋 정보를 출력한다.
func handleAPIAssets(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.StatusInternalServerError)
return
}
q := r.URL.Query()
project := q.Get("project")
if project == "" {
http.Error(w, "project 가 빈 문자열입니다", http.StatusBadRequest)
return
}
assets, err := SearchAllAsset(session, project, "name")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(assets)
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)
}
// handleAPI2Shots 함수는 project, seq를 입력받아서 cut 이름을 출력합니다.
func handleAPI2Shots(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
}
q := r.URL.Query()
project := q.Get("project")
if project == "" {
http.Error(w, "project 정보가 없습니다", http.StatusBadRequest)
return
}
seq := q.Get("seq")
if seq == "" {
http.Error(w, "seq 정보가 없습니다", http.StatusBadRequest)
return
}
shots, err := Shots(session, project, seq)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(shots)
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)
}
// handleAPIAllShots 함수는 project를 받아서 모든 샷을 출력한다.
func handleAPIAllShots(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
}
q := r.URL.Query()
project := q.Get("project")
if project == "" {
http.Error(w, "project 정보가 없습니다.", http.StatusUnauthorized)
return
}
items, err := SearchAllShot(session, project, "name")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(items)
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)
}
// handleAPIUseTypes 함수는 project를 받아서 모든 샷을 출력한다.
func handleAPIUseTypes(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
}
q := r.URL.Query()
project := q.Get("project")
if project == "" {
http.Error(w, "project 정보가 없습니다.", http.StatusUnauthorized)
return
}
name := q.Get("name")
if name == "" {
http.Error(w, "name 정보가 없습니다.", http.StatusUnauthorized)
return
}
types, err := UseTypes(session, project, name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
type Recipe struct {
Types []string `json:"types"`
}
rcp := Recipe{}
rcp.Types = types
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)
}
// handleAPI2SetTaskMov 함수는 Task에 mov를 설정한다.
func handleAPI2SetTaskMov(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"`
ID string `json:"id"`
Task string `json:"task"`
Mov string `json:"mov"`
UserID string `json:"userid"`
Error string `json:"error"`
Protocol string `json:"protocol"`
}
rcp := Recipe{}
rcp.Protocol = CachedAdminSetting.Protocol
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
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
r.ParseForm()
for key, values := range r.PostForm {
switch key {
case "project":
v, err := PostFormValueInList(key, values)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.Project = v
case "name", "shot", "asset":
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 "mov": // 앞뒤샷 포함 여러개의 mov를 등록할 수 있다.
rcp.Mov = strings.Join(values, ";")
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
}
default:
http.Error(w, key+"키는 사용할 수 없습니다.(project, shot, asset, task, mov 키값만 사용가능합니다.)", http.StatusBadRequest)
return
}
}
rcp.Mov = dipath.Win2lin(rcp.Mov) // 내부적으로 모든 경로는 unix 경로를 사용한다.
id, err := setTaskMov(session, rcp.Project, rcp.Name, rcp.Task, rcp.Mov)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.ID = id
// log
err = dilog.Add(*flagDBIP, host, fmt.Sprintf("Setmov: %s %s", rcp.Task, rcp.Mov), rcp.Project, rcp.Name, "csi3", rcp.UserID, 180)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// slack log
err = slacklog(session, rcp.Project, fmt.Sprintf("Setmov: %s %s\nProject: %s, Name: %s, Author: %s", rcp.Task, rcp.Mov, rcp.Project, rcp.Name, rcp.UserID))
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)
}
// handleAPISetTaskExpectDay 함수는 Task에 예상일을 설정한다.
func handleAPISetTaskExpectDay(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"`
ID string `json:"id"`
Task string `json:"task"`
ExpectDay int `json:"expectday"`
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
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
r.ParseForm()
project := r.FormValue("project")
if project == "" {
http.Error(w, "project를 설정해주세요", http.StatusBadRequest)
return
}
rcp.Project = project
id := r.FormValue("id")
if id == "" {
http.Error(w, "id를 설정해주세요", http.StatusBadRequest)
return
}
rcp.ID = id
task := r.FormValue("task")
if task == "" {
http.Error(w, "task를 설정해주세요", http.StatusBadRequest)
return
}
rcp.Task = task
expectday := r.FormValue("expectday")
if expectday == "" {
http.Error(w, "expectday를 설정해주세요", http.StatusBadRequest)
return
}
num, err := strconv.Atoi(expectday)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.ExpectDay = num
err = setTaskExpectDay(session, rcp.Project, rcp.ID, rcp.Task, rcp.ExpectDay)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// log
err = dilog.Add(*flagDBIP, host, fmt.Sprintf("Set ExpectDay: %s %d", rcp.Task, rcp.ExpectDay), rcp.Project, rcp.ID, "csi3", rcp.UserID, 180)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// slack log
err = slacklog(session, rcp.Project, fmt.Sprintf("Set ExpectDay: %s %d\nProject: %s, ID: %s, Author: %s", rcp.Task, rcp.ExpectDay, rcp.Project, rcp.ID, rcp.UserID))
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)
}
// handleAPISetTaskUserComment 함수는 Task에 UserComment를 설정한다.
func handleAPISetTaskUserComment(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"`
ID string `json:"id"`
Task string `json:"task"`
UserComment string `json:"usercomment"`
UserID string `json:"userid"`
}
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
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
r.ParseForm()
project := r.FormValue("project")
if project == "" {
http.Error(w, "project를 설정해주세요", http.StatusBadRequest)
return
}
rcp.Project = project
id := r.FormValue("id")
if id == "" {
http.Error(w, "id를 설정해주세요", http.StatusBadRequest)
return
}
rcp.ID = id
task := r.FormValue("task")
if task == "" {
http.Error(w, "task를 설정해주세요", http.StatusBadRequest)
return
}