add:get and delete func

This commit is contained in:
2026-04-06 03:12:19 +09:00
parent 5e44a3a35c
commit e39ee1694d
7 changed files with 138 additions and 22 deletions

View File

@@ -2,9 +2,23 @@ package services
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
// debugLog appends a timestamped line to /tmp/filepass-debug.log.
// Safe to call from any goroutine; errors are silently ignored.
func debugLog(format string, args ...any) {
f, err := os.OpenFile("/tmp/filepass-debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return
}
defer f.Close()
fmt.Fprintf(f, "[%s] %s\n", time.Now().Format("15:04:05.000"), fmt.Sprintf(format, args...))
}
// StorageService executes file operations against a single server's storage.
type StorageService struct {
server Server
@@ -30,6 +44,30 @@ func (s *StorageService) Check() ([]string, error) {
return strings.Split(raw, "\n"), nil
}
// Get downloads a single file from remote storage into destDir.
func (s *StorageService) Get(filename, destDir string) error {
src := RemotePath(s.server, filename)
dst := filepath.Join(destDir, filename)
cmd := RsyncCmd(s.server, src, dst)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("get failed: %w\n%s", err, strings.TrimSpace(string(out)))
}
return nil
}
// Delete removes a single file from remote storage.
func (s *StorageService) Delete(filename string) error {
remoteCmd := "rm -f " + defaultStoragePath + "/" + shellQuote(filename)
cmd := SSHCmd(s.server, remoteCmd)
debugLog("Delete | args: %v", cmd.Args)
out, err := cmd.CombinedOutput()
debugLog("Delete | exit_err: %v | output: %q", err, strings.TrimSpace(string(out)))
if err != nil {
return fmt.Errorf("delete failed: %w\n%s", err, strings.TrimSpace(string(out)))
}
return nil
}
// Send transfers one or more local files to the remote storage.
// Multiple files are archived into a temp tarball first.
func (s *StorageService) Send(localPaths []string) error {
@@ -37,16 +75,8 @@ func (s *StorageService) Send(localPaths []string) error {
return fmt.Errorf("send: not yet implemented")
}
// Get downloads one or more files from remote storage to destDir.
// Multiple files are archived server-side, transferred, then extracted.
func (s *StorageService) Get(remoteFiles []string, destDir string) error {
// CleanAll removes all files from remote storage.
func (s *StorageService) CleanAll() error {
// TODO: implement
return fmt.Errorf("get: not yet implemented")
}
// Clean removes specific files from remote storage.
// Pass a nil or empty slice to remove all files.
func (s *StorageService) Clean(remoteFiles []string) error {
// TODO: implement
return fmt.Errorf("clean: not yet implemented")
return fmt.Errorf("clean all: not yet implemented")
}