Add attachment replace workflow UI
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package appstate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -11,6 +12,11 @@ import (
|
||||
|
||||
type Section string
|
||||
|
||||
var (
|
||||
ErrAttachmentAlreadyExists = errors.New("attachment already exists")
|
||||
ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
)
|
||||
|
||||
const (
|
||||
SectionEntries Section = ""
|
||||
SectionTemplates Section = "templates"
|
||||
@@ -639,6 +645,36 @@ func (s *State) AddAttachmentToSelectedEntry(name string, content []byte) error
|
||||
if model.Entries[i].Attachments == nil {
|
||||
model.Entries[i].Attachments = map[string][]byte{}
|
||||
}
|
||||
if _, exists := model.Entries[i].Attachments[name]; exists {
|
||||
return ErrAttachmentAlreadyExists
|
||||
}
|
||||
model.Entries[i].Attachments[name] = append([]byte(nil), content...)
|
||||
session.Replace(model)
|
||||
s.Dirty = true
|
||||
return nil
|
||||
}
|
||||
|
||||
return vault.ErrEntryNotFound
|
||||
}
|
||||
|
||||
func (s *State) ReplaceAttachmentOnSelectedEntry(name string, content []byte) error {
|
||||
session, ok := s.Session.(MutableSession)
|
||||
if !ok {
|
||||
return fmt.Errorf("session is not mutable")
|
||||
}
|
||||
|
||||
model, err := session.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range model.Entries {
|
||||
if model.Entries[i].ID != s.SelectedEntryID {
|
||||
continue
|
||||
}
|
||||
if _, exists := model.Entries[i].Attachments[name]; !exists {
|
||||
return ErrAttachmentNotFound
|
||||
}
|
||||
model.Entries[i].Attachments[name] = append([]byte(nil), content...)
|
||||
session.Replace(model)
|
||||
s.Dirty = true
|
||||
@@ -663,6 +699,9 @@ func (s *State) DeleteAttachmentFromSelectedEntry(name string) error {
|
||||
if model.Entries[i].ID != s.SelectedEntryID {
|
||||
continue
|
||||
}
|
||||
if _, exists := model.Entries[i].Attachments[name]; !exists {
|
||||
return ErrAttachmentNotFound
|
||||
}
|
||||
delete(model.Entries[i].Attachments, name)
|
||||
if len(model.Entries[i].Attachments) == 0 {
|
||||
model.Entries[i].Attachments = nil
|
||||
|
||||
Reference in New Issue
Block a user