/
threads.go
61 lines (56 loc) · 1.33 KB
/
threads.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
package gateway
import (
"context"
"errors"
"net/http"
gopath "path"
"github.com/gin-gonic/gin"
"github.com/textileio/go-buckets/collection"
"github.com/textileio/go-threads/core/did"
core "github.com/textileio/go-threads/core/thread"
)
func (g *Gateway) threadHandler(c *gin.Context) {
thread, err := core.Decode(c.Param("thread"))
if err != nil {
renderError(c, http.StatusBadRequest, errors.New("invalid thread ID"))
return
}
g.renderThread(c, thread)
}
func (g *Gateway) renderThread(c *gin.Context, thread core.ID) {
token := did.Token(c.Query("token"))
ctx, cancel := context.WithTimeout(context.Background(), handlerTimeout)
defer cancel()
rep, err := g.lib.List(ctx, thread, token)
if err != nil {
render404(c)
return
}
links := make([]link, len(rep))
for i, r := range rep {
var name string
if r.Name != "" {
name = r.Name
} else {
name = r.Key
}
p := gopath.Join("thread", thread.String(), collection.Name, r.Key)
if token.Defined() {
p += "?token=" + string(token)
}
links[i] = link{
Name: name,
Path: p,
Size: "",
Links: "",
}
}
c.HTML(http.StatusOK, "/public/html/unixfs.gohtml", gin.H{
"Title": "Index of " + gopath.Join("/thread", thread.String(), collection.Name),
"Root": "/",
"Path": "",
"Updated": "",
"Back": "",
"Links": links,
})
}