summaryrefslogtreecommitdiffci
path: root/internal/blog
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
Diffstat (limited to 'internal/blog')
-rw-r--r--internal/blog/b2.go21
-rw-r--r--internal/blog/client.go1
-rw-r--r--internal/blog/index.go83
-rw-r--r--internal/blog/s3.go149
4 files changed, 35 insertions, 219 deletions
diff --git a/internal/blog/b2.go b/internal/blog/b2.go
index f2ac890..8a4d363 100644
--- a/internal/blog/b2.go
+++ b/internal/blog/b2.go
@@ -2,7 +2,6 @@ package blog
import (
"context"
- "encoding/json"
"fmt"
"slices"
"strings"
@@ -38,20 +37,6 @@ func NewB2Client(cfg *config.StorageConfig) (Client, error) {
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{}
@@ -117,11 +102,7 @@ func (c *B2Client) Scan(prefix string) ([]*Page, error) {
}
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)
+ obj := c.bucket.Object(c.prefix + path)
if obj == nil {
return nil, fmt.Errorf("failed to reference object in B2 bucket")
}
diff --git a/internal/blog/client.go b/internal/blog/client.go
index 53eb0fd..56bbee4 100644
--- a/internal/blog/client.go
+++ b/internal/blog/client.go
@@ -17,7 +17,6 @@ type Page struct {
type Client interface {
Scan(prefix string) ([]*Page, error)
- GetMedleys() ([]MedleyEntry, error)
ReadAll(path string) ([]byte, error)
ReadFrontmatter(path string) (metadata *frontmatter.Metadata, markdown []byte, err error)
}
diff --git a/internal/blog/index.go b/internal/blog/index.go
index a09b59e..861a90e 100644
--- a/internal/blog/index.go
+++ b/internal/blog/index.go
@@ -1,17 +1,10 @@
package blog
-import (
- "encoding/json"
- "fmt"
- "time"
-
- "github.com/SayaAndy/saya-today-web/internal/frontmatter"
-)
+import "time"
const IndexFileName = "index.json"
-const MedleysIndexFileName = "medleys.json"
-const IndexSchemaVersion = 2
+const IndexSchemaVersion = 1
type IndexEntry struct {
Link string `json:"link"`
@@ -27,77 +20,13 @@ type IndexEntry struct {
MedleyPart int `json:"medleyPart,omitempty"`
}
-func (e IndexEntry) Metadata() *frontmatter.Metadata {
- return &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,
- }
-}
-
-type IndexV2Category struct {
- GeneratedAt time.Time `json:"generatedAt"`
- Pages map[string]IndexEntry `json:"pages"`
-}
-
-type IndexV1Category struct {
+type IndexCategory struct {
GeneratedAt time.Time `json:"generatedAt"`
Pages []IndexEntry `json:"pages"`
}
type Index struct {
- SchemaVersion int `json:"schemaVersion"`
- GeneratedAt time.Time `json:"generatedAt"`
- Categories any `json:"categories"`
-}
-
-func (idx *Index) UnmarshalJSON(data []byte) error {
- var tmp struct {
- SchemaVersion int `json:"schemaVersion"`
- GeneratedAt time.Time `json:"generatedAt"`
- Categories json.RawMessage `json:"categories"`
- }
-
- if err := json.Unmarshal(data, &tmp); err != nil {
- return err
- }
-
- idx.SchemaVersion = tmp.SchemaVersion
- idx.GeneratedAt = tmp.GeneratedAt
-
- switch tmp.SchemaVersion {
- case 1:
- var categories map[string]*IndexV1Category
- if err := json.Unmarshal(tmp.Categories, &categories); err != nil {
- return fmt.Errorf("unmarshal map[string]*IndexV1Category: %w", err)
- }
- idx.Categories = &categories
- case 2:
- var categories map[string]*IndexV2Category
- if err := json.Unmarshal(tmp.Categories, &categories); err != nil {
- return fmt.Errorf("unmarshal map[string]*IndexV2Category: %w", err)
- }
- idx.Categories = &categories
- default:
- return fmt.Errorf("unsupported index version: %d", tmp.SchemaVersion)
- }
-
- return nil
-}
-
-type MedleyEntry struct {
- Codename string `json:"codename"`
- Localnames map[string]string `json:"localnames"`
- Content []string `json:"content"`
-}
-
-type MedleyPageEntry struct {
- Codename string `json:"codename"`
- Position int `json:"position"`
+ SchemaVersion int `json:"schemaVersion"`
+ GeneratedAt time.Time `json:"generatedAt"`
+ Categories map[string]IndexCategory `json:"categories"`
}
diff --git a/internal/blog/s3.go b/internal/blog/s3.go
index 3c1a932..5ee896a 100644
--- a/internal/blog/s3.go
+++ b/internal/blog/s3.go
@@ -1,7 +1,6 @@
package blog
import (
- "bytes"
"context"
"encoding/json"
"fmt"
@@ -10,13 +9,14 @@ import (
"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"
)
+const s3ScanConcurrency = 32
+
type S3Client struct {
prefix string
bucketName string
@@ -59,20 +59,6 @@ func NewS3Client(cfg *config.StorageConfig) (Client, error) {
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),
@@ -100,79 +86,34 @@ func (c *S3Client) Scan(prefix string) ([]*Page, error) {
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,
- },
- })
- }
+ for catKey, cat := range idx.Categories {
+ lang, ok := strings.CutPrefix(catKey, c.prefix)
+ if !ok {
+ continue
}
- 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 {
+ if wantLang != "" && wantLang != lang {
+ continue
+ }
+ for _, e := range cat.Pages {
+ if !strings.HasPrefix(e.Link, fullPrefix) {
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)
+ 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,
+ },
+ })
}
}
@@ -180,13 +121,9 @@ func (c *S3Client) Scan(prefix string) ([]*Page, error) {
}
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),
+ Key: aws.String(c.prefix + path),
})
if err != nil {
return nil, fmt.Errorf("get S3 object: %w", err)
@@ -202,40 +139,10 @@ func (c *S3Client) readAll(path string) ([]byte, error) {
}
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)
}