This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
forked from kosto1992/go-sitemap-generator
/
builder_file.go
93 lines (73 loc) · 2.03 KB
/
builder_file.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
package stm
import (
"bytes"
"log"
)
// builderFileError is implementation for the BuilderError interface.
type builderFileError struct {
error
full bool
}
// FullError returns true if a sitemap xml had been limit size.
func (e *builderFileError) FullError() bool {
return e.full
}
// NewBuilderFile returns the created the BuilderFile's pointer
func NewBuilderFile(opts *Options, loc *Location) *BuilderFile {
b := &BuilderFile{opts: opts, loc: loc}
b.clear()
return b
}
// BuilderFile provides implementation for the Builder interface.
type BuilderFile struct {
opts *Options
loc *Location
content []byte
linkcnt int
newscnt int
}
// Add method joins old bytes with creates bytes by it calls from Sitemap.Add method.
func (b *BuilderFile) Add(url interface{}) BuilderError {
u := MergeMap(url.(URL),
URL{{"host", b.loc.opts.defaultHost}},
)
b.linkcnt++
smu, err := NewSitemapURL(b.opts, u)
if err != nil {
log.Fatalf("[F] Sitemap: %s", err)
}
bytes := smu.XML()
if !b.isFileCanFit(bytes) {
return &builderFileError{error: err, full: true}
}
b.content = append(b.content, bytes...)
return nil
}
// isFileCanFit checks bytes to bigger than consts values.
func (b *BuilderFile) isFileCanFit(bytes []byte) bool {
r := len(append(b.content, bytes...)) < MaxSitemapFilesize
r = r && b.linkcnt < MaxSitemapLinks
return r && b.newscnt < MaxSitemapNews
}
// clear will initialize xml content.
func (b *BuilderFile) clear() {
b.content = make([]byte, 0, MaxSitemapFilesize)
}
// Content will return pooled bytes on content attribute.
func (b *BuilderFile) Content() []byte {
return b.content
}
// XMLContent will return an XML of the sitemap built
func (b *BuilderFile) XMLContent() []byte {
c := bytes.Join(bytes.Fields(XMLHeader), []byte(" "))
c = append(append(c, b.Content()...), XMLFooter...)
return c
}
// Write will write pooled bytes with header and footer to
// Location path for output sitemap file.
func (b *BuilderFile) Write() {
b.loc.ReserveName()
c := b.XMLContent()
b.loc.Write(c, b.linkcnt)
b.clear()
}