From 128f40288b50276d356c6e6ebb5dd769db41f164 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Fri, 4 Feb 2022 03:04:01 +0500 Subject: [PATCH] Play with Hypha.BinaryPath --- hyphae/files.go | 2 +- hyphae/hyphae.go | 17 ++++++++++------- shroom/delete.go | 2 +- shroom/init.go | 2 +- shroom/rename.go | 4 ++-- shroom/unattach.go | 6 +++--- shroom/upload.go | 23 ++++++++++++++--------- views/hypha.qtpl | 2 +- views/hypha.qtpl.go | 2 +- views/readers.qtpl | 10 +++++----- views/readers.qtpl.go | 10 +++++----- views/stuff.qtpl | 4 ++-- views/stuff.qtpl.go | 4 ++-- web/readers.go | 8 ++++---- 14 files changed, 52 insertions(+), 44 deletions(-) diff --git a/hyphae/files.go b/hyphae/files.go index 6f80318..131d5ac 100644 --- a/hyphae/files.go +++ b/hyphae/files.go @@ -57,7 +57,7 @@ func indexHelper(path string, nestLevel uint, ch chan *Hypha) { if isText { hypha.TextPath = hyphaPartPath } else { - hypha.BinaryPath = hyphaPartPath + hypha.SetBinaryPath(hyphaPartPath) } ch <- hypha } diff --git a/hyphae/hyphae.go b/hyphae/hyphae.go index 105b924..f1a8d74 100644 --- a/hyphae/hyphae.go +++ b/hyphae/hyphae.go @@ -34,9 +34,12 @@ type Hypha struct { Name string // Canonical name Exists bool TextPath string // == "" => no text part - BinaryPath string // == "" => no attachment + binaryPath string // == "" => no attachment } +func (h *Hypha) BinaryPath() string { return h.binaryPath } +func (h *Hypha) SetBinaryPath(s string) { h.binaryPath = s } + func (h *Hypha) CanonicalName() string { return h.Name } @@ -69,7 +72,7 @@ func (h *Hypha) TextPartPath() string { // HasAttachment is true if the hypha has an attachment. func (h *Hypha) HasAttachment() bool { - return h.BinaryPath != "" + return h.binaryPath != "" } var byNames = make(map[string]*Hypha) @@ -81,7 +84,7 @@ func EmptyHypha(hyphaName string) *Hypha { Name: hyphaName, Exists: false, TextPath: "", - BinaryPath: "", + binaryPath: "", } } @@ -145,11 +148,11 @@ func (h *Hypha) mergeIn(oh *Hypha) { if h.TextPath == "" && oh.TextPath != "" { h.TextPath = oh.TextPath } - if oh.BinaryPath != "" { - if h.BinaryPath != "" { - log.Println("There is a file collision for attachment of a hypha:", h.BinaryPath, "and", oh.BinaryPath, "-- going on with the latter") + if oh.binaryPath != "" { + if h.binaryPath != "" { + log.Println("There is a file collision for attachment of a hypha:", h.binaryPath, "and", oh.binaryPath, "-- going on with the latter") } - h.BinaryPath = oh.BinaryPath + h.binaryPath = oh.binaryPath } h.Unlock() } diff --git a/shroom/delete.go b/shroom/delete.go index 37b538b..ecf4939 100644 --- a/shroom/delete.go +++ b/shroom/delete.go @@ -21,7 +21,7 @@ func DeleteHypha(u *user.User, h *hyphae.Hypha, lc *l18n.Localizer) (hop *histor originalText, _ := FetchTextPart(h) hop. - WithFilesRemoved(h.TextPath, h.BinaryPath). + WithFilesRemoved(h.TextPath, h.BinaryPath()). WithMsg(fmt.Sprintf("Delete ā€˜%s’", h.Name)). WithUser(u). Apply() diff --git a/shroom/init.go b/shroom/init.go index d5bdd82..f441697 100644 --- a/shroom/init.go +++ b/shroom/init.go @@ -16,7 +16,7 @@ func init() { globals.HyphaAccess = func(hyphaName string) (rawText, binaryBlock string, err error) { if h := hyphae.ByName(hyphaName); h.Exists { rawText, err = FetchTextPart(h) - if h.BinaryPath != "" { + if h.BinaryPath() != "" { // 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 f7645dd..da2bc91 100644 --- a/shroom/rename.go +++ b/shroom/rename.go @@ -74,7 +74,7 @@ func RenameHypha(h hyphae.Hypher, newHypha hyphae.Hypher, recursive bool, u *use h.RenameTo(replaceName(h.CanonicalName())) h.Lock() h.TextPath = replaceName(h.TextPath) - h.BinaryPath = replaceName(h.BinaryPath) + h.SetBinaryPath(replaceName(h.BinaryPath())) h.Unlock() backlinks.UpdateBacklinksAfterRename(h, oldName) } @@ -101,7 +101,7 @@ func renamingPairs(hyphaeToRename []hyphae.Hypher, replaceName func(string) stri } if h.Kind() == hyphae.HyphaMedia { // ontology think h := h.(*hyphae.Hypha) - renameMap[h.BinaryPath] = replaceName(h.BinaryPath) + renameMap[h.BinaryPath()] = replaceName(h.BinaryPath()) } h.Unlock() } diff --git a/shroom/unattach.go b/shroom/unattach.go index 41452ba..fcc09ab 100644 --- a/shroom/unattach.go +++ b/shroom/unattach.go @@ -19,7 +19,7 @@ func UnattachHypha(u *user.User, h *hyphae.Hypha, lc *l18n.Localizer) (hop *hist } hop. - WithFilesRemoved(h.BinaryPath). + WithFilesRemoved(h.BinaryPath()). WithMsg(fmt.Sprintf("Unattach ā€˜%s’", h.Name)). WithUser(u). Apply() @@ -30,8 +30,8 @@ func UnattachHypha(u *user.User, h *hyphae.Hypha, lc *l18n.Localizer) (hop *hist return hop.WithErrAbort(fmt.Errorf("Could not unattach this hypha due to internal server errors: %v", hop.Errs)), "Error" } - if h.BinaryPath != "" { - h.BinaryPath = "" + if h.BinaryPath() != "" { + h.SetBinaryPath("") } // If nothing is left of the hypha if h.TextPath == "" { diff --git a/shroom/upload.go b/shroom/upload.go index a61002b..41d6b79 100644 --- a/shroom/upload.go +++ b/shroom/upload.go @@ -39,7 +39,7 @@ func UploadText(h *hyphae.Hypha, data []byte, message string, u *user.User, lc * if errtitle, err := CanEdit(u, h, lc); err != nil { return hop.WithErrAbort(err), errtitle } - if len(bytes.TrimSpace(data)) == 0 && h.BinaryPath == "" { + if len(bytes.TrimSpace(data)) == 0 && h.BinaryPath() == "" { return hop.WithErrAbort(errors.New("No data passed")), "Empty" } @@ -69,16 +69,16 @@ func UploadBinary(h *hyphae.Hypha, mime string, file multipart.File, u *user.Use // uploadHelp is a helper function for UploadText and UploadBinary func uploadHelp(h *hyphae.Hypha, hop *history.Op, ext string, data []byte, u *user.User) (*history.Op, string) { var ( - fullPath = filepath.Join(files.HyphaeDir(), h.Name+ext) - originalFullPath = &h.TextPath - originalText = "" // for backlink update + fullPath = filepath.Join(files.HyphaeDir(), h.Name+ext) + sourceFullPath = h.TextPartPath() + originalText = "" // for backlink update ) if !isValidPath(fullPath) || !hyphae.IsValidName(h.Name) { err := errors.New("bad path") return hop.WithErrAbort(err), err.Error() } if hop.Type == history.TypeEditBinary { - originalFullPath = &h.BinaryPath + sourceFullPath = h.BinaryPath() } if err := os.MkdirAll(filepath.Dir(fullPath), 0777); err != nil { @@ -93,18 +93,23 @@ func uploadHelp(h *hyphae.Hypha, hop *history.Op, ext string, data []byte, u *us return hop.WithErrAbort(err), err.Error() } - if h.Exists && *originalFullPath != fullPath && *originalFullPath != "" { - if err := history.Rename(*originalFullPath, fullPath); err != nil { + if h.Exists && sourceFullPath != fullPath && sourceFullPath != "" { + if err := history.Rename(sourceFullPath, fullPath); err != nil { return hop.WithErrAbort(err), err.Error() } - log.Println("Move", *originalFullPath, "to", fullPath) + log.Println("Move", sourceFullPath, "to", fullPath) } h.InsertIfNew() if h.Exists && h.TextPath != "" && hop.Type == history.TypeEditText && !history.FileChanged(fullPath) { return hop.Abort(), "No changes" } - *originalFullPath = fullPath + // TODO: test + if hop.Type == history.TypeEditBinary { + h.SetBinaryPath(fullPath) + } else { + h.TextPath = fullPath + } if hop.Type == history.TypeEditText { backlinks.UpdateBacklinksAfterEdit(h, originalText) } diff --git a/views/hypha.qtpl b/views/hypha.qtpl index db34a9e..265e513 100644 --- a/views/hypha.qtpl +++ b/views/hypha.qtpl @@ -78,7 +78,7 @@ {% func AttachmentHTMLRaw(h *hyphae.Hypha) %}{%= AttachmentHTML(h, l18n.New("en", "en")) %}{% endfunc %} {% func AttachmentHTML(h *hyphae.Hypha, lc *l18n.Localizer) %} - {% switch filepath.Ext(h.BinaryPath) %} + {% 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 40c506c..1ada2b9 100644 --- a/views/hypha.qtpl.go +++ b/views/hypha.qtpl.go @@ -387,7 +387,7 @@ func StreamAttachmentHTML(qw422016 *qt422016.Writer, h *hyphae.Hypha, lc *l18n.L qw422016.N().S(` `) //line views/hypha.qtpl:81 - switch filepath.Ext(h.BinaryPath) { + switch filepath.Ext(h.BinaryPath()) { //line views/hypha.qtpl:83 case ".jpg", ".gif", ".png", ".webp", ".svg", ".ico": //line views/hypha.qtpl:83 diff --git a/views/readers.qtpl b/views/readers.qtpl index 350a165..acece25 100644 --- a/views/readers.qtpl +++ b/views/readers.qtpl @@ -18,7 +18,7 @@

{%s= lc.Get("ui.attach_title", &l18n.Replacements{"name": beautifulLink(h.Name)}) %}

- {% if h.BinaryPath == "" %} + {% if h.BinaryPath() == "" %}

{%s lc.Get("ui.attach_empty") %} {%s lc.Get("ui.attach_link") %}

{% else %}

{%s lc.Get("ui.attach_tip") %} {%s lc.Get("ui.attach_link") %}

@@ -26,10 +26,10 @@
- {% if h.BinaryPath != "" %} + {% if h.BinaryPath() != "" %} {% code - mime := mimetype.FromExtension(path.Ext(h.BinaryPath)) - fileinfo, err := os.Stat(h.BinaryPath) %} + mime := mimetype.FromExtension(path.Ext(h.BinaryPath())) + fileinfo, err := os.Stat(h.BinaryPath()) %} {% if err == nil %}
{%s lc.Get("ui.attach_stat") %} @@ -62,7 +62,7 @@ {% endif %} - {% if h.BinaryPath != "" && u.CanProceed("unattach-confirm") %} + {% if h.BinaryPath() != "" && u.CanProceed("unattach-confirm") %}