repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / feeds
Antonio Mika · 08 Oct 24

scp_hooks.go

 1package feeds
 2
 3import (
 4	"fmt"
 5	"strings"
 6	"time"
 7
 8	"slices"
 9
10	"github.com/charmbracelet/ssh"
11	"github.com/picosh/pico/db"
12	"github.com/picosh/pico/filehandlers"
13	"github.com/picosh/pico/shared"
14	"github.com/picosh/utils"
15)
16
17type FeedHooks struct {
18	Cfg *shared.ConfigSite
19	Db  db.DB
20}
21
22func (p *FeedHooks) FileValidate(s ssh.Session, data *filehandlers.PostMetaData) (bool, error) {
23	if !utils.IsTextFile(string(data.Text)) {
24		err := fmt.Errorf(
25			"WARNING: (%s) invalid file must be plain text (utf-8), skipping",
26			data.Filename,
27		)
28		return false, err
29	}
30
31	if !utils.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) {
32		extStr := strings.Join(p.Cfg.AllowedExt, ",")
33		err := fmt.Errorf(
34			"WARNING: (%s) invalid file, format must be (%s), skipping",
35			data.Filename,
36			extStr,
37		)
38		return false, err
39	}
40
41	return true, nil
42}
43
44func (p *FeedHooks) FileMeta(s ssh.Session, data *filehandlers.PostMetaData) error {
45	parsedText := shared.ListParseText(string(data.Text))
46
47	if parsedText.Title == "" {
48		data.Title = utils.ToUpper(data.Slug)
49	} else {
50		data.Title = parsedText.Title
51	}
52
53	data.Description = parsedText.Description
54	data.Tags = parsedText.Tags
55
56	data.Hidden = slices.Contains(p.Cfg.HiddenPosts, data.Filename)
57
58	if data.Data.LastDigest == nil {
59		now := time.Now()
60		data.Data.LastDigest = &now
61	}
62
63	return nil
64}