-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwfs.go
executable file
·241 lines (228 loc) · 6.26 KB
/
wfs.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
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/digital-idea/dipath"
"github.com/shurcooL/httpfs/html/vfstemplate"
)
var (
flagHTTP = flag.String("http", "", "service port ex):8081")
flagProtocol = flag.String("protocol", "dilink", "protocol name")
flagRootPath = flag.String("rootpath", "/show", "wfs root path")
)
type item struct {
Typ string
Path string
Filename string
}
func (i *item) SupportIcon() {
switch i.Typ {
case ".mov", ".mp4", ".avi", ".mkv", ".rv":
i.Typ = strings.TrimPrefix(i.Typ, ".")
case ".nk", ".nknc", ".ntp", ".mb", ".ma", ".blend", ".hip", ".hipnc":
i.Typ = strings.TrimPrefix(i.Typ, ".")
case ".exr", ".png", ".jpg", ".dpx", ".tga", ".psd":
i.Typ = strings.TrimPrefix(i.Typ, ".")
case ".txt", ".py", ".pyc", ".go":
i.Typ = strings.TrimPrefix(i.Typ, ".")
case ".obj", ".3dl", ".cube":
i.Typ = strings.TrimPrefix(i.Typ, ".")
case ".gz", ".zip", "bz2", ".ttf", ".pdf":
i.Typ = strings.TrimPrefix(i.Typ, ".")
default:
i.Typ = "file"
}
}
type recipe struct {
RootPath string
URLPath string
Parent string
Items []item
Error string
Nukefile string
Protocol string
}
// LoadTemplates 함수는 템플릿을 로딩합니다.
func LoadTemplates() (*template.Template, error) {
t := template.New("")
t, err := vfstemplate.ParseGlob(assets, t, "/template/*.html")
return t, err
}
// Index 함수는 wfs "/"의 endpoint 함수입니다.
func Index(w http.ResponseWriter, r *http.Request) {
user, err := user.Current()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
t, err := LoadTemplates()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
rcp := recipe{}
rcp.RootPath, err = Home2Abspath(*flagRootPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.URLPath = r.URL.Path
rcp.Protocol = *flagProtocol
if rcp.URLPath == "/" {
err = t.ExecuteTemplate(w, "index.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
if !strings.HasPrefix(rcp.URLPath, *flagRootPath) {
err = t.ExecuteTemplate(w, "nopath.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
if dipath.Exist(r.URL.Path) {
// 상위경로가 존재한다면, 부모경로를 추가한다.
if len(strings.Split(rcp.URLPath, "/")) > 2 {
rcp.Parent = filepath.Dir(rcp.URLPath)
}
// 폴더에 존재하는 파일을 불러온다.
files, err := ioutil.ReadDir(rcp.URLPath + "/")
if err != nil {
rcp.Error = err.Error()
}
for _, f := range files {
if strings.HasPrefix(f.Name(), ".") || strings.HasSuffix(f.Name(), "~") || strings.Contains(f.Name(), "autosave") || strings.HasSuffix(f.Name(), ".lnk") || strings.HasSuffix(f.Name(), ".mel") || strings.HasSuffix(f.Name(), ".tmp") {
continue
}
if f.IsDir() || strings.HasPrefix(f.Mode().String(), "L") {
// 폴더의 경우
i := item{}
i.Typ = "directory"
i.Path = rcp.URLPath + "/" + f.Name()
i.Filename = f.Name()
rcp.Items = append(rcp.Items, i)
} else {
i := item{}
i.Typ = filepath.Ext(f.Name())
i.SupportIcon()
i.Path = rcp.URLPath + "/" + f.Name()
i.Path = rcp.URLPath + "/" + f.Name()
// 경로의 시작이 $HOME 과 같다면 ~문자로 치환한다.
if strings.HasPrefix(i.Path, user.HomeDir) {
i.Path = strings.Replace(i.Path, user.HomeDir, "~", 1)
}
i.Filename = f.Name()
rcp.Items = append(rcp.Items, i)
}
}
err = t.ExecuteTemplate(w, "wfs.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
// 이미 폴더가 있다면 위쪽 조건에 의해서 파일을 브라우징한다.
// 합성팀 뉴크파일 생성
if regexpCompTask.MatchString(rcp.URLPath) {
nkf, err := nkfilename(rcp.URLPath, "")
if err != nil {
rcp.Error = err.Error()
err = t.ExecuteTemplate(w, "wfs.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
initNukefile(rcp.URLPath, nkf)
rcp.Nukefile = nkf
err = t.ExecuteTemplate(w, "createNuke.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
// 라이팅팀, 환경팀, 모션그래픽 팀은 프리컴프 파일을 생성한다.
if regexpLightTask.MatchString(rcp.URLPath) || regexpEnvTask.MatchString(rcp.URLPath) || regexpMgTask.MatchString(rcp.URLPath) {
precompPath := rcp.URLPath + "/precomp"
nkf, err := nkfilename(rcp.URLPath, "")
if err != nil {
rcp.Error = err.Error()
err = t.ExecuteTemplate(w, "wfs", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
initNukefile(precompPath, nkf)
rcp.URLPath = precompPath
rcp.Nukefile = nkf
rcp.Protocol = *flagProtocol
err = t.ExecuteTemplate(w, "createNuke.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
// 매트팀 프리컴프파일 메시지
if regexpMatteTask.MatchString(rcp.URLPath) {
// 매트팀은 폴더만 생성하길 요청함.
err := mkdirs(rcp.URLPath)
if err != nil {
rcp.Error = err.Error()
err = t.ExecuteTemplate(w, "wfs.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
err = t.ExecuteTemplate(w, "createMatte.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
// 경로가 존재하지 않는 경우
err = t.ExecuteTemplate(w, "nopath.html", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main() {
flag.Parse()
if *flagHTTP == "" {
flag.PrintDefaults()
os.Exit(1)
}
ip, err := serviceIP()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Web Server Start : http://%s%s\n", ip, *flagHTTP)
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(assets)))
http.HandleFunc("/", Index)
err = http.ListenAndServe(*flagHTTP, nil)
if err != nil {
log.Println(err)
}
}