-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
181 lines (167 loc) · 4.01 KB
/
feed.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"html/template"
"io/fs"
"net/url"
"os"
"path/filepath"
"slices"
"time"
)
const (
XMLHeader = `<?xml version="1.0" encoding="UTF-8"?>`
// See the references in the package comment for a description of supported
// fields.
RSSTemplate = `
<rss version="2.0"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
>
<channel>
<title>{{.Metadata.Title}}</title>
<link>{{.Metadata.Link}}</link>
<description>{{.Metadata.Desc}}</description>
<language>{{.Metadata.Language}}</language>
<itunes:image href="{{.Metadata.CoverUrl}}" />
{{range .Items}}
<item>
<title>{{.Title}}</title>
<link>{{.Link}}</link>
<description>{{.Desc}}</description>
<pubDate>{{timeRFC2822 .ModTime}}</pubDate>
<enclosure url="{{.Enclosure.Url}}" length="{{.Enclosure.Length}}" Type="{{.Enclosure.Type}}" />
</item>
{{- end}}
</channel>
</rss>
`
)
// The date format required in a podcast RSS. See [2] in package documentation.
const TimeRFC2822 = "Mon, Jan 02 2006 15:04:05 MST"
type TemplateData struct {
Metadata Metadata
Items []Item
}
type Metadata struct {
Title string
Link string
Desc string
Language string
CoverUrl string
StylesheetUrl string
externalUrl string
localRoot string
}
type Item struct {
Title string
Path string
ModTime time.Time
Link string
Desc string
Enclosure Enclosure
}
type Enclosure struct {
Url string
Length int64
Type string
}
type FileInfo struct {
Path string
MimeType string
Size int64
ModTime time.Time
}
// I only use mp3/mp4 audio and have therefore only mapped those.
//
// Citing Apple [3], the following are supported, at least on iOS:
//
// "The type values for the supported file formats are: audio/x-m4a,
// audio/mpeg, video/quicktime, video/mp4, video/x-m4v, and application/pdf."
var mimeType = map[string]string{
".mp3": "audio/mpeg",
".mp4": "audio/x-m4a",
".m4a": "audio/x-m4a",
}
func GenerateFeed(m Metadata) ([]byte, map[string]FileInfo, []Item, error) {
items, err := m.Items()
if err != nil {
return nil, nil, nil, err
}
feedXml, err := m.Feed(items)
if err != nil {
return nil, nil, nil, err
}
files := make(map[string]FileInfo)
for _, it := range items {
files[it.Path] = FileInfo{
Path: filepath.Join(m.localRoot, it.Path),
MimeType: it.Enclosure.Type,
Size: it.Enclosure.Length,
ModTime: it.ModTime,
}
}
slices.SortFunc(items, func(a, b Item) int {
if b.ModTime.Before(a.ModTime) {
return -1
}
return 1
})
return feedXml, files, items, nil
}
// Reads the local file system and returns a slice of available Items
// with all the metadata required to serve them.
func (m Metadata) Items() ([]Item, error) {
if m.externalUrl[len(m.externalUrl)-1] != '/' {
panic("Meta.Items: expected externalUrl to end in '/'")
}
var pp []Item
fsys := os.DirFS(m.localRoot)
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
name := d.Name()
ext := filepath.Ext(name)
if mime, ok := mimeType[ext]; ok {
info, err := os.Stat(filepath.Join(m.localRoot, path))
if err != nil {
return err
}
title := name[:len(name)-len(ext)]
url, err := url.Parse(m.externalUrl + url.PathEscape(path))
if err != nil {
return err
}
pp = append(pp, Item{
Title: title,
Path: path,
ModTime: info.ModTime(),
Link: url.String(),
Desc: "",
Enclosure: Enclosure{
Url: url.String(),
Length: info.Size(),
Type: mime,
},
})
}
return nil
})
return pp, err
}
func (m Metadata) Feed(items []Item) ([]byte, error) {
ff := template.FuncMap{
"timeRFC2822": func(t *time.Time) string {
return t.Format(TimeRFC2822)
},
}
tmpl := template.Must(template.New("rss").Funcs(ff).Parse(RSSTemplate))
var buf bytes.Buffer
buf.Write([]byte(XMLHeader))
err := tmpl.Execute(&buf, TemplateData{m, items})
return buf.Bytes(), err
}