From ee233b95777542a9fcf51f684f6e2d655ee35d16 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Fri, 4 Feb 2022 22:25:07 +0500 Subject: [PATCH] Fix textual hypha creation --- hyphae/files.go | 2 +- hyphae/interface.go | 6 +++--- shroom/upload.go | 26 +++++++++++--------------- views/readers.qtpl.go | 6 +++--- views/stuff.qtpl.go | 2 +- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/hyphae/files.go b/hyphae/files.go index 9541260..423bc79 100644 --- a/hyphae/files.go +++ b/hyphae/files.go @@ -21,7 +21,7 @@ func Index(path string) { for nh := range ch { switch oh := ByName(nh.CanonicalName()).(type) { case *EmptyHypha: - insert(nh) + Insert(nh) default: // In case of conflicts the newer hypha overwrites the previous switch nh, oh := nh.(*NonEmptyHypha), oh.(*NonEmptyHypha); { diff --git a/hyphae/interface.go b/hyphae/interface.go index 801a178..1d6c9d0 100644 --- a/hyphae/interface.go +++ b/hyphae/interface.go @@ -60,8 +60,8 @@ func RenameHyphaTo(h Hypher, newName string) { h.Unlock() } -// insert inserts the hypha into the storage, possibly overwriting the previous hypha with the same name. Count incrementation is done if needed. -func insert(h Hypher) (madeNewRecord bool) { +// Insert inserts the hypha into the storage, possibly overwriting the previous hypha with the same name. Count incrementation is done if needed. +func Insert(h Hypher) (madeNewRecord bool) { _, recorded := byNames[h.CanonicalName()] byNamesMutex.Lock() @@ -79,7 +79,7 @@ func insert(h Hypher) (madeNewRecord bool) { func InsertIfNew(h Hypher) (madeNewRecord bool) { switch ByName(h.CanonicalName()).(type) { case *EmptyHypha: - return insert(h) + return Insert(h) default: return false } diff --git a/shroom/upload.go b/shroom/upload.go index 39d34f7..709c925 100644 --- a/shroom/upload.go +++ b/shroom/upload.go @@ -35,13 +35,7 @@ func historyMessageForTextUpload(h hyphae.Hypher, userMessage string) string { return fmt.Sprintf("%s ā€˜%sā€™: %s", verb, h.CanonicalName(), userMessage) } -func writeTextToDiskForEmptyHypha(eh *hyphae.EmptyHypha, data []byte) error { - h := hyphae.FillEmptyHyphaUpToTextualHypha(eh, filepath.Join(files.HyphaeDir(), eh.CanonicalName()+".myco")) - - return writeTextToDiskForNonEmptyHypha(h, data) -} - -func writeTextToDiskForNonEmptyHypha(h *hyphae.NonEmptyHypha, data []byte) error { +func writeTextToDisk(h *hyphae.NonEmptyHypha, data []byte, hop *history.Op) error { if err := os.MkdirAll(filepath.Dir(h.TextPartPath()), 0777); err != nil { return err } @@ -49,6 +43,8 @@ func writeTextToDiskForNonEmptyHypha(h *hyphae.NonEmptyHypha, data []byte) error if err := os.WriteFile(h.TextPartPath(), data, 0666); err != nil { return err } + hop.WithFiles(h.TextPartPath()) + return nil } @@ -56,7 +52,8 @@ func writeTextToDiskForNonEmptyHypha(h *hyphae.NonEmptyHypha, data []byte) error func UploadText(h hyphae.Hypher, data []byte, userMessage string, u *user.User, lc *l18n.Localizer) (hop *history.Op, errtitle string) { hop = history. Operation(history.TypeEditText). - WithMsg(historyMessageForTextUpload(h, userMessage)) + WithMsg(historyMessageForTextUpload(h, userMessage)). + WithUser(u) // Privilege check if !u.CanProceed("upload-text") { @@ -93,12 +90,14 @@ func UploadText(h hyphae.Hypher, data []byte, userMessage string, u *user.User, switch h := h.(type) { case *hyphae.EmptyHypha: - err := writeTextToDiskForEmptyHypha(h, data) + H := hyphae.FillEmptyHyphaUpToTextualHypha(h, filepath.Join(files.HyphaeDir(), h.CanonicalName()+".myco")) + + err := writeTextToDisk(H, data, hop) if err != nil { return hop.WithErrAbort(err), err.Error() } - hyphae.InsertIfNew(h) + hyphae.Insert(H) case *hyphae.NonEmptyHypha: oldText, err := FetchTextPart(h) if err != nil { @@ -111,7 +110,7 @@ func UploadText(h hyphae.Hypher, data []byte, userMessage string, u *user.User, return hop.Abort(), "" } - err = writeTextToDiskForNonEmptyHypha(h, data) + err = writeTextToDisk(h, data, hop) if err != nil { return hop.WithErrAbort(err), err.Error() } @@ -119,10 +118,7 @@ func UploadText(h hyphae.Hypher, data []byte, userMessage string, u *user.User, backlinks.UpdateBacklinksAfterEdit(h, oldText) } - return hop. - WithFiles(h.TextPartPath()). - WithUser(u). - Apply(), "" + return hop.Apply(), "" } // UploadBinary edits the hypha's media part and makes a history record about that. diff --git a/views/readers.qtpl.go b/views/readers.qtpl.go index 5865d82..62e974f 100644 --- a/views/readers.qtpl.go +++ b/views/readers.qtpl.go @@ -51,7 +51,7 @@ var ( ) //line views/readers.qtpl:14 -func StreamAttachmentMenuHTML(qw422016 *qt422016.Writer, rq *http.Request, h *hyphae.MediaHypha, u *user.User) { +func StreamAttachmentMenuHTML(qw422016 *qt422016.Writer, rq *http.Request, h *hyphae.NonEmptyHypha, u *user.User) { //line views/readers.qtpl:14 qw422016.N().S(` `) @@ -265,7 +265,7 @@ func StreamAttachmentMenuHTML(qw422016 *qt422016.Writer, rq *http.Request, h *hy } //line views/readers.qtpl:78 -func WriteAttachmentMenuHTML(qq422016 qtio422016.Writer, rq *http.Request, h *hyphae.MediaHypha, u *user.User) { +func WriteAttachmentMenuHTML(qq422016 qtio422016.Writer, rq *http.Request, h *hyphae.NonEmptyHypha, u *user.User) { //line views/readers.qtpl:78 qw422016 := qt422016.AcquireWriter(qq422016) //line views/readers.qtpl:78 @@ -276,7 +276,7 @@ func WriteAttachmentMenuHTML(qq422016 qtio422016.Writer, rq *http.Request, h *hy } //line views/readers.qtpl:78 -func AttachmentMenuHTML(rq *http.Request, h *hyphae.MediaHypha, u *user.User) string { +func AttachmentMenuHTML(rq *http.Request, h *hyphae.NonEmptyHypha, u *user.User) string { //line views/readers.qtpl:78 qb422016 := qt422016.AcquireByteBuffer() //line views/readers.qtpl:78 diff --git a/views/stuff.qtpl.go b/views/stuff.qtpl.go index e2321a5..25d21c6 100644 --- a/views/stuff.qtpl.go +++ b/views/stuff.qtpl.go @@ -1020,7 +1020,7 @@ func StreamHyphaListHTML(qw422016 *qt422016.Writer, lc *l18n.Localizer) { qw422016.N().S(` `) //line views/stuff.qtpl:265 - hypha := hyphae.ByName(hyphaName).(*hyphae.MediaHypha) + hypha := hyphae.ByName(hyphaName).(*hyphae.NonEmptyHypha) //line views/stuff.qtpl:265 qw422016.N().S(`