/
kodi.go
136 lines (112 loc) · 2.98 KB
/
kodi.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
package main
import (
"context"
_ "embed"
"html/template"
"io/fs"
"log"
"net/http"
"sort"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/bobg/ctrlc"
"github.com/bobg/gcsobj"
"github.com/bobg/go-generics/v2/maps"
"github.com/bobg/mid"
"github.com/pkg/errors"
)
type kodi struct {
bucket *storage.BucketHandle
username, password string
node *FSNode
}
func (c maincmd) doKodi(outerCtx context.Context, dir, listen, username, password, listfile, certfile, keyfile string, _ []string) error {
return ctrlc.Run(outerCtx, func(ctx context.Context) error {
k := &kodi{
bucket: c.bucket,
username: username,
password: password,
}
log.Print("Building file system, please wait")
f, err := newFS(ctx, c.bucket, listfile)
if err != nil {
return errors.Wrap(err, "building filesystem")
}
node, err := f.root.findNode(dir, false)
if err != nil {
return errors.Wrapf(err, "finding %s", dir)
}
k.node = node
s := &http.Server{
Addr: listen,
Handler: mid.Err(k.handle),
}
log.Printf("Listening on %s", listen)
if certfile != "" && keyfile != "" {
go s.ListenAndServeTLS(certfile, keyfile)
} else {
go s.ListenAndServe()
}
<-ctx.Done()
log.Print("Context canceled, shutting down server")
err = s.Shutdown(outerCtx)
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
})
}
func (k *kodi) handle(w http.ResponseWriter, req *http.Request) error {
if k.username != "" && k.password != "" {
username, password, ok := req.BasicAuth()
if !ok || username != k.username || password != k.password {
w.Header().Add("WWW-Authenticate", `Basic realm="Access to list and stream titles"`)
return mid.CodeErr{C: http.StatusUnauthorized}
}
}
ctx := req.Context()
path := strings.Trim(req.URL.Path, "/")
if path == "" {
return k.handleDir(ctx, w, k.node)
}
node, err := k.node.findNode(path, false)
if errors.Is(err, fs.ErrNotExist) {
return mid.CodeErr{C: http.StatusNotFound}
}
if err != nil {
return errors.Wrapf(err, "getting %s", path)
}
if node.isDir() {
return k.handleDir(ctx, w, node)
}
obj := k.bucket.Object(node.hash)
r, err := gcsobj.NewReader(ctx, obj)
if err != nil {
return errors.Wrapf(err, "creating reader for object %s", node.hash)
}
defer r.Close()
wrapper := &mid.ResponseWrapper{W: w}
http.ServeContent(wrapper, req, path, time.Time{}, r)
if wrapper.Code < 200 || wrapper.Code >= 400 {
return mid.CodeErr{C: wrapper.Code}
}
return nil
}
func (k *kodi) handleDir(ctx context.Context, w http.ResponseWriter, node *FSNode) error {
var items []template.URL
keys := maps.Keys(node.children)
sort.Strings(keys)
for _, key := range keys {
child := node.children[key]
if child.isDir() {
items = append(items, template.URL(key+"/"))
} else {
items = append(items, template.URL(key))
}
}
return dirtmpl.Execute(w, items)
}
//go:embed dir.html.tmpl
var dirtmplstr string
var dirtmpl = template.Must(template.New("").Parse(dirtmplstr))