summaryrefslogtreecommitdiff
diff options
from:
to:
context:
space:
mode:
-rw-r--r--config/config.go107
-rw-r--r--config/config.json31
-rw-r--r--go.mod16
-rw-r--r--go.sum26
-rw-r--r--main.go20
5 files changed, 200 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go
new file mode 100644
index 0000000..6d37904
--- /dev/null
+++ b/config/config.go
@@ -0,0 +1,107 @@
+package config
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+
+ "github.com/go-playground/validator/v10"
+)
+
+type Config struct {
+ Input InputConfig `json:"Input" 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"`
+}
+
+type StorageConfig struct {
+ Type string `json:"Type" validate:"required,oneof=b2 local"`
+ Config any `json:"Config" validate:"required"`
+}
+
+func (sc *StorageConfig) 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
+ }
+
+ sc.Type = tmp.Type
+
+ switch tmp.Type {
+ case "b2":
+ var b2Config B2Config
+ if err := json.Unmarshal(tmp.Config, &b2Config); err != nil {
+ return fmt.Errorf("unmarshal B2Config: %w", err)
+ }
+ 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
+ default:
+ return fmt.Errorf("unsupported storage type: %s", tmp.Type)
+ }
+
+ return nil
+}
+
+type B2Config struct {
+ BucketName string `json:"BucketName" validate:"required,min=1"`
+ Region string `json:"Region" validate:"required,min=1"`
+ Prefix string `json:"Prefix"`
+ KeyID string `json:"KeyID"`
+ ApplicationKey string `json:"ApplicationKey"`
+}
+
+type LocalConfig struct {
+ Path string `json:"Path" validate:"required,min=1"`
+}
+
+type SizeConfig struct {
+ MaxWidth int `json:"MaxWidth" validate:"required,min=0"`
+ MaxHeight int `json:"MaxHeight" validate:"required,min=0"`
+}
+
+func LoadConfig(path string, config *Config) error {
+ fileBytes, err := os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+
+ expandedFileBytes := []byte(os.ExpandEnv(string(fileBytes)))
+
+ if err = json.Unmarshal(expandedFileBytes, config); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func InitConfig(path string) (*Config, error) {
+ config := &Config{}
+ if err := LoadConfig(path, config); err != nil {
+ return nil, err
+ }
+
+ validate := validator.New(validator.WithRequiredStructEnabled())
+ if err := validate.Struct(config); err != nil {
+ return nil, err
+ }
+
+ return config, nil
+}
diff --git a/config/config.json b/config/config.json
new file mode 100644
index 0000000..d818107
--- /dev/null
+++ b/config/config.json
@@ -0,0 +1,31 @@
+{
+ "Input": {
+ "Storage": {
+ "Type": "b2",
+ "Config": {
+ "BucketName": "sayana-photos",
+ "Region": "eu-central-003",
+ "Prefix": "full/",
+ "KeyID": "${B2_KEY_ID}",
+ "ApplicationKey": "${B2_APPLICATION_KEY}"
+ }
+ }
+ },
+ "Output": {
+ "Storage": {
+ "Type": "b2",
+ "Config": {
+ "BucketName": "sayana-photos",
+ "Region": "eu-central-003",
+ "Prefix": "thumbnails/",
+ "KeyID": "${B2_KEY_ID}",
+ "ApplicationKey": "${B2_APPLICATION_KEY}"
+ }
+ },
+ "Quality": 80,
+ "Size": {
+ "MaxWidth": 1280,
+ "MaxHeight": 0
+ }
+ }
+} \ No newline at end of file
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..5a5a067
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,16 @@
+module github.com/SayaAndy/saya-today-thumbnail-generator
+
+go 1.24.4
+
+require (
+ github.com/gabriel-vasile/mimetype v1.4.8 // indirect
+ github.com/go-playground/locales v0.14.1 // indirect
+ github.com/go-playground/universal-translator v0.18.1 // indirect
+ github.com/go-playground/validator/v10 v10.27.0 // indirect
+ github.com/kolesa-team/go-webp v1.0.5 // indirect
+ github.com/leodido/go-urn v1.4.0 // indirect
+ golang.org/x/crypto v0.33.0 // indirect
+ golang.org/x/net v0.34.0 // indirect
+ golang.org/x/sys v0.30.0 // indirect
+ golang.org/x/text v0.22.0 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..5276298
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,26 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
+github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
+github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
+github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
+github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
+github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
+github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
+github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
+github.com/kolesa-team/go-webp v1.0.5 h1:GZQHJBaE8dsNKZltfwqsL0qVJ7vqHXsfA+4AHrQW3pE=
+github.com/kolesa-team/go-webp v1.0.5/go.mod h1:QmJu0YHXT3ex+4SgUvs+a+1SFCDcCqyZg+LbIuNNTnE=
+github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
+github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
+golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
+golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
+golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
+golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
+golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
+golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..f03ff52
--- /dev/null
+++ b/main.go
@@ -0,0 +1,20 @@
+package main
+
+import (
+ "flag"
+
+ "github.com/SayaAndy/saya-today-thumbnail-generator/config"
+)
+
+var (
+ configPath = flag.String("c", "config.json", "Path to the configuration file")
+)
+
+func main() {
+ flag.Parse()
+
+ cfg := &config.Config{}
+ if err := config.LoadConfig(*configPath, cfg); err != nil {
+ panic(err)
+ }
+}