summaryrefslogtreecommitdiff
path: root/internal
diff options
from:
to:
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/client/input/b2.go44
-rw-r--r--internal/client/input/input_client_interface.go15
-rw-r--r--internal/client/output/b2.go49
-rw-r--r--internal/client/output/output_client_interface.go20
-rw-r--r--internal/processor/processor_interface.go2
-rw-r--r--internal/processor/webp.go22
6 files changed, 101 insertions, 51 deletions
diff --git a/internal/client/input/b2.go b/internal/client/input/b2.go
index 67fb14e..54a6b09 100644
--- a/internal/client/input/b2.go
+++ b/internal/client/input/b2.go
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"slices"
+ "strconv"
"strings"
"github.com/Backblaze/blazer/b2"
@@ -72,7 +73,7 @@ func (c *B2InputClient) Scan() ([]string, error) {
}
}
- filePaths = append(filePaths, name)
+ filePaths = append(filePaths, strings.TrimPrefix(name, c.prefix))
}
if err := iter.Err(); err != nil {
@@ -82,8 +83,8 @@ func (c *B2InputClient) Scan() ([]string, error) {
return filePaths, nil
}
-func (c *B2InputClient) ReadMetadata(path string) (map[string]string, error) {
- obj := c.bucket.Object(path)
+func (c *B2InputClient) ReadMetadata(path string) (*MetadataStruct, error) {
+ obj := c.bucket.Object(c.prefix + path)
if obj == nil {
return nil, fmt.Errorf("object not found in B2 bucket")
}
@@ -93,37 +94,38 @@ func (c *B2InputClient) ReadMetadata(path string) (map[string]string, error) {
return nil, fmt.Errorf("get attributes for object: %w", err)
}
- metadata := attrs.Info
- metadata["Name"] = attrs.Name
- metadata["Size"] = fmt.Sprintf("%d", attrs.Size)
- metadata["ContentType"] = attrs.ContentType
- metadata["LastModified"] = attrs.LastModified.Format("2006-01-02T15:04:05Z")
- metadata["SHA1"] = attrs.SHA1
- metadata["UploadTimestamp"] = attrs.UploadTimestamp.Format("2006-01-02T15:04:05Z")
+ metadata := MetadataStruct{
+ Name: attrs.Name,
+ StorageType: "b2",
+ Hash: attrs.SHA1,
+ ContentType: attrs.ContentType,
+ FirstCreated: attrs.UploadTimestamp,
+ LastModified: attrs.LastModified,
+ Misc: attrs.Info,
+ }
+ metadata.Misc["Size"] = strconv.FormatInt(attrs.Size, 10)
switch attrs.Status {
case b2.Uploaded:
- metadata["Status"] = "Uploaded"
+ metadata.Misc["b2-status"] = "Uploaded"
case b2.Folder:
- metadata["Status"] = "Folder"
+ metadata.Misc["b2-status"] = "Folder"
case b2.Hider:
- metadata["Status"] = "Hider"
+ metadata.Misc["b2-status"] = "Hider"
case b2.Started:
- metadata["Status"] = "Started"
+ metadata.Misc["b2-status"] = "Started"
default:
- metadata["Status"] = "Unknown"
+ metadata.Misc["b2-status"] = "Unknown"
}
- return metadata, nil
+ return &metadata, nil
}
-func (c *B2InputClient) GetReader(path string) (io.Reader, error) {
- obj := c.bucket.Object(path)
+func (c *B2InputClient) GetReader(path string) (io.ReadCloser, error) {
+ obj := c.bucket.Object(c.prefix + path)
if obj == nil {
return nil, fmt.Errorf("failed to reference object in B2 bucket")
}
- reader := obj.NewReader(context.Background())
-
- return reader, nil
+ return obj.NewReader(context.Background()), nil
}
diff --git a/internal/client/input/input_client_interface.go b/internal/client/input/input_client_interface.go
index 4bbbae5..c896ca4 100644
--- a/internal/client/input/input_client_interface.go
+++ b/internal/client/input/input_client_interface.go
@@ -2,10 +2,21 @@ package input
import (
"io"
+ "time"
)
type InputClient interface {
Scan() ([]string, error)
- ReadMetadata(string) (map[string]string, error)
- GetReader(string) (io.Reader, error)
+ ReadMetadata(string) (*MetadataStruct, error)
+ GetReader(string) (io.ReadCloser, error)
+}
+
+type MetadataStruct struct {
+ Name string
+ StorageType string
+ Hash string
+ ContentType string
+ FirstCreated time.Time
+ LastModified time.Time
+ Misc map[string]string
}
diff --git a/internal/client/output/b2.go b/internal/client/output/b2.go
index 3c8b53c..6979e5b 100644
--- a/internal/client/output/b2.go
+++ b/internal/client/output/b2.go
@@ -4,9 +4,11 @@ import (
"context"
"fmt"
"io"
+ "strconv"
"github.com/Backblaze/blazer/b2"
"github.com/SayaAndy/saya-today-thumbnail-generator/config"
+ "github.com/SayaAndy/saya-today-thumbnail-generator/internal/client/input"
)
var _ OutputClient = (*B2OutputClient)(nil)
@@ -36,16 +38,19 @@ func NewB2OutputClient(cfg *config.OutputConfig) (*B2OutputClient, error) {
return &B2OutputClient{b2cl: b2cl, bucket: bucket, prefix: b2cfg.Prefix}, nil
}
-func (c *B2OutputClient) GetWriter(path string) (io.Writer, error) {
+func (c *B2OutputClient) GetWriter(path string, inputMetadata *input.MetadataStruct) (io.WriteCloser, error) {
obj := c.bucket.Object(c.prefix + path)
if obj == nil {
return nil, fmt.Errorf("failed to reference object in B2 bucket")
}
- return obj.NewWriter(context.Background()), nil
+ attrs := &b2.Attrs{Info: make(map[string]string)}
+ attrs.Info["sha1-original"] = inputMetadata.Hash
+
+ return obj.NewWriter(context.Background(), b2.WithAttrsOption(attrs)), nil
}
-func (c *B2OutputClient) ReadMetadata(path string) (map[string]string, error) {
+func (c *B2OutputClient) ReadMetadata(path string) (*MetadataStruct, error) {
obj := c.bucket.Object(c.prefix + path)
if obj == nil {
return nil, fmt.Errorf("failed to reference object in B2 bucket")
@@ -56,40 +61,44 @@ func (c *B2OutputClient) ReadMetadata(path string) (map[string]string, error) {
return nil, fmt.Errorf("get attributes for object: %w", err)
}
- metadata := attrs.Info
- metadata["Name"] = attrs.Name
- metadata["Size"] = fmt.Sprintf("%d", attrs.Size)
- metadata["ContentType"] = attrs.ContentType
- metadata["LastModified"] = attrs.LastModified.Format("2006-01-02T15:04:05Z")
- metadata["SHA1"] = attrs.SHA1
- metadata["UploadTimestamp"] = attrs.UploadTimestamp.Format("2006-01-02T15:04:05Z")
+ metadata := MetadataStruct{
+ Name: attrs.Name,
+ StorageType: "b2",
+ Hash: attrs.SHA1,
+ HashOriginal: attrs.Info["sha1-original"],
+ ContentType: attrs.ContentType,
+ FirstCreated: attrs.UploadTimestamp,
+ LastModified: attrs.LastModified,
+ Misc: attrs.Info,
+ }
+ metadata.Misc["Size"] = strconv.FormatInt(attrs.Size, 10)
switch attrs.Status {
case b2.Uploaded:
- metadata["Status"] = "Uploaded"
+ metadata.Misc["b2-status"] = "Uploaded"
case b2.Folder:
- metadata["Status"] = "Folder"
+ metadata.Misc["b2-status"] = "Folder"
case b2.Hider:
- metadata["Status"] = "Hider"
+ metadata.Misc["b2-status"] = "Hider"
case b2.Started:
- metadata["Status"] = "Started"
+ metadata.Misc["b2-status"] = "Started"
default:
- metadata["Status"] = "Unknown"
+ metadata.Misc["b2-status"] = "Unknown"
}
- return metadata, nil
+ return &metadata, nil
}
-func (c *B2OutputClient) IsMissing(path string) (bool, error) {
+func (c *B2OutputClient) IsMissing(path string) bool {
obj := c.bucket.Object(c.prefix + path)
if obj == nil {
- return false, fmt.Errorf("failed to reference object in B2 bucket")
+ return true
}
attrs, err := obj.Attrs(context.Background())
if err != nil {
- return true, fmt.Errorf("get attributes for object: %w", err)
+ return true
}
- return attrs.Status == b2.Hider, nil
+ return attrs.Status == b2.Hider
}
diff --git a/internal/client/output/output_client_interface.go b/internal/client/output/output_client_interface.go
index 5271e18..4ec4728 100644
--- a/internal/client/output/output_client_interface.go
+++ b/internal/client/output/output_client_interface.go
@@ -2,10 +2,24 @@ package output
import (
"io"
+ "time"
+
+ "github.com/SayaAndy/saya-today-thumbnail-generator/internal/client/input"
)
type OutputClient interface {
- GetWriter(path string) (io.Writer, error)
- ReadMetadata(string) (map[string]string, error)
- IsMissing(path string) (bool, error)
+ GetWriter(path string, inputMetadata *input.MetadataStruct) (io.WriteCloser, error)
+ ReadMetadata(string) (*MetadataStruct, error)
+ IsMissing(path string) bool
+}
+
+type MetadataStruct struct {
+ Name string
+ StorageType string
+ Hash string
+ HashOriginal string
+ ContentType string
+ FirstCreated time.Time
+ LastModified time.Time
+ Misc map[string]string
}
diff --git a/internal/processor/processor_interface.go b/internal/processor/processor_interface.go
index 8a8f803..7cf20f2 100644
--- a/internal/processor/processor_interface.go
+++ b/internal/processor/processor_interface.go
@@ -4,5 +4,5 @@ import "io"
type Processor interface {
DeductOutputPath(inputPath string) string
- Process(ext string, reader io.Reader, writer io.Writer) error
+ Process(ext string, reader io.ReadCloser, writer io.WriteCloser) error
}
diff --git a/internal/processor/webp.go b/internal/processor/webp.go
index 984aab1..b5e089d 100644
--- a/internal/processor/webp.go
+++ b/internal/processor/webp.go
@@ -6,6 +6,7 @@ import (
"image/jpeg"
"image/png"
"io"
+ "log/slog"
"strings"
"golang.org/x/image/draw"
@@ -41,10 +42,13 @@ func (p *WebpProcessor) DeductOutputPath(inputPath string) string {
return strings.Join(pathParts, ".")
}
-func (p *WebpProcessor) Process(contentType string, reader io.Reader, writer io.Writer) error {
+func (p *WebpProcessor) Process(contentType string, reader io.ReadCloser, writer io.WriteCloser) error {
var src image.Image
var err error
+ defer reader.Close()
+ defer writer.Close()
+
switch contentType {
case "image/jpeg":
src, err = jpeg.Decode(reader)
@@ -56,6 +60,8 @@ func (p *WebpProcessor) Process(contentType string, reader io.Reader, writer io.
if err != nil {
return fmt.Errorf("decode png: %w", err)
}
+ default:
+ return fmt.Errorf("unsupported content type: %s", contentType)
}
opts, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, float32(p.quality))
@@ -63,8 +69,16 @@ func (p *WebpProcessor) Process(contentType string, reader io.Reader, writer io.
return fmt.Errorf("create webp encoder options: %w", err)
}
- xCoef := p.maxWidth / src.Bounds().Max.X
- yCoef := p.maxHeight / src.Bounds().Max.Y
+ xCoef := float64(p.maxWidth) / float64(src.Bounds().Max.X)
+ if p.maxWidth == 0 {
+ xCoef = 1
+ }
+ yCoef := float64(p.maxHeight) / float64(src.Bounds().Max.Y)
+ if p.maxHeight == 0 {
+ yCoef = 1
+ }
+ slog.Debug("calculated coefficients", slog.Float64("x_coef", xCoef), slog.Float64("y_coef", yCoef))
+
if xCoef > 1 && yCoef > 1 {
return webp.Encode(writer, src, opts)
}
@@ -74,7 +88,7 @@ func (p *WebpProcessor) Process(contentType string, reader io.Reader, writer io.
minCoef = yCoef
}
- dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X*xCoef, src.Bounds().Max.Y*xCoef))
+ dst := image.NewRGBA(image.Rect(0, 0, int(float64(src.Bounds().Max.X)*minCoef), int(float64(src.Bounds().Max.Y)*minCoef)))
draw.CatmullRom.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
return webp.Encode(writer, dst, opts)