Add attachment replace workflow UI

This commit is contained in:
Joe Julian
2026-03-29 11:22:13 -07:00
parent d6bc213474
commit ac5b6894cf
6 changed files with 322 additions and 11 deletions
+47 -9
View File
@@ -11,6 +11,32 @@ import (
"git.julianfamily.org/keepassgo/vault"
)
func (u *ui) attachmentInput() (string, []byte, error) {
name := strings.TrimSpace(u.attachmentName.Text())
if name == "" {
return "", nil, fmt.Errorf("attachment name is required")
}
path := strings.TrimSpace(u.attachmentPath.Text())
if path == "" {
return "", nil, fmt.Errorf("attachment path is required")
}
info, err := os.Stat(path)
if err != nil {
return "", nil, fmt.Errorf("stat attachment: %w", err)
}
if info.Size() > maxAttachmentBytes {
return "", nil, fmt.Errorf("attachment too large: %d bytes exceeds %d byte limit", info.Size(), maxAttachmentBytes)
}
content, err := os.ReadFile(path)
if err != nil {
return "", nil, fmt.Errorf("read attachment: %w", err)
}
return name, content, nil
}
func (u *ui) loadSelectedEntryIntoEditor() {
u.selectedHistoryIndex = -1
u.historyIndex.SetText("")
@@ -185,16 +211,10 @@ func (u *ui) instantiateSelectedTemplateAction() error {
}
func (u *ui) addAttachmentAction() error {
content, err := os.ReadFile(strings.TrimSpace(u.attachmentPath.Text()))
name, content, err := u.attachmentInput()
if err != nil {
return fmt.Errorf("read attachment: %w", err)
return err
}
name := strings.TrimSpace(u.attachmentName.Text())
if name == "" {
return fmt.Errorf("attachment name is required")
}
if err := u.state.AddAttachmentToSelectedEntry(name, content); err != nil {
return err
}
@@ -204,6 +224,20 @@ func (u *ui) addAttachmentAction() error {
return nil
}
func (u *ui) replaceAttachmentAction() error {
name, content, err := u.attachmentInput()
if err != nil {
return err
}
if err := u.state.ReplaceAttachmentOnSelectedEntry(name, content); err != nil {
return err
}
u.loadSelectedEntryIntoEditor()
u.attachmentName.SetText(name)
u.filter()
return nil
}
func (u *ui) exportAttachmentAction() error {
item, ok := u.selectedEntry()
if !ok {
@@ -216,7 +250,11 @@ func (u *ui) exportAttachmentAction() error {
return fmt.Errorf("attachment not found")
}
if err := os.WriteFile(strings.TrimSpace(u.exportAttachmentPath.Text()), content, 0o600); err != nil {
exportPath := strings.TrimSpace(u.exportAttachmentPath.Text())
if exportPath == "" {
return fmt.Errorf("export attachment path is required")
}
if err := os.WriteFile(exportPath, content, 0o600); err != nil {
return fmt.Errorf("write attachment export: %w", err)
}
return nil