Files
pop/internal/attachment/manager.go
Michael Freno 7bbba9f15c FRE-683: Add contacts & attachments management
- Implemented contact CRUD operations (list, add, edit, delete)
- Implemented attachment management (list, upload, download)
- Created internal/contact/manager.go for contact persistence
- Created internal/attachment/manager.go for attachment storage
- Added CLI commands in cmd/contacts.go and cmd/attachments.go
- Integrated contact and attachment commands into root CLI

Files:
- internal/contact/types.go - Contact data models
- internal/contact/manager.go - Contact CRUD operations
- internal/attachment/manager.go - Attachment file operations
- cmd/contacts.go - Contact CLI commands
- cmd/attachments.go - Attachment CLI commands

Contacts stored in ~/.config/pop/contacts.json
Attachments stored in ~/.config/pop/attachments/
2026-04-26 10:26:29 -04:00

79 lines
1.6 KiB
Go

package attachment
import (
"io"
"os"
"path/filepath"
)
type AttachmentManager struct {
attachmentsDir string
}
func NewAttachmentManager() *AttachmentManager {
return &AttachmentManager{
attachmentsDir: filepath.Join(os.Getenv("HOME"), ".config", "pop", "attachments"),
}
}
func (m *AttachmentManager) Download(attachmentID, name, destPath string) error {
if err := os.MkdirAll(m.attachmentsDir, 0755); err != nil {
return err
}
srcPath := filepath.Join(m.attachmentsDir, attachmentID)
dest := filepath.Join(destPath, name)
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
return err
}
data, err := os.ReadFile(srcPath)
if err != nil {
return err
}
return os.WriteFile(dest, data, 0644)
}
func (m *AttachmentManager) Upload(attachmentID, name string, reader io.Reader) error {
if err := os.MkdirAll(m.attachmentsDir, 0755); err != nil {
return err
}
path := filepath.Join(m.attachmentsDir, attachmentID)
data, err := io.ReadAll(reader)
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func (m *AttachmentManager) Get(attachmentID string) ([]byte, error) {
path := filepath.Join(m.attachmentsDir, attachmentID)
return os.ReadFile(path)
}
func (m *AttachmentManager) Delete(attachmentID string) error {
path := filepath.Join(m.attachmentsDir, attachmentID)
return os.Remove(path)
}
func (m *AttachmentManager) List() ([]string, error) {
entries, err := os.ReadDir(m.attachmentsDir)
if err != nil {
if os.IsNotExist(err) {
return []string{}, nil
}
return nil, err
}
ids := make([]string, len(entries))
for i, entry := range entries {
ids[i] = entry.Name()
}
return ids, nil
}