1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-08 21:46:22 -07:00
gochan/pkg/building/building.go

181 lines
5.6 KiB
Go
Raw Normal View History

package building
import (
"errors"
"fmt"
"io"
"os"
"path"
2022-10-31 12:41:17 -07:00
"regexp"
"strconv"
"github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/gcsql"
"github.com/gochan-org/gochan/pkg/gctemplates"
"github.com/gochan-org/gochan/pkg/gcutil"
"github.com/gochan-org/gochan/pkg/posting/uploads"
"github.com/gochan-org/gochan/pkg/server/serverutil"
)
2022-10-31 12:41:17 -07:00
var (
bbcodeTagRE = regexp.MustCompile(`\[/?[^\[\]\s]+\]`)
)
2024-09-09 17:19:56 -07:00
type frontPagePost struct {
2022-10-31 12:41:17 -07:00
Board string
URL string
ThumbURL string
2022-12-21 15:34:22 -08:00
Filename string
2022-10-31 12:41:17 -07:00
FileDeleted bool
MessageSample string
}
2024-09-09 17:19:56 -07:00
func getFrontPagePosts() ([]frontPagePost, error) {
2022-10-31 12:41:17 -07:00
siteCfg := config.GetSiteConfig()
2024-10-15 09:55:41 -07:00
var query string
if siteCfg.RecentPostsWithNoFile {
// get recent posts, including those with no file
query = "SELECT * FROM DBPREFIXv_front_page_posts"
} else {
query = "SELECT * FROM DBPREFIXv_front_page_posts_with_file"
}
2024-10-15 09:55:41 -07:00
query += " ORDER BY id DESC LIMIT " + strconv.Itoa(siteCfg.MaxRecentPosts)
rows, cancel, err := gcsql.QueryTimeoutSQL(nil, query)
defer cancel()
2022-10-31 12:41:17 -07:00
if err != nil {
return nil, err
}
defer rows.Close()
2024-09-09 17:19:56 -07:00
var recentPosts []frontPagePost
2022-10-31 12:41:17 -07:00
for rows.Next() {
2024-09-09 17:19:56 -07:00
var post frontPagePost
var id, topPostID string
var message, boardDir, filename string
err = rows.Scan(&id, &message, &boardDir, &filename, &topPostID)
2022-10-31 12:41:17 -07:00
if err != nil {
return nil, err
}
message = bbcodeTagRE.ReplaceAllString(message, "")
if len(message) > 40 {
message = message[:37] + "..."
}
thumbnail, _ := uploads.GetThumbnailFilenames(filename)
2024-09-09 17:19:56 -07:00
post = frontPagePost{
2022-10-31 12:41:17 -07:00
Board: boardDir,
URL: config.WebPath(boardDir, "res", topPostID+".html") + "#" + id,
ThumbURL: config.WebPath(boardDir, "thumb", thumbnail),
2022-12-21 15:34:22 -08:00
Filename: filename,
2022-10-31 12:41:17 -07:00
FileDeleted: filename == "deleted",
MessageSample: message,
}
recentPosts = append(recentPosts, post)
}
return recentPosts, rows.Close()
2022-10-31 12:41:17 -07:00
}
// BuildFrontPage builds the front page using templates/front.html
func BuildFrontPage() error {
errEv := gcutil.LogError(nil).
Str("template", "front")
defer errEv.Discard()
err := gctemplates.InitTemplates(gctemplates.FrontPage)
if err != nil {
errEv.Err(err).Caller().Send()
return errors.New("Error loading front page template: " + err.Error())
}
criticalCfg := config.GetSystemCriticalConfig()
frontFile, err := os.OpenFile(path.Join(criticalCfg.DocumentRoot, "index.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode)
if err != nil {
errEv.Err(err).Caller().Send()
return errors.New("Failed opening front page for writing: " + err.Error())
}
if err = config.TakeOwnershipOfFile(frontFile); err != nil {
errEv.Err(err).Caller().Send()
return errors.New("Failed setting file ownership for front page: " + err.Error())
}
2024-09-09 17:19:56 -07:00
var recentPostsArr []frontPagePost
siteCfg := config.GetSiteConfig()
2024-09-09 17:19:56 -07:00
recentPostsArr, err = getFrontPagePosts()
2020-05-23 19:40:29 +02:00
if err != nil {
errEv.Err(err).Caller().Send()
return errors.New("Failed loading recent posts: " + err.Error())
}
if err = serverutil.MinifyTemplate(gctemplates.FrontPage, map[string]interface{}{
"siteConfig": siteCfg,
"sections": gcsql.AllSections,
"boards": gcsql.AllBoards,
"boardConfig": config.GetBoardConfig(""),
"recentPosts": recentPostsArr,
}, frontFile, "text/html"); err != nil {
errEv.Err(err).Caller().Send()
return errors.New("Failed executing front page template: " + err.Error())
}
return frontFile.Close()
}
// BuildPageHeader is a convenience function for automatically generating the top part
// of every normal HTML page
2022-08-01 16:08:57 -07:00
func BuildPageHeader(writer io.Writer, pageTitle string, board string, misc map[string]interface{}) error {
phMap := map[string]interface{}{
"pageTitle": pageTitle,
"siteConfig": config.GetSiteConfig(),
"sections": gcsql.AllSections,
"boards": gcsql.AllBoards,
"boardConfig": config.GetBoardConfig(board),
2022-08-01 16:08:57 -07:00
}
for k, val := range misc {
phMap[k] = val
}
return serverutil.MinifyTemplate(gctemplates.PageHeader, phMap, writer, "text/html")
}
// BuildPageFooter is a convenience function for automatically generating the bottom
// of every normal HTML page
func BuildPageFooter(writer io.Writer) (err error) {
return serverutil.MinifyTemplate(gctemplates.PageFooter,
map[string]interface{}{}, writer, "text/html")
}
// BuildJS minifies (if enabled) consts.js, which is built from a template
func BuildJS() error {
// build consts.js from template
err := gctemplates.InitTemplates(gctemplates.JsConsts)
errEv := gcutil.LogError(nil).Str("building", "consts.js")
defer errEv.Discard()
if err != nil {
errEv.Err(err).Caller().Send()
return errors.New("Error loading consts.js template:" + err.Error())
}
boardCfg := config.GetBoardConfig("")
criticalCfg := config.GetSystemCriticalConfig()
constsJSPath := path.Join(criticalCfg.DocumentRoot, "js", "consts.js")
constsJSFile, err := os.OpenFile(constsJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, config.NormalFileMode)
if err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("error opening consts.js for writing: %s", err.Error())
}
if err = config.TakeOwnershipOfFile(constsJSFile); err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("unable to update file ownership for consts.js: %s", err.Error())
}
2023-06-16 08:42:17 -07:00
if err = serverutil.MinifyTemplate(gctemplates.JsConsts, map[string]any{
"styles": boardCfg.Styles,
"defaultStyle": boardCfg.DefaultStyle,
"webroot": criticalCfg.WebRoot,
"timezone": criticalCfg.TimeZone,
"fileTypes": boardCfg.AllowOtherExtensions,
2023-06-16 08:42:17 -07:00
}, constsJSFile, "text/javascript"); err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("error building consts.js: %s", err.Error())
}
return constsJSFile.Close()
}