From 684d0b6a57d2eb79730ade63baccdc6e59bebc29 Mon Sep 17 00:00:00 2001 From: Saya Andy Date: Sat, 1 Aug 2026 19:58:57 +0700 Subject: feat: repurpose metadata parser as article transcoder for saya.uz and telegram --- internal/storage/b2.go | 142 ++++++++++--------------------------------------- 1 file changed, 27 insertions(+), 115 deletions(-) (limited to 'internal/storage/b2.go') diff --git a/internal/storage/b2.go b/internal/storage/b2.go index b02261f..6697023 100644 --- a/internal/storage/b2.go +++ b/internal/storage/b2.go @@ -3,7 +3,6 @@ package storage import ( "context" "fmt" - "io" "strconv" "strings" "time" @@ -16,13 +15,12 @@ import ( var _ StorageClient = &B2StorageClient{} type B2StorageClient struct { - prefix string - bucket *b2.Bucket - b2cl *b2.Client - draftModeCfg *config.DraftModeConfig + prefix string + bucket *b2.Bucket + b2cl *b2.Client } -func NewB2StorageClient(cfg *config.StorageConfig, draftModeCfg *config.DraftModeConfig) (StorageClient, error) { +func NewB2StorageClient(cfg *config.StorageConfig) (StorageClient, error) { if cfg.Type != "b2" { return nil, fmt.Errorf("invalid storage type for B2InputClient") } @@ -38,73 +36,25 @@ func NewB2StorageClient(cfg *config.StorageConfig, draftModeCfg *config.DraftMod return nil, err } - draftModeCfgCopy := *draftModeCfg - - return &B2StorageClient{b2cl: b2cl, bucket: bucket, prefix: b2cfg.Prefix, draftModeCfg: &draftModeCfgCopy}, nil + 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() - if !strings.HasSuffix(name, ".md") { - continue - } - - if sc.draftModeCfg.Enabled && !strings.HasSuffix(name, sc.draftModeCfg.DraftSuffix) { - 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 { - draft := sc.bucket.Object(sc.prefix + path) - if draft == nil { - return fmt.Errorf("failed to reference draft object in B2 bucket") - } - draftAttrs, err := draft.Attrs(context.Background()) - if err != nil { - return fmt.Errorf("error getting attributes of a draft object: %w", err) - } - +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") @@ -131,32 +81,19 @@ func (sc *B2StorageClient) WriteMetadata(path string, metadata *frontmatter.Meta 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, - "metadata-last-update-sha1": draftAttrs.SHA1, + "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, }} - reader := draft.NewReader(context.Background()) - content := make([]byte, draftAttrs.Size) - if _, err = reader.Read(content); err != nil { - return fmt.Errorf("failed to read a draft object back for writing (required for attribute setting): %w", err) - } - - var prod *b2.Object - if sc.draftModeCfg.Enabled { - prodPath := strings.TrimSuffix(path, sc.draftModeCfg.DraftSuffix) + sc.draftModeCfg.ProdSuffix - prod = sc.bucket.Object(sc.prefix + prodPath) - if prod == nil { - return fmt.Errorf("failed to reference prod object in B2 bucket") - } - } else { - prod = draft + prod := sc.bucket.Object(sc.prefix + key) + if prod == nil { + return fmt.Errorf("failed to reference prod object in B2 bucket") } writer := prod.NewWriter(context.Background(), b2.WithAttrsOption(attrs)) @@ -168,31 +105,6 @@ func (sc *B2StorageClient) WriteMetadata(path string, metadata *frontmatter.Meta return nil } -func (sc *B2StorageClient) CompareDraftAndProd(path string) (changed bool) { - draft := sc.bucket.Object(sc.prefix + path) - if draft == nil { - return false - } - - prodPath := strings.TrimSuffix(path, sc.draftModeCfg.DraftSuffix) + sc.draftModeCfg.ProdSuffix - prod := sc.bucket.Object(sc.prefix + prodPath) - if prod == nil { - return true - } - - draftAttrs, err := draft.Attrs(context.Background()) - if err != nil { - return false - } - prodAttrs, err := prod.Attrs(context.Background()) - if err != nil { - return true - } - - lastUpdateSha1, ok := prodAttrs.Info["metadata-last-update-sha1"] - if !ok { - return true - } - - return draftAttrs.SHA1 != lastUpdateSha1 +func (sc *B2StorageClient) BuildIndex() error { + return fmt.Errorf("BuildIndex not implemented for B2 storage") } -- cgit v1.3.1+13