diff --git a/hyphae/hyphae.go b/hyphae/hyphae.go index a260019..b8d8f14 100644 --- a/hyphae/hyphae.go +++ b/hyphae/hyphae.go @@ -37,6 +37,24 @@ type Hypha struct { BinaryPath string // == "" => no attachment } +func (h *Hypha) CanonicalName() string { + return h.Name +} + +func (h *Hypha) Kind() HyphaKind { + if !h.Exists { + return HyphaEmpty + } + if h.HasAttachment() { + return HyphaMedia + } + return HyphaText +} + +func (h *Hypha) HasTextPart() bool { + return h.TextPath != "" +} + // TextPartPath returns rooted path to the file where the text part should be. func (h *Hypha) TextPartPath() string { if h.TextPath == "" { @@ -103,16 +121,6 @@ func (h *Hypha) InsertIfNew() (justRecorded bool) { return false } -// Delete removes a hypha from the storage. -func (h *Hypha) Delete() { - byNamesMutex.Lock() - h.Lock() - delete(byNames, h.Name) - decrementCount() - byNamesMutex.Unlock() - h.Unlock() -} - // RenameTo renames a hypha and performs respective changes in the storage. func (h *Hypha) RenameTo(newName string) { byNamesMutex.Lock() diff --git a/hyphae/interface.go b/hyphae/interface.go new file mode 100644 index 0000000..2a81673 --- /dev/null +++ b/hyphae/interface.go @@ -0,0 +1,32 @@ +package hyphae + +import "sync" + +type HyphaKind int + +const ( + HyphaEmpty HyphaKind = iota + HyphaText + HyphaMedia +) + +// Hypher is a temporary name for this interface. The name will become Hypha, once the struct with the said name is deprecated for good. +type Hypher interface { + sync.Locker + + CanonicalName() string + Kind() HyphaKind + + HasTextPart() bool + TextPartPath() string +} + +// DeleteHypha deletes the hypha from the storage. +func DeleteHypha(h Hypher) { + byNamesMutex.Lock() + h.Lock() + delete(byNames, h.CanonicalName()) + decrementCount() + byNamesMutex.Unlock() + h.Unlock() +} diff --git a/shroom/delete.go b/shroom/delete.go index 2f3d5a8..37b538b 100644 --- a/shroom/delete.go +++ b/shroom/delete.go @@ -27,7 +27,7 @@ func DeleteHypha(u *user.User, h *hyphae.Hypha, lc *l18n.Localizer) (hop *histor Apply() if !hop.HasErrors() { backlinks.UpdateBacklinksAfterDelete(h, originalText) - h.Delete() + hyphae.DeleteHypha(h) } return hop, "" } diff --git a/shroom/unattach.go b/shroom/unattach.go index 02bd9a7..41452ba 100644 --- a/shroom/unattach.go +++ b/shroom/unattach.go @@ -35,7 +35,7 @@ func UnattachHypha(u *user.User, h *hyphae.Hypha, lc *l18n.Localizer) (hop *hist } // If nothing is left of the hypha if h.TextPath == "" { - h.Delete() + hyphae.DeleteHypha(h) } return hop, "" }