summaryrefslogtreecommitdiff
path: root/internal/storage/b2.go
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
Diffstat (limited to 'internal/storage/b2.go')
-rw-r--r--internal/storage/b2.go134
1 files changed, 52 insertions, 82 deletions
diff --git a/internal/storage/b2.go b/internal/storage/b2.go
index 168690a..6697023 100644
--- a/internal/storage/b2.go
+++ b/internal/storage/b2.go
@@ -3,7 +3,7 @@ package storage
import (
"context"
"fmt"
- "io"
+ "strconv"
"strings"
"time"
@@ -39,102 +39,72 @@ func NewB2StorageClient(cfg *config.StorageConfig) (StorageClient, error) {
return &B2StorageClient{b2cl: b2cl, bucket: bucket, prefix: b2cfg.Prefix}, nil
}
-func (sc *B2StorageClient) Scan() ([]string, error) {
- filePaths := []string{}
-
- iter := sc.bucket.List(context.Background(), b2.ListPrefix(sc.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
- }
-
- name := obj.Name()
-
- nameParts := strings.Split(name, ".")
- if len(nameParts) < 2 {
- continue
- }
-
- ext := strings.ToLower(nameParts[len(nameParts)-1])
- if ext != "md" {
- continue
- }
-
- filePaths = append(filePaths, strings.TrimPrefix(name, sc.prefix))
- }
-
- if err := iter.Err(); err != nil {
- return nil, fmt.Errorf("iterate over B2 objects: %w", err)
- }
-
- return filePaths, nil
-}
-
-func (sc *B2StorageClient) GetReader(path string) (io.ReadCloser, int64, error) {
- obj := sc.bucket.Object(sc.prefix + path)
+func (sc *B2StorageClient) GetMetadata(key string) (map[string]string, error) {
+ obj := sc.bucket.Object(sc.prefix + key)
if obj == nil {
- return nil, 0, fmt.Errorf("failed to reference object in B2 bucket")
+ return nil, fmt.Errorf("failed to reference object in B2 bucket")
}
attrs, err := obj.Attrs(context.Background())
if err != nil {
- return nil, 0, fmt.Errorf("error getting attributes of an object: %w", err)
+ if b2.IsNotExist(err) {
+ return map[string]string{}, nil
+ }
+ return nil, fmt.Errorf("get attributes of B2 object %q: %w", sc.prefix+key, err)
}
-
- return obj.NewReader(context.Background()), attrs.Size, nil
+ return attrs.Info, nil
}
-func (sc *B2StorageClient) WriteMetadata(path string, metadata *frontmatter.Metadata) error {
- obj := sc.bucket.Object(sc.prefix + path)
- if obj == nil {
- return fmt.Errorf("failed to reference object in B2 bucket")
+func (sc *B2StorageClient) Put(key string, content []byte, metadata *frontmatter.Metadata) error {
+ geolocationParts := strings.Split(metadata.Geolocation, " ")
+ if (len(geolocationParts) == 1 && geolocationParts[0] != "") || len(geolocationParts) >= 4 {
+ return fmt.Errorf("invalid geolocation format, expecting '{x} {y} [areaError]' or an empty string")
}
- oldAttrs, err := obj.Attrs(context.Background())
- if err != nil {
- return fmt.Errorf("error getting attributes of an object: %w", err)
+ if len(geolocationParts) >= 2 {
+ if _, err := strconv.ParseFloat(geolocationParts[0], 64); err != nil {
+ return fmt.Errorf("invalid geolocation parameter, expected float for X: %w", err)
+ }
+ if _, err := strconv.ParseFloat(geolocationParts[1], 64); err != nil {
+ return fmt.Errorf("invalid geolocation parameter, expected float for Y: %w", err)
+ }
+ }
+ if len(geolocationParts) == 3 {
+ if _, err := strconv.ParseFloat(geolocationParts[2], 64); err != nil {
+ return fmt.Errorf("invalid geolocation parameter, expected float for area error: %w", err)
+ }
}
- attrs := &b2.Attrs{Info: map[string]string{
- "title": metadata.Title,
- "short-description": metadata.ShortDescription,
- "action-date": metadata.ActionDate,
- "published-time": metadata.PublishedTime.Format(time.RFC3339),
- "thumbnail": metadata.Thumbnail,
- "tags": strings.Join(metadata.Tags, ","),
- "metadata-last-update-sha1": oldAttrs.SHA1,
- }}
-
- writer := obj.NewWriter(context.Background(), b2.WithAttrsOption(attrs))
- writer.Close()
+ medley := ""
+ if metadata.Medley != "" {
+ medley = fmt.Sprintf("%s %d", metadata.Medley, metadata.MedleyPart)
+ }
- return nil
-}
+ attrs := &b2.Attrs{
+ ContentType: "text/markdown; charset=utf-8",
+ Info: map[string]string{
+ "title": metadata.Title,
+ "short-description": metadata.ShortDescription,
+ "action-date": metadata.ActionDate,
+ "published-time": metadata.PublishedTime.Format(time.RFC3339),
+ "thumbnail": metadata.Thumbnail,
+ "tags": strings.Join(metadata.Tags, ","),
+ "geolocation": metadata.Geolocation,
+ "medley": medley,
+ }}
-func (sc *B2StorageClient) FileHasChanged(path string) bool {
- obj := sc.bucket.Object(sc.prefix + path)
- if obj == nil {
- return true
+ prod := sc.bucket.Object(sc.prefix + key)
+ if prod == nil {
+ return fmt.Errorf("failed to reference prod object in B2 bucket")
}
- attrs, err := obj.Attrs(context.Background())
- if err != nil {
- return true
+ writer := prod.NewWriter(context.Background(), b2.WithAttrsOption(attrs))
+ defer writer.Close()
+ if _, err := writer.Write(content); err != nil {
+ return fmt.Errorf("failed to write an object back after attribute settings: %w", err)
}
- lastUpdateSha1, ok := attrs.Info["metadata-last-update-sha1"]
- if !ok {
- return true
- }
+ return nil
+}
- return attrs.SHA1 == lastUpdateSha1
+func (sc *B2StorageClient) BuildIndex() error {
+ return fmt.Errorf("BuildIndex not implemented for B2 storage")
}