-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfiles.go
107 lines (103 loc) · 3.17 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
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() {
http.ServeFile(w, r, p)
} else {
fs, e := ioutil.ReadDir(p)
check(e)
var (
l *plist
ext string
z = "label"
ts, ms []plistObj
t byte
)
id := r.FormValue("id")
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: "{ico:msx-white-soft:folder-open} " + f.Name(), 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, "{ico:msx-white-soft:folder-open} "+f.Name(), "{ico:msx-white:photo-library}", "image", []plistObj{opt}, false)
}
l.Flag = "photo"
case 'm':
l, ext = mediaList(r, "{ico:msx-white-soft:folder-open} "+f.Name(), "{ico:msx-white:library-music}", "audiotrack", nil, false), extAud
default:
l, ext = mediaList(r, "{ico:msx-white-soft:folder-open} "+f.Name(), "{ico:msx-white:video-library}", "movie", nil, false), 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():
l.Items = append(l.Items, plistObj{"icon": "msx-yellow:folder", "label": n, "action": "content:" + u + "/?id={ID}"})
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 strings.Contains(ext, x):
i := plistObj{z: n, "playerLabel": n, "extensionLabel": sizeFormat(f.Size())}
if t == 'p' {
i["action"] = "image:" + u
if stg.Clients[id]&cPhoto != 0 {
i["image"] = u
}
} else {
i["action"] = playerURL(id, u, t == 'v')
}
ms = append(ms, i)
}
}
l.Items = append(l.Items, append(ts, ms...)...)
l.write(w)
}
}
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
}