diff --git a/hyphae/count.go b/hyphae/count.go index 4d67c81..1f769d3 100644 --- a/hyphae/count.go +++ b/hyphae/count.go @@ -4,7 +4,7 @@ import ( "sync" ) -// Its value is number of all existing hyphae. MediaHypha mutators are expected to manipulate the value. It is concurrent-safe. +// Its value is number of all existing hyphae. NonEmptyHypha mutators are expected to manipulate the value. It is concurrent-safe. var count = struct { value int sync.Mutex diff --git a/hyphae/empty_hypha.go b/hyphae/empty_hypha.go index 21c0b4b..31e1bf3 100644 --- a/hyphae/empty_hypha.go +++ b/hyphae/empty_hypha.go @@ -27,8 +27,8 @@ func NewEmptyHypha(hyphaName string) *EmptyHypha { } } -func FillEmptyHyphaUpToTextualHypha(e *EmptyHypha, textPath string) *MediaHypha { // sic! - return &MediaHypha{ +func FillEmptyHyphaUpToTextualHypha(e *EmptyHypha, textPath string) *NonEmptyHypha { // sic! + return &NonEmptyHypha{ name: e.CanonicalName(), TextPath: textPath, } diff --git a/hyphae/files.go b/hyphae/files.go index b1d249b..9541260 100644 --- a/hyphae/files.go +++ b/hyphae/files.go @@ -24,7 +24,7 @@ func Index(path string) { insert(nh) default: // In case of conflicts the newer hypha overwrites the previous - switch nh, oh := nh.(*MediaHypha), oh.(*MediaHypha); { + switch nh, oh := nh.(*NonEmptyHypha), oh.(*NonEmptyHypha); { case (nh.Kind() == HyphaText) && (oh.Kind() == HyphaMedia): oh.TextPath = nh.TextPartPath() @@ -64,7 +64,7 @@ func indexHelper(path string, nestLevel uint, ch chan Hypher) { var ( hyphaPartPath = filepath.Join(path, node.Name()) hyphaName, isText, skip = mimetype.DataFromFilename(hyphaPartPath) - hypha = &MediaHypha{name: hyphaName, Exists: true} + hypha = &NonEmptyHypha{name: hyphaName, Exists: true} ) if !skip { if isText { diff --git a/hyphae/interface.go b/hyphae/interface.go index ae8e991..801a178 100644 --- a/hyphae/interface.go +++ b/hyphae/interface.go @@ -29,7 +29,7 @@ const ( HyphaMedia ) -// Hypher is a temporary name for this interface. The name will become MediaHypha, once the struct with the said name is deprecated for good. +// Hypher is a temporary name for this interface. The name will become NonEmptyHypha, once the struct with the said name is deprecated for good. type Hypher interface { sync.Locker @@ -54,8 +54,8 @@ func RenameHyphaTo(h Hypher, newName string) { byNamesMutex.Lock() h.Lock() delete(byNames, h.CanonicalName()) - h.(*MediaHypha).SetName(newName) - byNames[h.CanonicalName()] = h.(*MediaHypha) + h.(*NonEmptyHypha).SetName(newName) + byNames[h.CanonicalName()] = h.(*NonEmptyHypha) byNamesMutex.Unlock() h.Unlock() } diff --git a/hyphae/media_hypha.go b/hyphae/media_hypha.go deleted file mode 100644 index 337acee..0000000 --- a/hyphae/media_hypha.go +++ /dev/null @@ -1,52 +0,0 @@ -// Package hyphae is for the MediaHypha type, hypha storage and stuff like that. It shall not depend on mycorrhiza modules other than util. -package hyphae - -import ( - "path/filepath" - "sync" - - "github.com/bouncepaw/mycorrhiza/files" -) - -// MediaHypha keeps vital information about a media hypha -type MediaHypha struct { - sync.RWMutex - - name string // Canonical name - Exists bool - TextPath string // == "" => no text part - binaryPath string // == "" => no attachment -} - -func (h *MediaHypha) SetName(s string) { h.name = s } - -func (h *MediaHypha) BinaryPath() string { return h.binaryPath } -func (h *MediaHypha) SetBinaryPath(s string) { h.binaryPath = s } - -func (h *MediaHypha) CanonicalName() string { - return h.name -} - -func (h *MediaHypha) Kind() HyphaKind { // sic! - if h.HasAttachment() { - return HyphaMedia - } - return HyphaText -} - -func (h *MediaHypha) HasTextPart() bool { - return h.TextPath != "" -} - -// TextPartPath returns rooted path to the file where the text part should be. -func (h *MediaHypha) TextPartPath() string { - if h.TextPath == "" { - return filepath.Join(files.HyphaeDir(), h.name+".myco") - } - return h.TextPath -} - -// HasAttachment is true if the hypha has an attachment. -func (h *MediaHypha) HasAttachment() bool { - return h.binaryPath != "" -} diff --git a/hyphae/non_empty_hypha.go b/hyphae/non_empty_hypha.go new file mode 100644 index 0000000..425544d --- /dev/null +++ b/hyphae/non_empty_hypha.go @@ -0,0 +1,52 @@ +// Package hyphae is for the NonEmptyHypha type, hypha storage and stuff like that. It shall not depend on mycorrhiza modules other than util. +package hyphae + +import ( + "path/filepath" + "sync" + + "github.com/bouncepaw/mycorrhiza/files" +) + +// NonEmptyHypha keeps vital information about a media hypha +type NonEmptyHypha struct { + sync.RWMutex + + name string // Canonical name + Exists bool + TextPath string // == "" => no text part + binaryPath string // == "" => no attachment +} + +func (h *NonEmptyHypha) SetName(s string) { h.name = s } + +func (h *NonEmptyHypha) BinaryPath() string { return h.binaryPath } +func (h *NonEmptyHypha) SetBinaryPath(s string) { h.binaryPath = s } + +func (h *NonEmptyHypha) CanonicalName() string { + return h.name +} + +func (h *NonEmptyHypha) Kind() HyphaKind { // sic! + if h.HasAttachment() { + return HyphaMedia + } + return HyphaText +} + +func (h *NonEmptyHypha) HasTextPart() bool { + return h.TextPath != "" +} + +// TextPartPath returns rooted path to the file where the text part should be. +func (h *NonEmptyHypha) TextPartPath() string { + if h.TextPath == "" { + return filepath.Join(files.HyphaeDir(), h.name+".myco") + } + return h.TextPath +} + +// HasAttachment is true if the hypha has an attachment. +func (h *NonEmptyHypha) HasAttachment() bool { + return h.binaryPath != "" +} diff --git a/shroom/can.go b/shroom/can.go index e5912c3..8c269a2 100644 --- a/shroom/can.go +++ b/shroom/can.go @@ -69,7 +69,7 @@ var ( func(h hyphae.Hypher, u *user.User, lc *l18n.Localizer) (errmsg, errtitle string) { switch h := h.(type) { case *hyphae.EmptyHypha: - case *hyphae.MediaHypha: + case *hyphae.NonEmptyHypha: if h.Kind() != hyphae.HyphaMedia { rejectUnattachLog(h, u, "no amnt") return lc.Get("ui.act_noattachment_tip"), lc.Get("ui.act_noattachment") diff --git a/shroom/delete.go b/shroom/delete.go index 73cb236..9654710 100644 --- a/shroom/delete.go +++ b/shroom/delete.go @@ -21,7 +21,7 @@ func DeleteHypha(u *user.User, h hyphae.Hypher, lc *l18n.Localizer) (hop *histor originalText, _ := FetchTextPart(h) hop. - WithFilesRemoved(h.TextPartPath(), h.(*hyphae.MediaHypha).BinaryPath()). + WithFilesRemoved(h.TextPartPath(), h.(*hyphae.NonEmptyHypha).BinaryPath()). WithMsg(fmt.Sprintf("Delete ā%sā", h.CanonicalName())). WithUser(u). Apply() diff --git a/shroom/init.go b/shroom/init.go index deef322..751d4dc 100644 --- a/shroom/init.go +++ b/shroom/init.go @@ -24,7 +24,7 @@ func init() { err = errors.New("Hypha " + hyphaName + " does not exist") default: rawText, err = FetchTextPart(h) - if h := h.(*hyphae.MediaHypha); h.Kind() == hyphae.HyphaMedia { + if h := h.(*hyphae.NonEmptyHypha); h.Kind() == hyphae.HyphaMedia { // the view is localized, but we can't pass it, so... binaryBlock = views.AttachmentHTMLRaw(h) } diff --git a/shroom/rename.go b/shroom/rename.go index 6668c44..ec2d8e3 100644 --- a/shroom/rename.go +++ b/shroom/rename.go @@ -71,7 +71,7 @@ func RenameHypha(h hyphae.Hypher, newHypha hyphae.Hypher, recursive bool, u *use Apply() if len(hop.Errs) == 0 { for _, h := range hyphaeToRename { - h := h.(*hyphae.MediaHypha) // ontology think + h := h.(*hyphae.NonEmptyHypha) // ontology think oldName := h.CanonicalName() hyphae.RenameHyphaTo(h, replaceName(h.CanonicalName())) h.Lock() @@ -102,7 +102,7 @@ func renamingPairs(hyphaeToRename []hyphae.Hypher, replaceName func(string) stri renameMap[h.TextPartPath()] = replaceName(h.TextPartPath()) } switch h := h.(type) { - case *hyphae.MediaHypha: + case *hyphae.NonEmptyHypha: if h.Kind() == hyphae.HyphaMedia { // ontology think renameMap[h.BinaryPath()] = replaceName(h.BinaryPath()) } @@ -110,7 +110,7 @@ func renamingPairs(hyphaeToRename []hyphae.Hypher, replaceName func(string) stri h.Unlock() } if firstFailure, ok := hyphae.AreFreeNames(newNames...); !ok { - return nil, errors.New("MediaHypha " + firstFailure + " already exists") + return nil, errors.New("NonEmptyHypha " + firstFailure + " already exists") } return renameMap, nil } diff --git a/shroom/unattach.go b/shroom/unattach.go index 5897e4a..3ef4877 100644 --- a/shroom/unattach.go +++ b/shroom/unattach.go @@ -17,7 +17,7 @@ func UnattachHypha(u *user.User, h hyphae.Hypher, lc *l18n.Localizer) (hop *hist hop.WithErrAbort(err) return hop, errtitle } - H := h.(*hyphae.MediaHypha) + H := h.(*hyphae.NonEmptyHypha) hop. WithFilesRemoved(H.BinaryPath()). diff --git a/shroom/upload.go b/shroom/upload.go index db29bb7..39d34f7 100644 --- a/shroom/upload.go +++ b/shroom/upload.go @@ -41,7 +41,7 @@ func writeTextToDiskForEmptyHypha(eh *hyphae.EmptyHypha, data []byte) error { return writeTextToDiskForNonEmptyHypha(h, data) } -func writeTextToDiskForNonEmptyHypha(h *hyphae.MediaHypha, data []byte) error { +func writeTextToDiskForNonEmptyHypha(h *hyphae.NonEmptyHypha, data []byte) error { if err := os.MkdirAll(filepath.Dir(h.TextPartPath()), 0777); err != nil { return err } @@ -77,7 +77,7 @@ func UploadText(h hyphae.Hypher, data []byte, userMessage string, u *user.User, case *hyphae.EmptyHypha: // It's ok, just like cancel button. return hop.Abort(), "" - case *hyphae.MediaHypha: + case *hyphae.NonEmptyHypha: switch h.Kind() { case hyphae.HyphaMedia: // Writing no description, it's ok, just like cancel button. @@ -99,7 +99,7 @@ func UploadText(h hyphae.Hypher, data []byte, userMessage string, u *user.User, } hyphae.InsertIfNew(h) - case *hyphae.MediaHypha: + case *hyphae.NonEmptyHypha: oldText, err := FetchTextPart(h) if err != nil { return hop.WithErrAbort(err), err.Error() @@ -152,7 +152,7 @@ func UploadBinary(h hyphae.Hypher, mime string, file multipart.File, u *user.Use err := errors.New("bad path") return hop.WithErrAbort(err), err.Error() } - if h := h.(*hyphae.MediaHypha); hop.Type == history.TypeEditBinary { + if h := h.(*hyphae.NonEmptyHypha); hop.Type == history.TypeEditBinary { sourceFullPath = h.BinaryPath() } @@ -186,7 +186,7 @@ func UploadBinary(h hyphae.Hypher, mime string, file multipart.File, u *user.Use } // sic! - h.(*hyphae.MediaHypha).SetBinaryPath(fullPath) + h.(*hyphae.NonEmptyHypha).SetBinaryPath(fullPath) return hop.WithFiles(fullPath).WithUser(u).Apply(), "" } diff --git a/tree/tree.go b/tree/tree.go index 5a354b9..0a38770 100644 --- a/tree/tree.go +++ b/tree/tree.go @@ -20,7 +20,7 @@ func findSiblings(hyphaName string) []*sibling { siblingsMap = make(map[string]bool) siblingCheck = func(h hyphae.Hypher) hyphae.CheckResult { switch { - case h.CanonicalName() == hyphaName, // MediaHypha is no sibling of itself + case h.CanonicalName() == hyphaName, // NonEmptyHypha is no sibling of itself h.CanonicalName() == parentHyphaName: // Parent hypha is no sibling of its child return hyphae.CheckContinue } diff --git a/views/hypha.qtpl b/views/hypha.qtpl index a41621f..8997de1 100644 --- a/views/hypha.qtpl +++ b/views/hypha.qtpl @@ -75,9 +75,9 @@ {% endfunc %} -{% func AttachmentHTMLRaw(h *hyphae.MediaHypha) %}{%= AttachmentHTML(h, l18n.New("en", "en")) %}{% endfunc %} +{% func AttachmentHTMLRaw(h *hyphae.NonEmptyHypha) %}{%= AttachmentHTML(h, l18n.New("en", "en")) %}{% endfunc %} -{% func AttachmentHTML(h *hyphae.MediaHypha, lc *l18n.Localizer) %} +{% func AttachmentHTML(h *hyphae.NonEmptyHypha, lc *l18n.Localizer) %} {% switch filepath.Ext(h.BinaryPath()) %} {% case ".jpg", ".gif", ".png", ".webp", ".svg", ".ico" %} diff --git a/views/hypha.qtpl.go b/views/hypha.qtpl.go index 8986c92..d689883 100644 --- a/views/hypha.qtpl.go +++ b/views/hypha.qtpl.go @@ -349,14 +349,14 @@ func NaviTitleHTML(h hyphae.Hypher) string { } //line views/hypha.qtpl:78 -func StreamAttachmentHTMLRaw(qw422016 *qt422016.Writer, h *hyphae.MediaHypha) { +func StreamAttachmentHTMLRaw(qw422016 *qt422016.Writer, h *hyphae.NonEmptyHypha) { //line views/hypha.qtpl:78 StreamAttachmentHTML(qw422016, h, l18n.New("en", "en")) //line views/hypha.qtpl:78 } //line views/hypha.qtpl:78 -func WriteAttachmentHTMLRaw(qq422016 qtio422016.Writer, h *hyphae.MediaHypha) { +func WriteAttachmentHTMLRaw(qq422016 qtio422016.Writer, h *hyphae.NonEmptyHypha) { //line views/hypha.qtpl:78 qw422016 := qt422016.AcquireWriter(qq422016) //line views/hypha.qtpl:78 @@ -367,7 +367,7 @@ func WriteAttachmentHTMLRaw(qq422016 qtio422016.Writer, h *hyphae.MediaHypha) { } //line views/hypha.qtpl:78 -func AttachmentHTMLRaw(h *hyphae.MediaHypha) string { +func AttachmentHTMLRaw(h *hyphae.NonEmptyHypha) string { //line views/hypha.qtpl:78 qb422016 := qt422016.AcquireByteBuffer() //line views/hypha.qtpl:78 @@ -382,7 +382,7 @@ func AttachmentHTMLRaw(h *hyphae.MediaHypha) string { } //line views/hypha.qtpl:80 -func StreamAttachmentHTML(qw422016 *qt422016.Writer, h *hyphae.MediaHypha, lc *l18n.Localizer) { +func StreamAttachmentHTML(qw422016 *qt422016.Writer, h *hyphae.NonEmptyHypha, lc *l18n.Localizer) { //line views/hypha.qtpl:80 qw422016.N().S(` `) @@ -486,7 +486,7 @@ func StreamAttachmentHTML(qw422016 *qt422016.Writer, h *hyphae.MediaHypha, lc *l } //line views/hypha.qtpl:109 -func WriteAttachmentHTML(qq422016 qtio422016.Writer, h *hyphae.MediaHypha, lc *l18n.Localizer) { +func WriteAttachmentHTML(qq422016 qtio422016.Writer, h *hyphae.NonEmptyHypha, lc *l18n.Localizer) { //line views/hypha.qtpl:109 qw422016 := qt422016.AcquireWriter(qq422016) //line views/hypha.qtpl:109 @@ -497,7 +497,7 @@ func WriteAttachmentHTML(qq422016 qtio422016.Writer, h *hyphae.MediaHypha, lc *l } //line views/hypha.qtpl:109 -func AttachmentHTML(h *hyphae.MediaHypha, lc *l18n.Localizer) string { +func AttachmentHTML(h *hyphae.NonEmptyHypha, lc *l18n.Localizer) string { //line views/hypha.qtpl:109 qb422016 := qt422016.AcquireByteBuffer() //line views/hypha.qtpl:109 diff --git a/views/readers.qtpl b/views/readers.qtpl index 9d64e0a..e3a5beb 100644 --- a/views/readers.qtpl +++ b/views/readers.qtpl @@ -11,7 +11,7 @@ {% import "github.com/bouncepaw/mycorrhiza/user" %} {% import "github.com/bouncepaw/mycorrhiza/util" %} -{% func AttachmentMenuHTML(rq *http.Request, h *hyphae.MediaHypha, u *user.User) %} +{% func AttachmentMenuHTML(rq *http.Request, h *hyphae.NonEmptyHypha, u *user.User) %} {% code lc := l18n.FromRequest(rq) %} diff --git a/views/stuff.qtpl b/views/stuff.qtpl index 287ada6..9547ef2 100644 --- a/views/stuff.qtpl +++ b/views/stuff.qtpl @@ -262,7 +262,7 @@ sort.Strings(editors) close(hyphaNames) %} {% for hyphaName := range sortedHypha %} - {% code hypha := hyphae.ByName(hyphaName).(*hyphae.MediaHypha) %} + {% code hypha := hyphae.ByName(hyphaName).(*hyphae.NonEmptyHypha) %}