-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler_importexport.go
2115 lines (2046 loc) · 58.9 KB
/
handler_importexport.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 (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/digital-idea/ditime"
"gopkg.in/mgo.v2"
)
// handleImportExcel 함수는 엑셀파일을 Import 하는 페이지 이다.
func handleImportExcel(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
type recipe struct {
User
SessionID string
Projectlist []string
Setting Setting
}
rcp := recipe{}
rcp.Setting = CachedAdminSetting
rcp.SessionID = ssid.ID
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
rcp.User, err = getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Projectlist, err = OnProjectlistV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 만약 사용자에게 AccessProjects가 설정되어있다면 해당리스트를 사용한다.
if len(rcp.User.AccessProjects) != 0 {
var accessProjects []string
for _, i := range rcp.Projectlist {
for _, j := range rcp.User.AccessProjects {
if i != j {
continue
}
accessProjects = append(accessProjects, j)
}
}
rcp.Projectlist = accessProjects
}
// 기존 Temp 경로 내부 .xlsx 데이터를 삭제한다.
tmp, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = RemoveExt(tmp, ".xlsx")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
err = TEMPLATES.ExecuteTemplate(w, "importexcel", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// handleImportJSON 함수는 JSON 파일을 Import 하는 페이지 이다.
func handleImportJSON(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
type recipe struct {
User
SessionID string
Projectlist []string
Setting Setting
}
rcp := recipe{}
rcp.Setting = CachedAdminSetting
rcp.SessionID = ssid.ID
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
rcp.User, err = getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Projectlist, err = OnProjectlistV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 만약 사용자에게 AccessProjects가 설정되어있다면 해당리스트를 사용한다.
if len(rcp.User.AccessProjects) != 0 {
var accessProjects []string
for _, i := range rcp.Projectlist {
for _, j := range rcp.User.AccessProjects {
if i != j {
continue
}
accessProjects = append(accessProjects, j)
}
}
rcp.Projectlist = accessProjects
}
// 기존 Temp 경로 내부 .json 데이터를 삭제한다.
tmp, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = RemoveExt(tmp, ".json")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
err = TEMPLATES.ExecuteTemplate(w, "importjson", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// handleUploadExcel 핸들러는 Excel 파일을 받아 서버에 저장한다.
func handleUploadExcel(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
// dropzone setting
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
mimeType := header.Header.Get("Content-Type")
switch mimeType {
case "text/csv":
data, err := io.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmp, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
path := tmp + "/" + header.Filename // 업로드한 파일 리스트를 불러오기 위해 뒤에 붙는 Unixtime을 제거한다.
err = os.WriteFile(path, data, 0666)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/docxconverter", "application/haansoftxlsx", "application/kset", "application/vnd.ms-excel.12", "application/vnd.openxmlformats-officedocument.spreadsheetml.shee", "x-softmaker-pm": // MS-Excel, Google & Libre Excel
data, err := io.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmp, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
path := tmp + "/" + header.Filename // 업로드한 파일 리스트를 불러오기 위해 뒤에 붙는 Unixtime을 제거한다.
err = os.WriteFile(path, data, 0666)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
http.Error(w, fmt.Sprintf("Not support: %s", mimeType), http.StatusInternalServerError) // 지원하지 않는 파일. 저장하지 않는다.
return
}
}
// handleUploadJSON 핸들러는 JSON 파일을 받아 서버에 저장한다.
func handleUploadJSON(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
// dropzone setting
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
mimeType := header.Header.Get("Content-Type")
switch mimeType {
case "application/json":
data, err := ioutil.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmp, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
path := tmp + "/" + header.Filename // 업로드한 파일 리스트를 불러오기 위해 뒤에 붙는 Unixtime을 제거한다.
err = ioutil.WriteFile(path, data, 0666)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
http.Error(w, fmt.Sprintf("Not support: %s", mimeType), http.StatusInternalServerError) // 지원하지 않는 파일. 저장하지 않는다.
return
}
}
// handleReportExcel 함수는 excel 파일을 체크하고 분석 보고서로 Redirection 한다.
func handleReportExcel(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
q := r.URL.Query()
project := q.Get("project")
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
// 파일네임을 구한다.
tmppath, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// .xlsx 파일을 읽는다.
xlsxs, err := GetXLSX(tmppath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(xlsxs) != 1 {
http.Redirect(w, r, "/importexcel", http.StatusSeeOther)
return
}
f, err := excelize.OpenFile(xlsxs[0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
type recipe struct {
Project string
Filename string
Sheet string
Overwrite bool
Rows []Excelrow
User
SessionID string
SearchOption
Errornum int
Projectlist []string
Setting Setting
}
rcp := recipe{}
rcp.Setting = CachedAdminSetting
rcp.Sheet = "Sheet1"
rcp.SessionID = ssid.ID
rcp.SearchOption = handleRequestToSearchOption(r)
rcp.User, err = getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Projectlist, err = OnProjectlistV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 만약 사용자에게 AccessProjects가 설정되어있다면 해당리스트를 사용한다.
if len(rcp.User.AccessProjects) != 0 {
var accessProjects []string
for _, i := range rcp.Projectlist {
for _, j := range rcp.User.AccessProjects {
if i != j {
continue
}
accessProjects = append(accessProjects, j)
}
}
rcp.Projectlist = accessProjects
}
var rows []Excelrow
excelRows, err := f.GetRows(rcp.Sheet)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(excelRows) == 0 {
http.Error(w, rcp.Sheet+"값이 비어있습니다.", http.StatusBadRequest)
return
}
for n, line := range excelRows {
if n == 0 { // 첫번째줄
if len(line) != 15 {
http.Error(w, "약속된 Cell 갯수가 다릅니다", http.StatusBadRequest)
return
}
continue
}
row := Excelrow{}
row.Name, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("A%d", n+1)) // Name
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if row.Name == "" { // Name이 비어있다면 넘긴다.
continue
}
row.Rnum, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("B%d", n+1)) // Rollnumber
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Shottype, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("C%d", n+1)) // Shottype(2d,3d)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Note, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("D%d", n+1)) // 작업내용
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Comment, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("E%d", n+1)) // 수정사항
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Tags, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("F%d", n+1)) // Tags
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Link, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("G%d", n+1)) // Source(제목:경로)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.JustTimecodeIn, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("H%d", n+1)) // JustTimecodeIn
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.JustTimecodeOut, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("I%d", n+1)) // JustTimecodeOut
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Ddline2D, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("J%d", n+1)) // 2D마감
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Ddline3D, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("K%d", n+1)) // 3D마감
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Findate, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("L%d", n+1)) // FIN날짜
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.Finver, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("M%d", n+1)) // FIN버전
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.HandleIn, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("N%d", n+1)) // 핸들IN
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.HandleOut, err = f.GetCellValue(rcp.Sheet, fmt.Sprintf("O%d", n+1)) // 핸들OUT
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
row.checkerrorV2(client, project)
rcp.Errornum += row.Errornum
rows = append(rows, row)
}
rcp.Rows = rows
err = TEMPLATES.ExecuteTemplate(w, "reportexcel", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// handleReportJSON 함수는 json 파일을 체크하고 분석 보고서로 Redirection 한다.
func handleReportJSON(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
q := r.URL.Query()
project := q.Get("project")
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
// 파일네임을 구한다.
tmppath, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// .json 파일을 읽는다.
jsons, err := GetJSON(tmppath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(jsons) != 1 {
http.Redirect(w, r, "/importjson", http.StatusSeeOther)
return
}
jsonFile, err := os.ReadFile(jsons[0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 파일이 정상인지 체크한다.
type recipe struct {
Project string
Filename string
Overwrite bool
Rows []Item
User
SessionID string
SearchOption
Projectlist []string
Setting Setting
}
rcp := recipe{}
rcp.Setting = CachedAdminSetting
rcp.Project = project
rcp.SessionID = ssid.ID
rcp.SearchOption = handleRequestToSearchOption(r)
rcp.User, err = getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Projectlist, err = OnProjectlistV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 만약 사용자에게 AccessProjects가 설정되어있다면 해당리스트를 사용한다.
if len(rcp.User.AccessProjects) != 0 {
var accessProjects []string
for _, i := range rcp.Projectlist {
for _, j := range rcp.User.AccessProjects {
if i != j {
continue
}
accessProjects = append(accessProjects, j)
}
}
rcp.Projectlist = accessProjects
}
var rows []Item
err = json.Unmarshal(jsonFile, &rows)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(rows) == 0 {
http.Error(w, "json 값이 비어있습니다.", http.StatusBadRequest)
return
}
rcp.Rows = rows
err = TEMPLATES.ExecuteTemplate(w, "reportjson", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// handleExcelSubmit 함수는 excel 파일을 전송한다.
func handleExcelSubmit(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
// 사용자의 이름을 구한다.
u, err := getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized) // 사용자가 존재하지 않으면 당연히 Comment를 작성하면 안된다.
return
}
authorName := u.LastNameKor + u.FirstNameKor
// 파일네임을 구한다.
tmppath, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 로그 기록을 위해서 host 값을 구한다.
_, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// .xlsx 파일을 읽는다.
xlsxs, err := GetXLSX(tmppath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(xlsxs) != 1 {
http.Redirect(w, r, "/importexcel", http.StatusSeeOther)
return
}
f, err := excelize.OpenFile(xlsxs[0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
type ErrorItem struct {
Name string
Error string
}
type recipe struct {
Filename string
Sheet string
User
SessionID string
SearchOption
ErrorItems []ErrorItem
Setting Setting
}
rcp := recipe{}
rcp.Setting = CachedAdminSetting
rcp.SessionID = ssid.ID
rcp.SearchOption = handleRequestToSearchOption(r)
rcp.User, err = getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Sheet = "Sheet1"
project := r.FormValue("project")
overwrite := str2bool(r.FormValue("overwrite"))
excelRows, err := f.GetRows(rcp.Sheet)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(excelRows) == 0 {
http.Error(w, rcp.Sheet+"값이 비어있습니다.", http.StatusBadRequest)
return
}
// 로그 처리시 로그서버에는 로그를 기록하지만, 대량이 들어갈 때 slack에는 전달하지 않습니다.
// slack에 대량의 로그가 쌓이는것을 원치않기 때문입니다.
for n, line := range excelRows {
if n == 0 { // 첫번째줄
if len(line) != 15 {
http.Error(w, "약속된 Cell 갯수가 다릅니다", http.StatusBadRequest)
return
}
continue
}
name, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("A%d", n+1)) // Name
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if name == "" { // 샷이름이 없다면 넘긴다.
continue
}
id, err := GetIDV2(client, project, name)
if err != nil {
continue // 샷 타입을 가지고 올 수 없다면 넘긴다.
}
// 롤넘버
rnum, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("B%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if rnum != "" {
err := SetRnumV2(client, id, rnum)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// Shottype 2d,3d
shottype, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("C%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if shottype != "" {
err := SetShotTypeV2(client, id, shottype)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// 작업내용
note, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("D%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if note != "" {
err := SetNoteV2(client, id, ssid.ID, note, overwrite)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// 수정사항
comment, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("E%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if comment != "" {
err = AddCommentV2(client, id, ssid.ID, authorName, time.Now().Format(time.RFC3339), comment, "", "")
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// Tags
tags, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("F%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if tags != "" {
for _, tag := range strings.Split(tags, ",") {
removeSpaceTag := strings.Replace(tag, " ", "", -1) // Tag에 존재하는 띄어쓰기를 제거한다.
if !regexpTag.MatchString(removeSpaceTag) {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: "tag에는 특수문자를 사용할 수 없습니다"})
continue
}
err = AddTagV2(client, id, removeSpaceTag)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
}
// Source(제목:경로)
sources, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("G%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if sources != "" {
for _, s := range strings.Split(sources, "\n") {
source := strings.Split(s, ":")
title := strings.TrimSpace(source[0])
path := strings.TrimSpace(source[1])
err = AddSourceV2(client, id, ssid.ID, title, path)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
}
// JustTimecodeIn
justTimecodeIn, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("H%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if justTimecodeIn != "" {
err = SetJustTimecodeInV2(client, id, justTimecodeIn)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// JustTimecoeOut
justTimecodeOut, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("I%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if justTimecodeOut != "" {
err = SetJustTimecodeOutV2(client, id, justTimecodeOut)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// 2D마감
ddline2d, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("J%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if ddline2d != "" {
date, err := ditime.ToFullTime(19, ddline2d)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
err = SetDeadline2DV2(client, id, date)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// 3D마감
ddline3d, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("K%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if ddline3d != "" {
date, err := ditime.ToFullTime(19, ddline3d)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
err = SetDeadline3DV2(client, id, date)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// FIN날짜
findate, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("L%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if findate != "" {
date, err := ditime.ToFullTime(19, findate)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
err = SetFindateV2(client, id, date)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// FIN버전
finver, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("M%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if finver != "" {
err = SetFinverV2(client, id, finver)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// HandleIn
handleIn, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("N%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if handleIn != "" {
num, err := strconv.Atoi(handleIn)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
err = SetFrameV2(client, id, "handlein", num)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
// HandleOut
handleOut, err := f.GetCellValue(rcp.Sheet, fmt.Sprintf("O%d", n+1))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if handleOut != "" {
num, err := strconv.Atoi(handleOut)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
err = SetFrameV2(client, id, "handleout", num)
if err != nil {
rcp.ErrorItems = append(rcp.ErrorItems, ErrorItem{Name: name, Error: err.Error()})
continue
}
}
}
err = TEMPLATES.ExecuteTemplate(w, "resultexcel", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// handleJSONSubmit 함수는 json 파일을 전송한다.
func handleJSONSubmit(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel == 0 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
// 파일네임을 구한다.
tmppath, err := userTemppath(ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 로그 기록을 위해서 host 값을 구한다.
_, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// .xlsx 파일을 읽는다.
jsonFiles, err := GetJSON(tmppath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(jsonFiles) != 1 {
http.Redirect(w, r, "/importexcel", http.StatusSeeOther)
return
}
jsonFile, err := ioutil.ReadFile(jsonFiles[0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
type recipe struct {
Filename string
User
SessionID string
SearchOption
Setting Setting
}
rcp := recipe{}
rcp.Setting = CachedAdminSetting
rcp.SessionID = ssid.ID
rcp.SearchOption = handleRequestToSearchOption(r)
rcp.User, err = getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
overwrite := str2bool(r.FormValue("overwrite"))
var rows []Item
err = json.Unmarshal(jsonFile, &rows)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(rows) == 0 {
http.Error(w, "json 값이 비어있습니다.", http.StatusBadRequest)
return
}
for _, i := range rows {
if overwrite {
err = setItemV2(client, i) // 기존데이터를 덮어쓰기 한다.
if err != nil && err == mgo.ErrNotFound {
// 새로운 데이터를 추가한다.
err = addItemV2(client, i)
if err != nil {
log.Println(err)
}
} else {
log.Println(err)
}
} else {
// 새로운 데이터를 추가한다.
err = addItemV2(client, i)
if err != nil {
log.Println(err)
}
}
}
err = TEMPLATES.ExecuteTemplate(w, "resultjson", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}