-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler_review.go
349 lines (340 loc) · 10.9 KB
/
handler_review.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
func handleDailyReviewStatus(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
}
today := time.Now().Format("2006-01-02")
// 오늘날짜를 구하고 리다이렉트한다.
http.Redirect(w, r, "/reviewstatus?searchword=createtime:"+today, http.StatusSeeOther)
}
// handleReviewData 함수는 리뷰 영상데이터를 전송한다.
func handleReviewData(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel < 2 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
q := r.URL.Query()
id := q.Get("id")
ext := q.Get("ext") // 확장자를 자동으로 가지고 오지 않는 이유는 DB에 접근하는것을 줄이기 위해서이다.
if ext == "" {
ext = ".mp4" // 확장자가 없다면 기본적으로 mp4를 불러온다.
}
http.ServeFile(w, r, fmt.Sprintf("%s/%s%s", CachedAdminSetting.ReviewDataPath, id, ext))
}
// handleReviewDrawingData 함수는 리뷰 드로잉 데이터를 전송한다.
func handleReviewDrawingData(w http.ResponseWriter, r *http.Request) {
ssid, err := GetSessionID(r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
if ssid.AccessLevel < 2 {
http.Redirect(w, r, "/invalidaccess", http.StatusSeeOther)
return
}
q := r.URL.Query()
id := q.Get("id")
frameStr := q.Get("frame")
_ = q.Get("time") // 브라우저에서 캐쉬되지 않은 이미지를 가지고 오기 위해 time 옵션을 사용한다. 리소스 URL로 이미지를 캐쉬하기 때문이다.
frame, err := strconv.Atoi(frameStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
http.ServeFile(w, r, fmt.Sprintf("%s/%s.%06d.png", CachedAdminSetting.ReviewDataPath, id, frame))
}
// handleReviewStatusSubmit 함수는 리뷰 검색창의 검색어를 입력받아 새로운 URI로 리다이렉션 한다.
func handleReviewStatusSubmit(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Post Only", http.StatusMethodNotAllowed)
return
}
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
}
searchword := r.FormValue("searchword")
reviewProject := r.FormValue("reviewproject")
itemStatus := r.FormValue("itemstatus")
createtime := r.FormValue("review-createtime")
task := r.FormValue("review-task")
redirectURL := fmt.Sprintf("/reviewstatus?searchword=%s&project=%s&itemstatus=%s&createtime=%s&task=%s", searchword, reviewProject, itemStatus, createtime, task)
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
}
// handleUploadReviewFile 함수는 리뷰 파일을 업로드 한다.
func handleUploadReviewFile(w http.ResponseWriter, r *http.Request) {
type Recipe struct {
Path string `json:"path"`
Unixtime string `json:"unixtime"`
Project string `json:"project"`
Vendor string `json:"vendor"`
Partner string `json:"partner"`
Task string `json:"task"`
Year string `json:"year"`
Month string `json:"month"`
Day string `json:"day"`
Type string `json:"type"`
Ext string `json:"ext"`
}
rcp := Recipe{}
rcp.Unixtime = fmt.Sprintf("%d", time.Now().Unix())
// MultipartForm을 파싱합니다.
buffer := CachedAdminSetting.MultipartFormBufferSize // 이 절차를 위해 매번 DB에 접근하지 않기 위해서 CachedAdminSetting을 이용합니다.
err := r.ParseMultipartForm(int64(buffer))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Project = r.FormValue("project")
rcp.Vendor = r.FormValue("vendor")
rcp.Partner = r.FormValue("partner")
rcp.Task = r.FormValue("task")
y, m, d := time.Now().Date()
rcp.Year = fmt.Sprintf("%04d", y)
rcp.Month = fmt.Sprintf("%02d", int(m))
rcp.Day = fmt.Sprintf("%02d", d)
for _, files := range r.MultipartForm.File {
for _, f := range files {
if f.Size == 0 {
http.Error(w, "파일사이즈가 0 바이트입니다", http.StatusInternalServerError)
return
}
file, err := f.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
defer file.Close()
mimeType := f.Header.Get("Content-Type")
ext := strings.ToLower(filepath.Ext(f.Filename))
switch mimeType {
case "image/jpeg":
rcp.Type = "image"
rcp.Ext = ".jpg"
if !(ext == ".jpg" || ext == ".jpeg") {
http.Error(w, ".jpg, .png 이미지만 허용합니다", http.StatusBadRequest)
return
}
case "image/png":
rcp.Type = "image"
rcp.Ext = ".png"
if !(ext == ".png") {
http.Error(w, ".jpg, .png 이미지만 허용합니다", http.StatusBadRequest)
return
}
case "video/quicktime", "video/mp4":
rcp.Type = "clip"
rcp.Ext = ".mp4"
if !(ext == ".mov" || ext == ".mp4") { // .mov, .mp4 외에는 허용하지 않는다.
http.Error(w, "허용하지 않는 파일 포맷입니다", http.StatusBadRequest)
return
}
default:
//허용하지 않는 파일 포맷입니다.
http.Error(w, "허용하지 않는 파일 포맷입니다", http.StatusBadRequest)
return
}
data, err := io.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if CachedAdminSetting.ReviewUploadPathPermission == "" {
http.Error(w, "Need Upload path and file permission in adminsetting.", http.StatusBadRequest)
return
}
filePerm, err := strconv.ParseInt(CachedAdminSetting.ReviewUploadPathPermission, 8, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var path string
if CachedAdminSetting.ReviewUploadPath == "" {
path, err = os.MkdirTemp("", "")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
// Review Upload Path를 파싱한다.
var reviewUploadPath bytes.Buffer
reviewUploadPathTmpl, err := template.New("reviewUploadPath").Parse(CachedAdminSetting.ReviewUploadPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = reviewUploadPathTmpl.Execute(&reviewUploadPath, rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
path = reviewUploadPath.String()
// path가 존재하지 않으면 생성한다.
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.FileMode(filePerm))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
uid, err := strconv.Atoi(CachedAdminSetting.ReviewUploadPathUID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
gid, err := strconv.Atoi(CachedAdminSetting.ReviewUploadPathGID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = os.Chown(path, uid, gid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
err = os.WriteFile(path+"/"+f.Filename, data, os.FileMode(filePerm))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Path = path + "/" + f.Filename
}
}
// 업로드 경로를 리턴합니다. Dropzone에서 활용하기 위해 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)
}
// handleReviewStatus 함수는 Status 방식의 리뷰 페이지이다.
func handleReviewStatus(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())
q := r.URL.Query()
type recipe struct {
User User
Projectlist []string
SearchOption
Searchword string
Status []Status // css 생성을 위해서 필요함
CurrentReview Review // 현재 리뷰 자료구조
Reviews []Review // 옆 Review 항목
ReviewGroup []Review // 하단 Review 항목
TasksettingNames []string
Project string
ItemStatus string
Createtime string
Task string
Setting
}
rcp := recipe{}
rcp.Setting = CachedAdminSetting
rcp.Project = q.Get("project")
rcp.ItemStatus = q.Get("itemstatus")
rcp.Createtime = q.Get("createtime")
rcp.Task = q.Get("task")
rcp.Searchword = q.Get("searchword")
id := q.Get("id")
err = rcp.SearchOption.LoadCookieV2(client, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u, err := getUserV2(client, ssid.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.User = u
rcp.Projectlist, err = ProjectlistV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.TasksettingNames, err = TaskSettingNamesV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Status, err = AllStatusV2(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Searchword = setSearchFilter(rcp.Searchword, "project", rcp.Project)
rcp.Searchword = setSearchFilter(rcp.Searchword, "itemstatus", rcp.ItemStatus)
rcp.Searchword = setSearchFilter(rcp.Searchword, "task", rcp.Task)
rcp.Searchword = setSearchFilter(rcp.Searchword, "createtime", rcp.Createtime)
rcp.Reviews, err = searchReviewV2(client, rcp.Searchword)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if id != "" {
review, err := getReviewV2(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.CurrentReview = review
rcp.ReviewGroup, err = searchReviewV2(client, fmt.Sprintf("project:%s name:%s", review.Project, review.Name))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
w.Header().Set("Content-Type", "text/html")
err = TEMPLATES.ExecuteTemplate(w, "reviewstatus", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}