repos / pico

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

commit
f8e4071
parent
01ffc21
author
Antonio Mika
date
2023-11-12 22:13:41 +0000 UTC
Merge pull request #53 from picosh/post-filesize

post filesize script
1 files changed,  +46, -0
A cmd/scripts/file-size-sync/sync.go
+46, -0
 1@@ -0,0 +1,46 @@
 2+package main
 3+
 4+import (
 5+	"encoding/binary"
 6+	"log"
 7+	"os"
 8+
 9+	"github.com/picosh/pico/db/postgres"
10+	"github.com/picosh/pico/wish/cms/config"
11+	"go.uber.org/zap"
12+)
13+
14+func createLogger() *zap.SugaredLogger {
15+	logger, err := zap.NewProduction()
16+	if err != nil {
17+		log.Fatal(err)
18+	}
19+
20+	return logger.Sugar()
21+}
22+
23+func bail(err error) {
24+	if err != nil {
25+		panic(err)
26+	}
27+}
28+
29+func main() {
30+	logger := createLogger()
31+
32+	picoCfg := config.NewConfigCms()
33+	picoCfg.Logger = logger
34+	picoCfg.DbURL = os.Getenv("DATABASE_URL")
35+	picoDb := postgres.NewDB(picoCfg.DbURL, picoCfg.Logger)
36+
37+	posts, err := picoDb.FindPosts()
38+	bail(err)
39+	for _, post := range posts {
40+		if post.Space == "imgs" {
41+			continue
42+		}
43+		post.FileSize = binary.Size([]byte(post.Text))
44+		_, err := picoDb.UpdatePost(post)
45+		bail(err)
46+	}
47+}