repos / pico

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

pico / cmd / scripts / file-size-sync
Eric Bower · 08 Apr 24

sync.go

 1package main
 2
 3import (
 4	"encoding/binary"
 5	"log/slog"
 6	"os"
 7
 8	"github.com/picosh/pico/db/postgres"
 9	"github.com/picosh/pico/shared"
10)
11
12func bail(err error) {
13	if err != nil {
14		panic(err)
15	}
16}
17
18func main() {
19	logger := slog.Default()
20
21	picoCfg := shared.NewConfigSite()
22	picoCfg.Logger = logger
23	picoCfg.DbURL = os.Getenv("DATABASE_URL")
24	picoDb := postgres.NewDB(picoCfg.DbURL, picoCfg.Logger)
25
26	posts, err := picoDb.FindPosts()
27	bail(err)
28	for _, post := range posts {
29		if post.Space == "imgs" {
30			continue
31		}
32		post.FileSize = binary.Size([]byte(post.Text))
33		_, err := picoDb.UpdatePost(post)
34		bail(err)
35	}
36}