summaryrefslogtreecommitdiff
path: root/internal
diff options
from:
to:
context:
space:
mode:
authorGravatar SayaAndy <montferrat@tuta.io> 2025-07-16 17:49:03 +0700
committerGravatar SayaAndy <montferrat@tuta.io> 2025-07-16 17:49:03 +0700
commitf968d443dc5c6e1f5140922313b0d6e8019fc269 (patch)
treeefe1507ec20feccbe408ca2b05303400546431b1 /internal
parent4fa22c837cb00b87cd43c0aceec26729b8c3fa48 (diff)
downloadthumbnail-generator-f968d443dc5c6e1f5140922313b0d6e8019fc269.tar.gz
thumbnail-generator-f968d443dc5c6e1f5140922313b0d6e8019fc269.zip
feat: implement local-unix clients
Diffstat (limited to 'internal')
-rw-r--r--internal/client/input/b2.go2
-rw-r--r--internal/client/input/input_client_interface.go7
-rw-r--r--internal/client/input/local_unix.go107
-rw-r--r--internal/client/output/b2.go2
-rw-r--r--internal/client/output/local_unix.go130
-rw-r--r--internal/client/output/output_client_interface.go6
-rw-r--r--internal/converter/converter_interface.go16
-rw-r--r--internal/converter/processor_interface.go8
-rw-r--r--internal/converter/webp.go2
9 files changed, 269 insertions, 11 deletions
diff --git a/internal/client/input/b2.go b/internal/client/input/b2.go
index 54a6b09..0092313 100644
--- a/internal/client/input/b2.go
+++ b/internal/client/input/b2.go
@@ -21,7 +21,7 @@ type B2InputClient struct {
knownExtensions []string
}
-func NewB2InputClient(cfg *config.InputConfig) (*B2InputClient, error) {
+func NewB2InputClient(cfg *config.InputConfig) (InputClient, error) {
if cfg.Storage.Type != "b2" {
return nil, fmt.Errorf("invalid storage type for B2InputClient")
}
diff --git a/internal/client/input/input_client_interface.go b/internal/client/input/input_client_interface.go
index c896ca4..5d12fa8 100644
--- a/internal/client/input/input_client_interface.go
+++ b/internal/client/input/input_client_interface.go
@@ -3,6 +3,8 @@ package input
import (
"io"
"time"
+
+ "github.com/SayaAndy/saya-today-thumbnail-generator/config"
)
type InputClient interface {
@@ -20,3 +22,8 @@ type MetadataStruct struct {
LastModified time.Time
Misc map[string]string
}
+
+var NewInputClientMap = map[string]func(cfg *config.InputConfig) (InputClient, error){
+ "b2": NewB2InputClient,
+ "local-unix": NewLocalUnixInputClient,
+}
diff --git a/internal/client/input/local_unix.go b/internal/client/input/local_unix.go
new file mode 100644
index 0000000..2df3a41
--- /dev/null
+++ b/internal/client/input/local_unix.go
@@ -0,0 +1,107 @@
+package input
+
+import (
+ "fmt"
+ "io"
+ "log/slog"
+ "mime"
+ "os"
+ "slices"
+ "strconv"
+ "strings"
+ "syscall"
+ "time"
+
+ "github.com/SayaAndy/saya-today-thumbnail-generator/config"
+)
+
+var _ InputClient = (*LocalUnixInputClient)(nil)
+
+type LocalUnixInputClient struct {
+ path string
+ maxDepth int
+ knownExtensions []string
+}
+
+func NewLocalUnixInputClient(cfg *config.InputConfig) (InputClient, error) {
+ if cfg.Storage.Type != "local-unix" {
+ return nil, fmt.Errorf("invalid storage type for LocalUnixInputClient")
+ }
+ localCfg := cfg.Storage.Config.(*config.InputLocalUnixConfig)
+
+ return &LocalUnixInputClient{
+ path: localCfg.Path,
+ maxDepth: localCfg.MaxDepth,
+ knownExtensions: cfg.KnownExtensions,
+ }, nil
+}
+
+func (c *LocalUnixInputClient) Scan() ([]string, error) {
+ return c.recursiveScan(c.path, c.maxDepth)
+}
+
+func (c *LocalUnixInputClient) recursiveScan(dir string, depth int) ([]string, error) {
+ filePaths := make([]string, 0)
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ return nil, fmt.Errorf("fail to read directory: %w", err)
+ }
+
+ for _, entry := range entries {
+ if entry.IsDir() && depth > 0 {
+ subFilePaths, err := c.recursiveScan(dir+entry.Name()+"/", depth-1)
+ if err != nil {
+ return nil, fmt.Errorf("fail to scan subdirectory '%s': %w", entry.Name(), err)
+ }
+ filePaths = append(filePaths, subFilePaths...)
+ } else if !entry.IsDir() {
+ nameParts := strings.Split(entry.Name(), ".")
+ if len(nameParts) < 2 {
+ continue
+ }
+ if slices.Contains(c.knownExtensions, strings.ToLower(nameParts[len(nameParts)-1])) {
+ filePaths = append(filePaths, strings.TrimPrefix(dir+entry.Name(), c.path))
+ }
+ fmt.Println(entry.Name())
+ }
+ }
+
+ return filePaths, nil
+}
+
+func (c *LocalUnixInputClient) ReadMetadata(path string) (*MetadataStruct, error) {
+ nodePathParts := strings.Split(path, "/")
+ nodeName := nodePathParts[len(nodePathParts)-1]
+ nodeNameParts := strings.Split(nodeName, ".")
+ nodeExt := ""
+ if len(nodeNameParts) >= 2 {
+ nodeExt = nodeNameParts[len(nodeNameParts)-1]
+ }
+ slog.Debug("got a file extension", slog.String("extension", nodeExt), slog.String("path", path), slog.String("filename", nodeName))
+
+ fileInfo, err := os.Stat(c.path + path)
+ if err != nil {
+ return nil, fmt.Errorf("fail to read file info: %w", err)
+ }
+
+ stat_t := fileInfo.Sys().(*syscall.Stat_t)
+ creationTime := time.Unix(stat_t.Ctim.Sec, stat_t.Ctim.Nsec)
+
+ misc := map[string]string{
+ "Size": strconv.FormatInt(fileInfo.Size(), 10),
+ }
+
+ return &MetadataStruct{
+ Name: fileInfo.Name(),
+ StorageType: "local-unix",
+ Hash: strconv.FormatInt(fileInfo.ModTime().Unix(), 16),
+ ContentType: mime.TypeByExtension("." + nodeExt),
+ FirstCreated: creationTime,
+ LastModified: fileInfo.ModTime(),
+ Misc: misc,
+ }, nil
+}
+
+func (c *LocalUnixInputClient) GetReader(path string) (io.ReadCloser, error) {
+ return os.Open(c.path + path)
+}
diff --git a/internal/client/output/b2.go b/internal/client/output/b2.go
index 6979e5b..e741e25 100644
--- a/internal/client/output/b2.go
+++ b/internal/client/output/b2.go
@@ -19,7 +19,7 @@ type B2OutputClient struct {
b2cl *b2.Client
}
-func NewB2OutputClient(cfg *config.OutputConfig) (*B2OutputClient, error) {
+func NewB2OutputClient(cfg *config.OutputConfig) (OutputClient, error) {
if cfg.Storage.Type != "b2" {
return nil, fmt.Errorf("invalid storage type for B2OutputClient")
}
diff --git a/internal/client/output/local_unix.go b/internal/client/output/local_unix.go
new file mode 100644
index 0000000..798be3b
--- /dev/null
+++ b/internal/client/output/local_unix.go
@@ -0,0 +1,130 @@
+package output
+
+import (
+ "fmt"
+ "io"
+ "mime"
+ "os"
+ "strconv"
+ "strings"
+ "syscall"
+ "time"
+
+ "github.com/SayaAndy/saya-today-thumbnail-generator/config"
+ "github.com/SayaAndy/saya-today-thumbnail-generator/internal/client/input"
+ "golang.org/x/sys/unix"
+)
+
+var _ OutputClient = (*LocalUnixOutputClient)(nil)
+
+type LocalUnixOutputClient struct {
+ path string
+ fileMode uint32
+ dirMode uint32
+ attrMode string
+}
+
+func NewLocalUnixOutputClient(cfg *config.OutputConfig) (OutputClient, error) {
+ if cfg.Storage.Type != "local-unix" {
+ return nil, fmt.Errorf("invalid storage type for LocalUnixOutputClient")
+ }
+ localCfg := cfg.Storage.Config.(*config.OutputLocalUnixConfig)
+
+ fpm, err := strconv.ParseInt(localCfg.FilePermissionMode, 8, 32)
+ if err != nil {
+ return nil, fmt.Errorf("fail to parse file permission mode as an octal number: %w", err)
+ }
+
+ dpm, err := strconv.ParseInt(localCfg.DirPermissionMode, 8, 32)
+ if err != nil {
+ return nil, fmt.Errorf("fail to parse directory permission mode as an octal number: %w", err)
+ }
+
+ return &LocalUnixOutputClient{localCfg.Path, uint32(fpm), uint32(dpm), localCfg.AttributesImplementation}, nil
+}
+
+func (c *LocalUnixOutputClient) GetWriter(path string, inputMetadata *input.MetadataStruct) (io.WriteCloser, error) {
+ pathSegments := strings.Split(path, "/")
+ dirpath := strings.Join(pathSegments[0:len(pathSegments)-1], "/")
+ if err := os.MkdirAll(c.path+dirpath, os.FileMode(c.dirMode)); err != nil {
+ return nil, fmt.Errorf("fail to mkdir parent directories for a path: %w", err)
+ }
+
+ var err error
+ if _, err = os.Stat(c.path + path); err != nil && !os.IsNotExist(err) {
+ return nil, fmt.Errorf("fail to stat a file, although file exists: %w", err)
+ }
+ if os.IsNotExist(err) {
+ f, err := os.OpenFile(c.path+path, os.O_WRONLY|os.O_CREATE, os.FileMode(c.fileMode))
+ if err != nil {
+ return nil, fmt.Errorf("fail to create a file: %w", err)
+ }
+ f.Close()
+ }
+
+ switch c.attrMode {
+ case "xattr":
+ if err := unix.Setxattr(c.path+path, "user.originalfile.mddate", []byte(strconv.FormatInt(inputMetadata.LastModified.Unix(), 16)), 0); err != nil {
+ return nil, fmt.Errorf("fail to write user.originalfile.mddate xattribute: %w", err)
+ }
+ case "none":
+ default:
+ return nil, fmt.Errorf("unknown attributes implementation: %s", c.attrMode)
+ }
+
+ return os.OpenFile(c.path+path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(c.fileMode))
+}
+
+func (c *LocalUnixOutputClient) ReadMetadata(path string) (*MetadataStruct, error) {
+ nodePathParts := strings.Split(path, "/")
+ nodeName := nodePathParts[len(nodePathParts)-1]
+ nodeNameParts := strings.Split(nodeName, ".")
+ nodeExt := ""
+ if len(nodeNameParts) >= 2 {
+ nodeExt = nodeNameParts[len(nodeNameParts)-1]
+ }
+
+ fileInfo, err := os.Stat(c.path + path)
+ if err != nil {
+ return nil, fmt.Errorf("fail to read file info: %w", err)
+ }
+
+ stat_t := fileInfo.Sys().(*syscall.Stat_t)
+ creationTime := time.Unix(stat_t.Ctim.Sec, stat_t.Ctim.Nsec)
+
+ misc := map[string]string{
+ "Size": strconv.FormatInt(fileInfo.Size(), 10),
+ }
+
+ mddateOriginal := make([]byte, 0)
+ switch c.attrMode {
+ case "xattr":
+ sz, err := unix.Getxattr(c.path+path, "user.originalfile.mddate", nil)
+ if err != nil {
+ return nil, fmt.Errorf("fail to get size of user.originalfile.mddate attribute: %w", err)
+ }
+ mddateOriginal = make([]byte, sz)
+ if _, err = unix.Getxattr(c.path+path, "user.originalfile.mddate", mddateOriginal); err != nil {
+ return nil, fmt.Errorf("fail to get user.originalfile.mddate attribute: %w", err)
+ }
+ case "none":
+ default:
+ return nil, fmt.Errorf("unknown attributes implementation: %s", c.attrMode)
+ }
+
+ return &MetadataStruct{
+ Name: fileInfo.Name(),
+ StorageType: "local-unix",
+ Hash: strconv.FormatInt(fileInfo.ModTime().Unix(), 16),
+ HashOriginal: string(mddateOriginal),
+ ContentType: mime.TypeByExtension("." + nodeExt),
+ FirstCreated: creationTime,
+ LastModified: fileInfo.ModTime(),
+ Misc: misc,
+ }, nil
+}
+
+func (c *LocalUnixOutputClient) IsMissing(path string) bool {
+ _, err := os.Stat(c.path + path)
+ return err != nil
+}
diff --git a/internal/client/output/output_client_interface.go b/internal/client/output/output_client_interface.go
index 4ec4728..437895c 100644
--- a/internal/client/output/output_client_interface.go
+++ b/internal/client/output/output_client_interface.go
@@ -4,6 +4,7 @@ import (
"io"
"time"
+ "github.com/SayaAndy/saya-today-thumbnail-generator/config"
"github.com/SayaAndy/saya-today-thumbnail-generator/internal/client/input"
)
@@ -23,3 +24,8 @@ type MetadataStruct struct {
LastModified time.Time
Misc map[string]string
}
+
+var NewOutputClientMap = map[string]func(cfg *config.OutputConfig) (OutputClient, error){
+ "b2": NewB2OutputClient,
+ "local-unix": NewLocalUnixOutputClient,
+}
diff --git a/internal/converter/converter_interface.go b/internal/converter/converter_interface.go
new file mode 100644
index 0000000..342ca1e
--- /dev/null
+++ b/internal/converter/converter_interface.go
@@ -0,0 +1,16 @@
+package converter
+
+import (
+ "io"
+
+ "github.com/SayaAndy/saya-today-thumbnail-generator/config"
+)
+
+type Converter interface {
+ DeductOutputPath(inputPath string) string
+ Process(ext string, reader io.ReadCloser, writer io.WriteCloser) error
+}
+
+var NewConverterMap = map[string]func(cfg *config.ConverterConfig) (Converter, error){
+ "webp": NewWebpConverter,
+}
diff --git a/internal/converter/processor_interface.go b/internal/converter/processor_interface.go
deleted file mode 100644
index 88b5ac3..0000000
--- a/internal/converter/processor_interface.go
+++ /dev/null
@@ -1,8 +0,0 @@
-package converter
-
-import "io"
-
-type Converter interface {
- DeductOutputPath(inputPath string) string
- Process(ext string, reader io.ReadCloser, writer io.WriteCloser) error
-}
diff --git a/internal/converter/webp.go b/internal/converter/webp.go
index b7f1216..8a9c09a 100644
--- a/internal/converter/webp.go
+++ b/internal/converter/webp.go
@@ -24,7 +24,7 @@ type WebpConverter struct {
quality int
}
-func NewWebpConverter(cfg *config.ConverterConfig) (*WebpConverter, error) {
+func NewWebpConverter(cfg *config.ConverterConfig) (Converter, error) {
if cfg.Type != "webp" {
return nil, fmt.Errorf("invalid storage type for WebpConverter")
}