summaryrefslogtreecommitdiff
path: root/internal/blog/s3.go
blob: de4eb3a073aea53c307572ce2eaa99c9d6255aa9 (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
package blog

import (
	"context"
	"fmt"
	"io"
	"slices"
	"strings"
	"time"

	"github.com/SayaAndy/saya-today-web/config"
	"github.com/SayaAndy/saya-today-web/internal/frontmatter"
	"github.com/aws/aws-sdk-go-v2/aws"
	"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)

	s3cl := s3.New(s3.Options{
		Region:       s3cfg.Region,
		BaseEndpoint: aws.String(s3cfg.Endpoint),
		Credentials:  credentials.NewStaticCredentialsProvider(s3cfg.AccessKeyID, s3cfg.SecretAccessKey, ""),
	})

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

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

	fullPrefix := c.prefix + prefix
	input := &s3.ListObjectsV2Input{
		Bucket: aws.String(c.bucketName),
		Prefix: aws.String(fullPrefix),
	}

	paginator := s3.NewListObjectsV2Paginator(c.s3cl, input)

	for paginator.HasMorePages() {
		output, err := paginator.NextPage(context.Background())
		if err != nil {
			return nil, fmt.Errorf("list S3 objects: %w", err)
		}

		for _, obj := range output.Contents {
			key := aws.ToString(obj.Key)

			if !strings.HasSuffix(key, ".md") {
				continue
			}

			head, err := c.s3cl.HeadObject(context.Background(), &s3.HeadObjectInput{
				Bucket: aws.String(c.bucketName),
				Key:    aws.String(key),
			})
			if err != nil {
				return nil, fmt.Errorf("head S3 object %s: %w", key, err)
			}

			if head.ContentType == nil || !strings.Contains(*head.ContentType, "text/markdown") {
				continue
			}

			meta := head.Metadata
			if meta["title"] == "" {
				continue
			}

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

			linkParts := strings.Split(key, "/")
			nameParts := strings.Split(linkParts[len(linkParts)-1], ".")
			fileName := strings.Join(nameParts[:len(nameParts)-1], ".")

			tags := strings.Split(meta["tags"], ",")
			slices.Sort(tags)

			lang, _ := strings.CutPrefix(linkParts[0], c.prefix)

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

	return pages, nil
}

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(c.prefix + 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) {
	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)
}