summaryrefslogtreecommitdiff
path: root/config
diff options
from:
to:
context:
space:
mode:
authorGravatar SayaAndy <montferrat@tuta.io> 2025-07-15 12:43:28 +0700
committerGravatar SayaAndy <montferrat@tuta.io> 2025-07-15 12:43:28 +0700
commit373ec07b40178cb3b704aa83fc4fd53abfe279bc (patch)
treedcadacab21909195fbe660f105f060f4e6f686ce /config
parent6fef5c8ea06b0b25542afc65b412c75aa3d879a2 (diff)
downloadthumbnail-generator-373ec07b40178cb3b704aa83fc4fd53abfe279bc.tar.gz
thumbnail-generator-373ec07b40178cb3b704aa83fc4fd53abfe279bc.zip
feat: prepare all internal pkg (input/output client & processor)
Diffstat (limited to 'config')
-rw-r--r--config/config.go58
-rw-r--r--config/config.json22
2 files changed, 63 insertions, 17 deletions
diff --git a/config/config.go b/config/config.go
index 6d37904..5df29df 100644
--- a/config/config.go
+++ b/config/config.go
@@ -9,18 +9,14 @@ import (
)
type Config struct {
- Input InputConfig `json:"Input" validate:"required"`
- Output OutputConfig `json:"Output" validate:"required"`
+ Input InputConfig `json:"Input" validate:"required"`
+ Processor ProcessorConfig `json:"Processor" validate:"required"`
+ Output OutputConfig `json:"Output" validate:"required"`
}
type InputConfig struct {
- Storage StorageConfig `json:"Storage" validate:"required"`
-}
-
-type OutputConfig struct {
- Storage StorageConfig `json:"Storage" validate:"required"`
- Quality int `json:"Quality" validate:"required,min=1,max=100"`
- Size SizeConfig `json:"Size" validate:"required"`
+ Storage StorageConfig `json:"Storage" validate:"required"`
+ KnownExtensions []string `json:"KnownExtensions" validate:"required,min=0,dive,min=1"`
}
type StorageConfig struct {
@@ -46,13 +42,13 @@ func (sc *StorageConfig) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(tmp.Config, &b2Config); err != nil {
return fmt.Errorf("unmarshal B2Config: %w", err)
}
- sc.Config = b2Config
+ sc.Config = &b2Config
case "local":
var localConfig LocalConfig
if err := json.Unmarshal(tmp.Config, &localConfig); err != nil {
return fmt.Errorf("unmarshal LocalConfig: %w", err)
}
- sc.Config = localConfig
+ sc.Config = &localConfig
default:
return fmt.Errorf("unsupported storage type: %s", tmp.Type)
}
@@ -72,11 +68,51 @@ type LocalConfig struct {
Path string `json:"Path" validate:"required,min=1"`
}
+type ProcessorConfig struct {
+ Type string `json:"Type" validate:"required,oneof=webp"`
+ Config any `json:"Config" validate:"required"`
+}
+
+func (pc *ProcessorConfig) UnmarshalJSON(data []byte) error {
+ var tmp struct {
+ Type string `json:"Type"`
+ Config json.RawMessage `json:"Config"`
+ }
+
+ if err := json.Unmarshal(data, &tmp); err != nil {
+ return err
+ }
+
+ pc.Type = tmp.Type
+
+ switch tmp.Type {
+ case "webp":
+ var webpConfig WebpConfig
+ if err := json.Unmarshal(tmp.Config, &webpConfig); err != nil {
+ return fmt.Errorf("unmarshal WebpConfig: %w", err)
+ }
+ pc.Config = &webpConfig
+ default:
+ return fmt.Errorf("unsupported storage type: %s", tmp.Type)
+ }
+
+ return nil
+}
+
+type WebpConfig struct {
+ Quality int `json:"Quality" validate:"required,min=1,max=100"`
+ Size SizeConfig `json:"Size" validate:"required"`
+}
+
type SizeConfig struct {
MaxWidth int `json:"MaxWidth" validate:"required,min=0"`
MaxHeight int `json:"MaxHeight" validate:"required,min=0"`
}
+type OutputConfig struct {
+ Storage StorageConfig `json:"Storage" validate:"required"`
+}
+
func LoadConfig(path string, config *Config) error {
fileBytes, err := os.ReadFile(path)
if err != nil {
diff --git a/config/config.json b/config/config.json
index d818107..298a03f 100644
--- a/config/config.json
+++ b/config/config.json
@@ -9,6 +9,21 @@
"KeyID": "${B2_KEY_ID}",
"ApplicationKey": "${B2_APPLICATION_KEY}"
}
+ },
+ "KnownExtensions": [
+ "jpg",
+ "jpeg",
+ "png"
+ ]
+ },
+ "Processor": {
+ "Type": "webp",
+ "Config": {
+ "Quality": 80,
+ "Size": {
+ "MaxWidth": 1280,
+ "MaxHeight": 0
+ }
}
},
"Output": {
@@ -21,11 +36,6 @@
"KeyID": "${B2_KEY_ID}",
"ApplicationKey": "${B2_APPLICATION_KEY}"
}
- },
- "Quality": 80,
- "Size": {
- "MaxWidth": 1280,
- "MaxHeight": 0
}
}
-} \ No newline at end of file
+}