-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
111 lines (94 loc) · 2.52 KB
/
render.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
package main
import (
"bytes"
"fmt"
"github.com/docker/go-units"
"github.com/h2non/filetype"
"html/template"
"path"
)
const previewHeight = 330
func pictureWaterfall(fullPath string, previewPath string, files []fileStruct) string {
tp, err := template.ParseFiles("templates/img.html")
if err != nil {
fmt.Println(err.Error())
}
buf := new(bytes.Buffer)
for _, file := range files {
data := map[string]interface{}{
"alt": file.Name,
"src": path.Join(previewPath, file.Name),
"href": path.Join(fullPath, file.Name),
}
err = tp.Execute(buf, data)
if err != nil {
fmt.Println(err.Error())
}
}
return buf.String()
}
func folderWaterfall(baseUrl string, fullPath string, folders []fileStruct) string {
tp, err := template.ParseFiles("templates/folder-img.html")
if err != nil {
fmt.Println(err.Error())
}
buf := new(bytes.Buffer)
for _, folder := range folders {
data := map[string]interface{}{
"alt": folder.Name,
"src": path.Join(baseUrl, "/assets/folder.webp"),
"href": path.Join(fullPath, folder.Name+"/"),
}
err = tp.Execute(buf, data)
if err != nil {
fmt.Println(err.Error())
}
}
return buf.String()
}
func detailsList(groupName, action string, fileDirectory string, folders []fileStruct, files []fileStruct) string {
tp, err := template.ParseFiles("templates/details-item.html")
if err != nil {
fmt.Println(err.Error())
}
buf := new(bytes.Buffer)
for _, item := range append(folders, files...) {
var fileType, href string
if item.IsDir {
fileType = "文件夹"
href = path.Join(groupName, detailsBaseUrl, action, item.Name) + "/"
} else {
buf := readFileBytes(path.Join(fileDirectory, item.Name), 8192)
if filetype.IsVideo(buf) {
fileType = "视频"
} else if filetype.IsDocument(buf) {
fileType = "文档"
} else if filetype.IsApplication(buf) {
fileType = "应用程序"
} else if filetype.IsArchive(buf) {
fileType = "压缩包"
} else if filetype.IsAudio(buf) {
fileType = "音频"
} else if filetype.IsFont(buf) {
fileType = "字体"
} else if filetype.IsImage(buf) {
fileType = "图像"
} else {
fileType = "未知"
}
href = path.Join(groupName, fileBaseUrl, action, item.Name)
}
data := map[string]interface{}{
"name": item.Name,
"modTime": item.ModTime.Format("2006-01-02 15:04:05"),
"type": fileType,
"size": units.HumanSize(float64(item.Size)),
"href": href,
}
err = tp.Execute(buf, data)
if err != nil {
fmt.Println(err.Error())
}
}
return buf.String()
}