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

231 lines
7.1 KiB
Go
Raw Normal View History

package building
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"os"
"path"
2022-10-31 12:41:17 -07:00
"regexp"
"strconv"
"strings"
"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 id, message_raw, dir, filename, op_id FROM DBPREFIXv_front_page_posts"
2024-10-15 09:55:41 -07:00
} else {
query = "SELECT id, message_raw, dir, filename, op_id 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 fmt.Errorf("failed loading front page template: %w", err)
}
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 fmt.Errorf("failed opening front page for writing: %w", err)
}
if err = config.TakeOwnershipOfFile(frontFile); err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("failed setting file ownership for front page: %w", err)
}
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 fmt.Errorf("failed loading recent posts: %w", err)
}
if err = serverutil.MinifyTemplate(gctemplates.FrontPage, map[string]any{
"siteConfig": siteCfg,
"sections": gcsql.AllSections,
"boards": gcsql.AllBoards,
"boardConfig": config.GetBoardConfig(""),
"recentPosts": recentPostsArr,
}, frontFile, "text/html"); err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("failed executing front page template: %w", err)
}
return frontFile.Close()
}
// BuildPageHeader is a convenience function for automatically generating the top part
// of every normal HTML page
func BuildPageHeader(writer io.Writer, pageTitle string, board string, misc map[string]any) error {
phMap := map[string]any{
"pageTitle": pageTitle,
"documentTitle": pageTitle + " - " + config.GetSiteConfig().SiteName,
"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]any{}, 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 fmt.Errorf("failed loading consts.js template: %w", err)
}
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("failed opening consts.js for writing: %w", err)
}
if err = config.TakeOwnershipOfFile(constsJSFile); err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("unable to update file ownership for consts.js: %w", err)
}
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("failed building consts.js: %w", err)
}
return constsJSFile.Close()
}
func embedMedia(post *Post) (template.HTML, error) {
filenameParts := strings.SplitN(post.Filename, ":", 2)
if len(filenameParts) != 2 {
return "", errors.New("invalid embed ID")
}
boardCfg := config.GetBoardConfig(post.BoardDir)
embedTmpl, thumbTmpl, err := boardCfg.GetEmbedTemplates(filenameParts[1])
if err != nil {
return "", err
}
templateData := config.EmbedTemplateData{
MediaID: post.OriginalFilename,
HandlerID: filenameParts[1],
ThumbWidth: boardCfg.ThumbWidth,
ThumbHeight: boardCfg.ThumbHeight,
}
if !post.IsTopPost {
templateData.ThumbWidth = boardCfg.ThumbWidthReply
templateData.ThumbHeight = boardCfg.ThumbHeightReply
}
var buf bytes.Buffer
if thumbTmpl != nil {
if err := thumbTmpl.Execute(&buf, templateData); err != nil {
return "", err
}
return template.HTML(fmt.Sprintf(
`<img src=%q alt="Embedded video" class="embed thumb embed-%s" style="max-width: %dpx; max-height: %dpx;" embed-width="%d" embed-height="%d">`,
buf.String(), filenameParts[1], templateData.ThumbWidth, templateData.ThumbHeight, boardCfg.EmbedWidth, boardCfg.EmbedHeight)), nil
}
if err = embedTmpl.Execute(&buf, templateData); err != nil {
return "", err
}
return template.HTML(buf.String()), nil
}
func init() {
gctemplates.AddTemplateFuncs(template.FuncMap{
"embedMedia": embedMedia,
})
}