-
Notifications
You must be signed in to change notification settings - Fork 0
/
browse.go
163 lines (140 loc) · 4.17 KB
/
browse.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
package web
import (
"context"
_ "embed"
"html/template"
"net/http"
"path"
"path/filepath"
"strings"
"github.com/fatalbanana/filetundra/internal/env"
"github.com/fatalbanana/filetundra/internal/idx"
"github.com/fatalbanana/filetundra/internal/log"
"github.com/fatalbanana/filetundra/internal/properties"
"github.com/blugelabs/bluge"
"github.com/blugelabs/bluge/search"
"go.uber.org/zap"
)
//go:embed templates/browse.html
var browseTemplate string
type DirectoryListing struct {
Autoback bool
Back string
Name string
Files []DirectoryListingFile
SearchValue string
}
type DirectoryListingFile struct {
Name string
Image string
Path string
}
func pathToDirectoryListing(ctx context.Context, searchPath string, virtualPath string) (res DirectoryListing, err error) {
reader, err := bluge.OpenReader(idx.BlugeConfig)
if err != nil {
return res, err
}
defer reader.Close()
query := bluge.NewTermQuery(searchPath).SetField(properties.Dirname)
searchReq := bluge.NewAllMatches(query)
searchResults, err := reader.Search(ctx, searchReq)
if err != nil {
return res, err
}
res.Name = virtualPath
res.Files = make([]DirectoryListingFile, 0)
if virtualPath != "/" && virtualPath != "" {
virtualParentDir, _ := path.Split(virtualPath)
res.Back = path.Join("/browse", virtualParentDir)
}
var next *search.DocumentMatch
var fi idx.FileInfo
next, err = searchResults.Next()
for err == nil && next != nil {
fi, err = idx.DocumentMatchToFileInfo(reader, next)
if err != nil {
return res, err
}
basePath := "/download"
if fi.MimeType == "inode/directory" {
basePath = "/browse"
}
properBasename := fi.BareBasename + fi.Extname
fileRes := DirectoryListingFile{
Name: properBasename,
Image: getImage(fi.MimeType),
Path: path.Join(basePath, virtualPath, properBasename),
}
res.Files = append(res.Files, fileRes)
next, err = searchResults.Next()
}
return res, err
}
func browseHandler(w http.ResponseWriter, r *http.Request) {
virtualPath := path.Clean(strings.TrimPrefix(r.URL.Path, "/browse") + "/")
searchPath := filepath.Join(env.Env.Root, virtualPath)
t, err := template.New("browse").Parse(browseTemplate)
if err != nil {
log.Logger.Error("error preparing browse template",
zap.String("directory", searchPath),
zap.Error(err))
http.Error(w, "", http.StatusInternalServerError)
return
}
var res DirectoryListing
res, err = pathToDirectoryListing(r.Context(), searchPath, virtualPath)
if err != nil {
log.Logger.Error("error fetching directory listing",
zap.String("directory", searchPath),
zap.Error(err))
http.Error(w, "", http.StatusInternalServerError)
return
}
err = t.Execute(w, res)
if err != nil {
log.Logger.Error("error rendering template", zap.Error(err))
panic(http.ErrAbortHandler)
}
}
func getImage(mType string) string {
switch mType {
case "application/gzip":
case "application/vnd.debian.binary-package":
case "application/vnd.rar":
case "application/x-7z-compressed":
case "application/x-rpm":
case "application/x-tar":
case "application/x-unix-archive":
case "application/x-xz":
case "application/zip":
case "application/zstd":
return "/static/icons/archive-unindexed.svg"
case "inode/directory":
return "/static/icons/directory.svg"
case "application/msword":
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return "/static/icons/document.svg"
case "application/x-executable":
case "application/x-mach-binary":
case "application/vnd.microsoft.portable-executable":
return "/static/icons/executable.svg"
case "application/pdf":
return "/static/icons/pdf.svg"
case "application/vnd.ms-powerpoint":
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
return "/static/icons/presentation.svg"
case "application/vnd.ms-excel":
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
return "/static/icons/spreadsheet.svg"
}
majorVersion := strings.Split(mType, "/")[0]
switch majorVersion {
case "image":
return "/static/icons/image.svg"
case "audio":
return "/static/icons/audio.svg"
case "video":
return "/static/icons/video.svg"
}
return "/static/icons/text.svg"
}