2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2018-2019 Musing Studio LLC.
|
2018-12-24 18:45:15 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 07:05:26 +01:00
|
|
|
|
2018-11-08 07:19:03 +01:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-08-01 17:25:22 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2018-11-08 07:19:03 +01:00
|
|
|
"github.com/gorilla/mux"
|
2019-07-21 01:22:41 +02:00
|
|
|
"github.com/ikeikeikeike/go-sitemap-generator/v2/stm"
|
2018-11-08 07:19:03 +01:00
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func buildSitemap(host, alias string) *stm.Sitemap {
|
2019-07-21 01:22:41 +02:00
|
|
|
sm := stm.NewSitemap(0)
|
2018-11-08 07:19:03 +01:00
|
|
|
sm.SetDefaultHost(host)
|
|
|
|
if alias != "/" {
|
|
|
|
sm.SetSitemapsPath(alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.Create()
|
|
|
|
|
|
|
|
// Note: Do not call `sm.Finalize()` because it flushes
|
|
|
|
// the underlying datastructure from memory to disk.
|
|
|
|
|
|
|
|
return sm
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewSitemap(app *App, w http.ResponseWriter, r *http.Request) error {
|
2018-11-08 07:19:03 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
// Determine canonical blog URL
|
|
|
|
alias := vars["collection"]
|
|
|
|
subdomain := vars["subdomain"]
|
|
|
|
isSubdomain := subdomain != ""
|
|
|
|
if isSubdomain {
|
|
|
|
alias = subdomain
|
|
|
|
}
|
|
|
|
|
|
|
|
host := fmt.Sprintf("%s/%s/", app.cfg.App.Host, alias)
|
|
|
|
var c *Collection
|
|
|
|
var err error
|
|
|
|
pre := "/"
|
|
|
|
if app.cfg.App.SingleUser {
|
|
|
|
c, err = app.db.GetCollectionByID(1)
|
|
|
|
} else {
|
|
|
|
c, err = app.db.GetCollection(alias)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-15 00:54:04 +02:00
|
|
|
c.hostName = app.cfg.App.Host
|
2018-11-08 07:19:03 +01:00
|
|
|
|
|
|
|
if !isSubdomain {
|
|
|
|
pre += alias + "/"
|
|
|
|
}
|
|
|
|
host = c.CanonicalURL()
|
|
|
|
|
|
|
|
sm := buildSitemap(host, pre)
|
2019-08-07 15:26:07 +02:00
|
|
|
posts, err := app.db.GetPosts(app.cfg, c, 0, false, false, false)
|
2018-11-08 07:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error getting posts: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lastSiteMod := time.Now()
|
|
|
|
for i, p := range *posts {
|
|
|
|
if i == 0 {
|
|
|
|
lastSiteMod = p.Updated
|
|
|
|
}
|
|
|
|
u := stm.URL{
|
2019-07-21 01:22:41 +02:00
|
|
|
{"loc", p.Slug.String},
|
|
|
|
{"changefreq", "weekly"},
|
|
|
|
{"mobile", true},
|
|
|
|
{"lastmod", p.Updated},
|
2018-11-08 07:19:03 +01:00
|
|
|
}
|
2019-08-01 17:25:22 +02:00
|
|
|
if len(p.Images) > 0 {
|
2019-08-02 03:13:04 +02:00
|
|
|
imgs := []stm.URL{}
|
2019-08-01 17:25:22 +02:00
|
|
|
for _, i := range p.Images {
|
2019-08-02 03:13:04 +02:00
|
|
|
imgs = append(imgs, stm.URL{
|
|
|
|
{"loc", i},
|
|
|
|
{"title", ""},
|
2019-08-01 17:25:22 +02:00
|
|
|
})
|
2018-11-08 07:19:03 +01:00
|
|
|
}
|
2019-08-01 17:25:22 +02:00
|
|
|
u = append(u, []interface{}{"image", imgs})
|
|
|
|
}
|
2018-11-08 07:19:03 +01:00
|
|
|
sm.Add(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add top URL
|
|
|
|
sm.Add(stm.URL{
|
2019-07-21 01:22:41 +02:00
|
|
|
{"loc", pre},
|
|
|
|
{"changefreq", "daily"},
|
|
|
|
{"priority", "1.0"},
|
|
|
|
{"lastmod", lastSiteMod},
|
2018-11-08 07:19:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
w.Write(sm.XMLContent())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|