0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-05 05:39:14 +01:00
gitea/modules/sitemap/sitemap.go

83 lines
1.9 KiB
Go
Raw Normal View History

2022-06-25 19:06:01 +02:00
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2022-06-25 19:06:01 +02:00
package sitemap
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"time"
)
const (
sitemapFileLimit = 50 * 1024 * 1024 // the maximum size of a sitemap file
urlsLimit = 50000
2022-06-25 19:06:01 +02:00
schemaURL = "http://www.sitemaps.org/schemas/sitemap/0.9"
urlsetName = "urlset"
sitemapindexName = "sitemapindex"
)
// URL represents a single sitemap entry
2022-06-25 19:06:01 +02:00
type URL struct {
URL string `xml:"loc"`
LastMod *time.Time `xml:"lastmod,omitempty"`
}
// Sitemap represents a sitemap
2022-06-25 19:06:01 +02:00
type Sitemap struct {
XMLName xml.Name
Namespace string `xml:"xmlns,attr"`
URLs []URL `xml:"url"`
Sitemaps []URL `xml:"sitemap"`
2022-06-25 19:06:01 +02:00
}
// NewSitemap creates a sitemap
func NewSitemap() *Sitemap {
return &Sitemap{
XMLName: xml.Name{Local: urlsetName},
Namespace: schemaURL,
2022-06-25 19:06:01 +02:00
}
}
// NewSitemapIndex creates a sitemap index.
2022-06-25 19:06:01 +02:00
func NewSitemapIndex() *Sitemap {
return &Sitemap{
XMLName: xml.Name{Local: sitemapindexName},
Namespace: schemaURL,
2022-06-25 19:06:01 +02:00
}
}
// Add adds a URL to the sitemap
func (s *Sitemap) Add(u URL) {
if s.XMLName.Local == sitemapindexName {
s.Sitemaps = append(s.Sitemaps, u)
} else {
s.URLs = append(s.URLs, u)
}
2022-06-25 19:06:01 +02:00
}
// WriteTo writes the sitemap to a response
2022-06-25 19:06:01 +02:00
func (s *Sitemap) WriteTo(w io.Writer) (int64, error) {
if l := len(s.URLs); l > urlsLimit {
return 0, fmt.Errorf("The sitemap contains %d URLs, but only %d are allowed", l, urlsLimit)
}
if l := len(s.Sitemaps); l > urlsLimit {
return 0, fmt.Errorf("The sitemap contains %d sub-sitemaps, but only %d are allowed", l, urlsLimit)
2022-06-25 19:06:01 +02:00
}
buf := bytes.NewBufferString(xml.Header)
if err := xml.NewEncoder(buf).Encode(s); err != nil {
return 0, err
}
if err := buf.WriteByte('\n'); err != nil {
return 0, err
}
if buf.Len() > sitemapFileLimit {
return 0, fmt.Errorf("The sitemap has %d bytes, but only %d are allowed", buf.Len(), sitemapFileLimit)
2022-06-25 19:06:01 +02:00
}
return buf.WriteTo(w)
}