-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfiles.go
138 lines (134 loc) · 3.86 KB
/
files.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
package main
import (
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
)
const (
pthVideo, pthMusic, pthPhoto = "video", "music", "photo"
extVid, extAud, extPic = ".avi.mp4.mkv.mpeg.mpg.m4v.mp2.webm.ts.mts.m2ts.mov.wmv.flv", ".mp3.m4a.flac.wav.wma.aac.ogg", ".jpg.jpeg.png"
)
func init() {
for _, f := range [3]string{pthVideo, pthMusic, pthPhoto} {
http.HandleFunc("/msx/"+f+"/", files)
}
}
func files(w http.ResponseWriter, r *http.Request) {
p := filepath.Clean(strings.TrimPrefix(r.URL.Path, "/msx/"))
if f, e := os.Stat(p); os.IsNotExist(e) {
panic(404)
} else if e != nil {
panic(e)
} else if f.IsDir() {
fs, e := ioutil.ReadDir(p)
check(e)
var (
l *plist
ext, hdr string
z = "label"
ts, ms []plistObj
t byte
hl, hs int
)
id := r.FormValue("id")
if strings.IndexRune(p, filepath.Separator) > 0 {
hdr = "{ico:msx-white-soft:folder-open} " + f.Name()
}
switch t = p[0]; t {
case 'p':
opt := plistObj{"key": "green", "label": "{dic:Files|List of files} {ico:apps}", "action": "execute:http://" + r.Host + "/settings?id={ID}", "data": cPhoto}
ext = extPic
if stg.Clients[id]&cPhoto != 0 {
opt["label"] = "{dic:Files|List of files} {ico:list}"
z, l = "title", &plist{
Type: "list", Head: hdr, Ext: "{ico:msx-white:photo-library}",
Template: plistObj{"type": "separate", "imageFiller": "smart", "layout": "0,0,4,4"}, Compress: true,
Options: options("", opt, plistObj{"key": "yellow", "label": "{dic:Up|up}", "action": "focus:index:0"}),
}
} else {
l = mediaList(r, hdr, "{ico:msx-white:photo-library}", "image", []plistObj{opt}, false, false, false)
}
l.Flag = "photo"
if hh := r.FormValue("height"); hh != "" && stg.FFmpeg != "" {
if hs, _ = strconv.Atoi(hh); hs > 0 {
if stg.Clients[id]&cPhotoScale != 0 {
hl = hs
}
hs /= 4
}
}
case 'm':
l, ext = mediaList(r, hdr, "{ico:msx-white:library-music}", "audiotrack", nil, false, true, true), extAud
default:
l, ext = mediaList(r, hdr, "{ico:msx-white:video-library}", "movie", nil, false, false, true), extVid
}
for _, f := range fs {
n := f.Name()
x, u := strings.ToLower(filepath.Ext(n)), "http://"+r.Host+r.URL.EscapedPath()+url.PathEscape(n)
switch {
case f.IsDir():
q := "/?id={ID}"
if t == 'p' {
q += "&height={HEIGHT}"
}
l.Items = append(l.Items, plistObj{"icon": "msx-yellow:folder", "label": n, "action": "content:" + u + q})
case x == ".torrent":
if t != 'p' && stg.TorrServer != "" {
ts = append(ts, plistObj{"icon": "msx-yellow:offline-bolt", "label": n, "action": "content:http://" + r.Host + "/msx/torr?id={ID}&link=" + url.QueryEscape(u)})
}
case t != 'p' && stg.Background != "" && n == stg.Background:
l.Background = u
case strings.Contains(ext, x):
i := plistObj{z: n, "playerLabel": n, "extensionLabel": sizeFormat(f.Size())}
switch t {
case 'p':
uu := strings.Replace(u, "/msx/", pthFFmpeg, 1) + "?height="
if hl > 0 {
i["action"] = "image:" + uu + strconv.Itoa(hl)
} else {
i["action"] = "image:" + u
}
if stg.Clients[id]&cPhoto != 0 {
if hs > 0 {
i["image"] = uu + strconv.Itoa(hs)
} else {
i["image"] = u
}
}
case 'm':
i["cover"] = strings.Replace(u, "/msx/", pthFFmpeg, 1)
fallthrough
default:
i["action"] = playerURL(id, u, t == 'v')
}
ms = append(ms, i)
}
}
l.Items = append(l.Items, append(ts, ms...)...)
l.write(w)
} else {
http.ServeFile(w, r, p)
}
}
func sizeFormat(i int64) string {
f, b, p := float64(i), "", 0
for _, b = range []string{" B", " KB", " MB", " GB", " TB"} {
if f < 1000 {
break
} else if b != " TB" {
f = f / 1024
}
}
switch {
case f < 10:
p++
fallthrough
case f < 100:
p++
}
return strconv.FormatFloat(f, 'f', p, 64) + b
}