mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-04 18:19:54 +00:00
Rename MediaHypha to NonEmptyHypha
This commit is contained in:
parent
002e9f7a93
commit
6fc5cf994e
@ -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
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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 != ""
|
||||
}
|
52
hyphae/non_empty_hypha.go
Normal file
52
hyphae/non_empty_hypha.go
Normal file
@ -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 != ""
|
||||
}
|
@ -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")
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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()).
|
||||
|
@ -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(), ""
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -75,9 +75,9 @@
|
||||
</h1>
|
||||
{% 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" %}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
%}
|
||||
|
@ -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) %}
|
||||
<li class="hypha-list__entry">
|
||||
<a class="hypha-list__link" href="/hypha/{%s hypha.CanonicalName() %}">
|
||||
{%s util.BeautifulName(hypha.CanonicalName()) %}
|
||||
|
@ -47,7 +47,7 @@ func handlerAttachment(w http.ResponseWriter, rq *http.Request) {
|
||||
util.HTTP200Page(w,
|
||||
views.BaseHTML(
|
||||
lc.Get("ui.attach_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName)}),
|
||||
views.AttachmentMenuHTML(rq, h.(*hyphae.MediaHypha), u),
|
||||
views.AttachmentMenuHTML(rq, h.(*hyphae.NonEmptyHypha), u),
|
||||
lc,
|
||||
u))
|
||||
}
|
||||
@ -154,7 +154,7 @@ func handlerBinary(w http.ResponseWriter, rq *http.Request) {
|
||||
switch h := hyphae.ByName(hyphaName).(type) {
|
||||
case *hyphae.EmptyHypha:
|
||||
default: // TODO: deindent
|
||||
switch h := h.(*hyphae.MediaHypha); h.Kind() {
|
||||
switch h := h.(*hyphae.NonEmptyHypha); h.Kind() {
|
||||
case hyphae.HyphaText:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
log.Printf("Textual hypha ‘%s’ has no media, cannot serve\n", h.CanonicalName())
|
||||
@ -187,7 +187,7 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
|
||||
lc,
|
||||
u,
|
||||
openGraph))
|
||||
case *hyphae.MediaHypha:
|
||||
case *hyphae.NonEmptyHypha:
|
||||
fileContentsT, errT := os.ReadFile(h.TextPartPath())
|
||||
if errT == nil {
|
||||
ctx, _ := mycocontext.ContextFromStringInput(hyphaName, string(fileContentsT))
|
||||
|
Loading…
Reference in New Issue
Block a user