summaryrefslogtreecommitdiffci
path: root/internal/blog/s3.go
blob: 3c1a9321184c24320590440c5fd1f9ac63ff6e73 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package blog

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"strings"

	"github.com/SayaAndy/saya-today-web/config"
	"github.com/SayaAndy/saya-today-web/internal/frontmatter"
	"github.com/SayaAndy/saya-today-web/l10n"
	"github.com/aws/aws-sdk-go-v2/aws"
	awsconfig "github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/credentials"
	"github.com/aws/aws-sdk-go-v2/service/s3"
)

type S3Client struct {
	prefix     string
	bucketName string
	s3cl       *s3.Client
}

func NewS3Client(cfg *config.StorageConfig) (Client, error) {
	if cfg.Type != "s3" {
		return nil, fmt.Errorf("invalid storage type for S3Client")
	}
	s3cfg := cfg.Config.(*config.S3Config)

	opts := []func(*awsconfig.LoadOptions) error{
		awsconfig.WithRegion(s3cfg.Region),
	}
	if s3cfg.AccessKeyID != "" && s3cfg.SecretAccessKey != "" {
		opts = append(opts, awsconfig.WithCredentialsProvider(
			credentials.NewStaticCredentialsProvider(s3cfg.AccessKeyID, s3cfg.SecretAccessKey, ""),
		))
	}

	awsCfg, err := awsconfig.LoadDefaultConfig(context.Background(), opts...)
	if err != nil {
		return nil, fmt.Errorf("load AWS config: %w", err)
	}

	var s3Opts []func(*s3.Options)
	if s3cfg.Endpoint != "" {
		s3Opts = append(s3Opts, func(o *s3.Options) {
			o.BaseEndpoint = aws.String(s3cfg.Endpoint)
		})
	}
	s3Opts = append(s3Opts, func(o *s3.Options) {
		o.UsePathStyle = s3cfg.UsePathStyle
		o.DisableLogOutputChecksumValidationSkipped = true
	})

	s3cl := s3.NewFromConfig(awsCfg, s3Opts...)

	return &S3Client{s3cfg.Prefix, s3cfg.BucketName, s3cl}, nil
}

func (c *S3Client) 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 *S3Client) Scan(prefix string) ([]*Page, error) {
	out, err := c.s3cl.GetObject(context.Background(), &s3.GetObjectInput{
		Bucket: aws.String(c.bucketName),
		Key:    aws.String(IndexFileName),
	})
	if err != nil {
		return nil, fmt.Errorf("get %s: %w", IndexFileName, err)
	}
	defer out.Body.Close()

	raw, err := io.ReadAll(out.Body)
	if err != nil {
		return nil, fmt.Errorf("read %s: %w", IndexFileName, err)
	}

	var idx Index
	if err := json.Unmarshal(raw, &idx); err != nil {
		return nil, fmt.Errorf("unmarshal %s: %w", IndexFileName, err)
	}

	wantLang := ""
	if i := strings.Index(prefix, "/"); i > 0 {
		wantLang = prefix[:i]
	}

	fullPrefix := c.prefix + prefix
	pages := make([]*Page, 0)

	switch idx.SchemaVersion {
	case 1:
		for catKey, cat := range *idx.Categories.(*map[string]*IndexV1Category) {
			lang, ok := strings.CutPrefix(catKey, c.prefix)
			if !ok {
				continue
			}
			if wantLang != "" && wantLang != lang {
				continue
			}
			for _, e := range cat.Pages {
				if !strings.HasPrefix(e.Link, fullPrefix) {
					continue
				}
				fileName := e.Link[strings.LastIndex(e.Link, "/")+1 : strings.LastIndex(e.Link, ".")]
				pages = append(pages, &Page{
					Link:         e.Link,
					FileName:     fileName,
					Lang:         lang,
					ModifiedTime: e.ModifiedTime,
					Metadata: &frontmatter.Metadata{
						Title:            e.Title,
						ShortDescription: e.ShortDescription,
						ActionDate:       e.ActionDate,
						PublishedTime:    e.PublishedTime,
						Thumbnail:        e.Thumbnail,
						Tags:             e.Tags,
						Geolocation:      e.Geolocation,
						Medley:           e.Medley,
						MedleyPart:       e.MedleyPart,
					},
				})
			}
		}
	case 2:
		for catKey, cat := range *idx.Categories.(*map[string]*IndexV2Category) {
			lang, ok := strings.CutPrefix(catKey, c.prefix)
			if !ok {
				continue
			}
			if wantLang != "" && wantLang != lang {
				continue
			}
			for codename, e := range cat.Pages {
				if !strings.HasPrefix(e.Link, fullPrefix) {
					continue
				}
				pages = append(pages, &Page{
					Link:         e.Link,
					FileName:     codename,
					Lang:         lang,
					ModifiedTime: e.ModifiedTime,
					Metadata: &frontmatter.Metadata{
						Title:            e.Title,
						ShortDescription: e.ShortDescription,
						ActionDate:       e.ActionDate,
						PublishedTime:    e.PublishedTime,
						Thumbnail:        e.Thumbnail,
						Tags:             e.Tags,
						Geolocation:      e.Geolocation,
						Medley:           e.Medley,
						MedleyPart:       e.MedleyPart,
					},
				})
			}
		}
	}

	medleys, _ := c.GetMedleys()
	for _, medley := range medleys {
		for locale, localname := range medley.Localnames {
			l10n.T.SetPath(localname, true, locale, "Medleys", medley.Codename)
		}
	}

	return pages, nil
}

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

func (c *S3Client) readAll(path string) ([]byte, error) {
	output, err := c.s3cl.GetObject(context.Background(), &s3.GetObjectInput{
		Bucket: aws.String(c.bucketName),
		Key:    aws.String(path),
	})
	if err != nil {
		return nil, fmt.Errorf("get S3 object: %w", err)
	}
	defer output.Body.Close()

	content, err := io.ReadAll(output.Body)
	if err != nil {
		return nil, fmt.Errorf("read S3 object body: %w", err)
	}

	return content, nil
}

func (c *S3Client) ReadFrontmatter(path string) (metadata *frontmatter.Metadata, markdown []byte, err error) {
	idxRaw, err := c.readAll(IndexFileName)
	if err != nil {
		return nil, nil, fmt.Errorf("read %s: %w", IndexFileName, err)
	}

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

	contentBytes, err := c.ReadAll(path)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to read file for frontmatter parsing: %w", err)
	}

	switch idx.SchemaVersion {
	case 1:
		return frontmatter.ParseFrontmatter(contentBytes)
	case 2:
		fullPath := c.prefix + path
		page := (*idx.Categories.(*map[string]*IndexV2Category))[fullPath[:strings.LastIndex(fullPath, "/")]].Pages[fullPath[strings.LastIndex(fullPath, "/")+1:strings.LastIndex(fullPath, ".")]]
		metadata = page.Metadata()

		if !bytes.HasPrefix(contentBytes, []byte("---\n")) {
			return metadata, contentBytes, nil
		}

		end := bytes.Index(contentBytes[4:], []byte("\n---\n"))
		if end == -1 {
			return metadata, contentBytes, nil
		}

		return metadata, contentBytes[end+9:], nil
	}

	return frontmatter.ParseFrontmatter(contentBytes)
}