diff options
| author | 2025-07-16 17:49:03 +0700 | |
|---|---|---|
| committer | 2025-07-16 17:49:03 +0700 | |
| commit | f968d443dc5c6e1f5140922313b0d6e8019fc269 (patch) | |
| tree | efe1507ec20feccbe408ca2b05303400546431b1 /internal/client/input | |
| parent | 4fa22c837cb00b87cd43c0aceec26729b8c3fa48 (diff) | |
| download | thumbnail-generator-f968d443dc5c6e1f5140922313b0d6e8019fc269.tar.gz thumbnail-generator-f968d443dc5c6e1f5140922313b0d6e8019fc269.zip | |
feat: implement local-unix clients
Diffstat (limited to 'internal/client/input')
| -rw-r--r-- | internal/client/input/b2.go | 2 | ||||
| -rw-r--r-- | internal/client/input/input_client_interface.go | 7 | ||||
| -rw-r--r-- | internal/client/input/local_unix.go | 107 |
3 files changed, 115 insertions, 1 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) +} |