summaryrefslogtreecommitdiffci
path: root/internal/blog/b2.go
blob: f2ac8907d1f95ea6e3b76e36b2fa095f44d4bbbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package blog

import (
	"context"
	"encoding/json"
	"fmt"
	"slices"
	"strings"
	"time"

	"github.com/Backblaze/blazer/b2"
	"github.com/SayaAndy/saya-today-web/config"
	"github.com/SayaAndy/saya-today-web/internal/frontmatter"
)

type B2Client struct {
	prefix string
	bucket *b2.Bucket
	b2cl   *b2.Client
}

func NewB2Client(cfg *config.StorageConfig) (Client, error) {
	if cfg.Type != "b2" {
		return nil, fmt.Errorf("invalid storage type for B2InputClient")
	}
	b2cfg := cfg.Config.(*config.B2Config)

	b2cl, err := b2.NewClient(context.Background(), b2cfg.KeyID, b2cfg.ApplicationKey)
	if err != nil {
		return nil, err
	}

	bucket, err := b2cl.Bucket(context.Background(), b2cfg.BucketName)
	if err != nil {
		return nil, err
	}

	return &B2Client{b2cl: b2cl, bucket: bucket, prefix: b2cfg.Prefix}, nil
}

func (c *B2Client) GetMedleys() ([]MedleyEntry, error) {
	idxRaw, err := c.readAll(MedleysIndexFileName)
	if err != nil {
		return nil, fmt.Errorf("read %s: %w", MedleysIndexFileName, err)
	}

	var idx []MedleyEntry
	if err := json.Unmarshal(idxRaw, &idx); err != nil {
		return nil, fmt.Errorf("unmarshal %s: %w", MedleysIndexFileName, err)
	}

	return idx, nil
}

func (c *B2Client) Scan(prefix string) ([]*Page, error) {
	filePaths := []*Page{}

	iter := c.bucket.List(context.Background(), b2.ListPrefix(c.prefix+prefix))

	for iter.Next() {
		obj := iter.Object()
		if obj == nil {
			return nil, fmt.Errorf("failed to reference object in B2 bucket")
		}

		attrs, err := obj.Attrs(context.Background())
		if err != nil {
			return nil, fmt.Errorf("get attributes for object: %w", err)
		}

		if attrs.Status != b2.Uploaded {
			continue
		}

		if !strings.Contains(attrs.ContentType, "text/markdown") {
			continue
		}

		if _, ok := attrs.Info["title"]; !ok {
			continue
		}

		publishedTime, err := time.Parse(time.RFC3339, attrs.Info["published-time"])
		if err != nil {
			return nil, fmt.Errorf("failed to parse published time metadata field: %w", err)
		}

		link := obj.Name()
		fileName := link[strings.LastIndex(link, "/")+1 : strings.LastIndex(link, ".")]
		tags := strings.Split(attrs.Info["tags"], ",")
		slices.Sort(tags)

		lang, _ := strings.CutPrefix(link[0:strings.Index(link, "/")], c.prefix)

		filePaths = append(filePaths, &Page{
			Link:         link,
			FileName:     fileName,
			Lang:         lang,
			ModifiedTime: attrs.LastModified,
			Metadata: &frontmatter.Metadata{
				Title:            attrs.Info["title"],
				ShortDescription: attrs.Info["short-description"],
				ActionDate:       attrs.Info["action-date"],
				PublishedTime:    publishedTime,
				Thumbnail:        attrs.Info["thumbnail"],
				Tags:             tags,
				Geolocation:      attrs.Info["geolocation"],
			},
		})
	}

	if err := iter.Err(); err != nil {
		return nil, fmt.Errorf("iterate over B2 objects: %w", err)
	}

	return filePaths, nil
}

func (c *B2Client) ReadAll(path string) ([]byte, error) {
	return c.readAll(c.prefix + path)
}

func (c *B2Client) readAll(path string) ([]byte, error) {
	obj := c.bucket.Object(path)
	if obj == nil {
		return nil, fmt.Errorf("failed to reference object in B2 bucket")
	}
	attrs, err := obj.Attrs(context.Background())
	if err != nil {
		return nil, fmt.Errorf("error getting attributes of an object: %w", err)
	}

	content := make([]byte, attrs.Size)
	reader := obj.NewReader(context.Background())

	if _, err = reader.Read(content); err != nil {
		return nil, fmt.Errorf("failed to read file content: %w", err)
	}

	return content, nil
}

func (c *B2Client) ReadFrontmatter(path string) (metadata *frontmatter.Metadata, markdown []byte, err error) {
	contentBytes, err := c.ReadAll(path)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to read file for frontmatter parsing: %w", err)
	}

	return frontmatter.ParseFrontmatter(contentBytes)
}