summaryrefslogtreecommitdiff
path: root/internal/client/input/local_unix.go
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/client/input/local_unix.go
parent4fa22c837cb00b87cd43c0aceec26729b8c3fa48 (diff)
downloadthumbnail-generator-f968d443dc5c6e1f5140922313b0d6e8019fc269.tar.gz
thumbnail-generator-f968d443dc5c6e1f5140922313b0d6e8019fc269.zip
feat: implement local-unix clients
Diffstat (limited to 'internal/client/input/local_unix.go')
-rw-r--r--internal/client/input/local_unix.go107
1 files changed, 107 insertions, 0 deletions
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)
+}