From f968d443dc5c6e1f5140922313b0d6e8019fc269 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Wed, 16 Jul 2025 17:49:03 +0700 Subject: feat: implement local-unix clients --- config/config-b2.sample.json | 44 +++++++++++++++++++++ config/config-local-unix.sample.json | 40 +++++++++++++++++++ config/config.go | 77 ++++++++++++++++++++++++++++-------- config/config.json | 44 --------------------- 4 files changed, 145 insertions(+), 60 deletions(-) create mode 100644 config/config-b2.sample.json create mode 100644 config/config-local-unix.sample.json delete mode 100644 config/config.json (limited to 'config') diff --git a/config/config-b2.sample.json b/config/config-b2.sample.json new file mode 100644 index 0000000..d3d772e --- /dev/null +++ b/config/config-b2.sample.json @@ -0,0 +1,44 @@ +{ + "ForceRewrite": false, + "MaxConcurrentJobs": 4, + "LogLevel": "info", + "Input": { + "Storage": { + "Type": "b2", + "Config": { + "BucketName": "sayana-photos", + "Region": "eu-central-003", + "Prefix": "full/", + "KeyID": "${B2_KEY_ID}", + "ApplicationKey": "${B2_APPLICATION_KEY}" + } + }, + "KnownExtensions": [ + "jpg", + "jpeg", + "png" + ] + }, + "Converter": { + "Type": "webp", + "Config": { + "Quality": 80, + "Size": { + "MaxWidth": 800, + "MaxHeight": 0 + } + } + }, + "Output": { + "Storage": { + "Type": "b2", + "Config": { + "BucketName": "sayana-photos", + "Region": "eu-central-003", + "Prefix": "thumbnails/", + "KeyID": "${B2_KEY_ID}", + "ApplicationKey": "${B2_APPLICATION_KEY}" + } + } + } +} \ No newline at end of file diff --git a/config/config-local-unix.sample.json b/config/config-local-unix.sample.json new file mode 100644 index 0000000..637a05a --- /dev/null +++ b/config/config-local-unix.sample.json @@ -0,0 +1,40 @@ +{ + "ForceRewrite": false, + "MaxConcurrentJobs": 4, + "LogLevel": "debug", + "Input": { + "Storage": { + "Type": "local-unix", + "Config": { + "MaxDepth": 3, + "Path": "/tmp/thumbnailing/full/" + } + }, + "KnownExtensions": [ + "jpg", + "jpeg", + "png" + ] + }, + "Converter": { + "Type": "webp", + "Config": { + "Quality": 80, + "Size": { + "MaxWidth": 800, + "MaxHeight": 0 + } + } + }, + "Output": { + "Storage": { + "Type": "local-unix", + "Config": { + "Path": "/tmp/thumbnailing/thumbnails/", + "DirPermissionMode": "0755", + "FilePermissionMode": "0644", + "AttributesImplementation": "xattr" + } + } + } +} \ No newline at end of file diff --git a/config/config.go b/config/config.go index 3ca8f84..d2b7072 100644 --- a/config/config.go +++ b/config/config.go @@ -19,16 +19,16 @@ type Config struct { } type InputConfig struct { - Storage StorageConfig `json:"Storage" validate:"required"` - KnownExtensions []string `json:"KnownExtensions" validate:"required,min=0,dive,min=1"` + Storage InputStorageConfig `json:"Storage" validate:"required"` + KnownExtensions []string `json:"KnownExtensions" validate:"required,min=0,dive,min=1"` } -type StorageConfig struct { - Type string `json:"Type" validate:"required,oneof=b2 local"` +type InputStorageConfig struct { + Type string `json:"Type" validate:"required,oneof=b2 local-unix"` Config any `json:"Config" validate:"required"` } -func (sc *StorageConfig) UnmarshalJSON(data []byte) error { +func (sc *InputStorageConfig) UnmarshalJSON(data []byte) error { var tmp struct { Type string `json:"Type"` Config json.RawMessage `json:"Config"` @@ -47,12 +47,49 @@ func (sc *StorageConfig) UnmarshalJSON(data []byte) error { 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) + case "local-unix": + var localUnixConfig InputLocalUnixConfig + if err := json.Unmarshal(tmp.Config, &localUnixConfig); err != nil { + return fmt.Errorf("unmarshal LocalUnixConfig: %w", err) } - sc.Config = &localConfig + sc.Config = &localUnixConfig + default: + return fmt.Errorf("unsupported storage type: %s", tmp.Type) + } + + return nil +} + +type OutputStorageConfig struct { + Type string `json:"Type" validate:"required,oneof=b2 local-unix"` + Config any `json:"Config" validate:"required"` +} + +func (sc *OutputStorageConfig) 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-unix": + var localUnixConfig OutputLocalUnixConfig + if err := json.Unmarshal(tmp.Config, &localUnixConfig); err != nil { + return fmt.Errorf("unmarshal LocalUnixConfig: %w", err) + } + sc.Config = &localUnixConfig default: return fmt.Errorf("unsupported storage type: %s", tmp.Type) } @@ -68,8 +105,20 @@ type B2Config struct { ApplicationKey string `json:"ApplicationKey"` } -type LocalConfig struct { - Path string `json:"Path" validate:"required,min=1"` +type InputLocalUnixConfig struct { + MaxDepth int `json:"MaxDepth" validate:"required,min=0"` + Path string `json:"Path" validate:"required,min=1"` +} + +type OutputConfig struct { + Storage OutputStorageConfig `json:"Storage" validate:"required"` +} + +type OutputLocalUnixConfig struct { + Path string `json:"Path" validate:"required,min=1"` + DirPermissionMode string `json:"DirPermissionMode" validate:"required,min=3"` + FilePermissionMode string `json:"FilePermissionMode" validate:"required,min=3"` + AttributesImplementation string `json:"AttributesImplementation" validate:"required,oneof=xattr none"` } type ConverterConfig struct { @@ -113,10 +162,6 @@ type SizeConfig struct { 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 deleted file mode 100644 index db4ddd4..0000000 --- a/config/config.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "ForceRewrite": false, - "MaxConcurrentJobs": 4, - "LogLevel": "info", - "Input": { - "Storage": { - "Type": "b2", - "Config": { - "BucketName": "sayana-photos", - "Region": "eu-central-003", - "Prefix": "full/", - "KeyID": "${B2_KEY_ID}", - "ApplicationKey": "${B2_APPLICATION_KEY}" - } - }, - "KnownExtensions": [ - "jpg", - "jpeg", - "png" - ] - }, - "Converter": { - "Type": "webp", - "Config": { - "Quality": 80, - "Size": { - "MaxWidth": 800, - "MaxHeight": 0 - } - } - }, - "Output": { - "Storage": { - "Type": "b2", - "Config": { - "BucketName": "sayana-photos", - "Region": "eu-central-003", - "Prefix": "thumbnails/", - "KeyID": "${B2_KEY_ID}", - "ApplicationKey": "${B2_APPLICATION_KEY}" - } - } - } -} -- cgit v1.3.1+13