-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsitemap.go
More file actions
68 lines (61 loc) · 1.46 KB
/
sitemap.go
File metadata and controls
68 lines (61 loc) · 1.46 KB
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
package main
import (
"archive/zip"
"encoding/xml"
"log/slog"
"net/url"
"strings"
"time"
)
type SiteMapRoot struct {
XMLName xml.Name `xml:"urlset"`
NS string `xml:"xmlns,attr"`
SiteList []*SiteURL `xml:"url"`
}
func (root SiteMapRoot) LastMod() time.Time {
res := time.Unix(0, 0)
for _, v := range root.SiteList {
if res.Before(v.UpdatedAt) {
res = v.UpdatedAt
}
}
return res
}
type SiteURL struct {
URL string `xml:"loc"`
UpdatedAt time.Time `xml:"lastmod"`
}
func (r *SiteMapRoot) initialize() error {
r.NS = "http://www.sitemaps.org/schemas/sitemap/0.9"
return nil
}
func (r *SiteMapRoot) AddZip(baseurl string, fi *zip.File) error {
if fi.FileInfo().IsDir() {
return nil
}
u, err := url.JoinPath(baseurl, fi.Name)
if err != nil {
slog.Error("joinpath", "base", baseurl, "name", fi.Name)
return err
}
if strings.HasSuffix(u, "/index.html") {
u = strings.TrimSuffix(u, "index.html")
}
r.SiteList = append(r.SiteList, &SiteURL{URL: u, UpdatedAt: fi.Modified})
return nil
}
func (r *SiteMapRoot) AddFile(baseurl string, indexname string, filename string, updated time.Time) error {
if baseurl == "" {
return nil
}
u, err := url.JoinPath(baseurl, filename)
if err != nil {
slog.Error("joinpath", "base", baseurl, "name", filename)
return err
}
if strings.HasSuffix(u, "/"+indexname) {
u = strings.TrimSuffix(u, indexname)
}
r.SiteList = append(r.SiteList, &SiteURL{URL: u, UpdatedAt: updated})
return nil
}