1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-04-09 20:26:47 +00:00

Play with Hypha.BinaryPath

This commit is contained in:
Timur Ismagilov 2022-02-04 03:04:01 +05:00 committed by Timur Ismagilov
parent 927ac4f1da
commit 128f40288b
14 changed files with 52 additions and 44 deletions

View File

@ -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
}

View File

@ -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()
}

View File

@ -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()

View File

@ -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)
}

View File

@ -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()
}

View File

@ -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: <code>%v</code>", hop.Errs)), "Error"
}
if h.BinaryPath != "" {
h.BinaryPath = ""
if h.BinaryPath() != "" {
h.SetBinaryPath("")
}
// If nothing is left of the hypha
if h.TextPath == "" {

View File

@ -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)
}

View File

@ -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" %}
<div class="binary-container binary-container_with-img">

View File

@ -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

View File

@ -18,7 +18,7 @@
<div class="layout">
<main class="main-width attachment-tab">
<h1>{%s= lc.Get("ui.attach_title", &l18n.Replacements{"name": beautifulLink(h.Name)}) %}</h1>
{% if h.BinaryPath == "" %}
{% if h.BinaryPath() == "" %}
<p class="explanation">{%s lc.Get("ui.attach_empty") %} <a href="/help/en/attachment" class="shy-link">{%s lc.Get("ui.attach_link") %}</a></p>
{% else %}
<p class="explanation">{%s lc.Get("ui.attach_tip") %} <a href="/help/en/attachment" class="shy-link">{%s lc.Get("ui.attach_link") %}</a></p>
@ -26,10 +26,10 @@
<section class="amnt-grid">
{% 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 %}
<fieldset class="amnt-menu-block">
<legend class="modal__title modal__title_small">{%s lc.Get("ui.attach_stat") %}</legend>
@ -62,7 +62,7 @@
</form>
{% endif %}
{% if h.BinaryPath != "" && u.CanProceed("unattach-confirm") %}
{% if h.BinaryPath() != "" && u.CanProceed("unattach-confirm") %}
<form action="/unattach-confirm/{%s h.Name %}" method="post" class="modal amnt-menu-block">
<fieldset class="modal__fieldset">
<legend class="modal__title modal__title_small">{%s lc.Get("ui.attach_remove") %}</legend>

View File

@ -69,7 +69,7 @@ func StreamAttachmentMenuHTML(qw422016 *qt422016.Writer, rq *http.Request, h *hy
qw422016.N().S(`</h1>
`)
//line views/readers.qtpl:21
if h.BinaryPath == "" {
if h.BinaryPath() == "" {
//line views/readers.qtpl:21
qw422016.N().S(`
<p class="explanation">`)
@ -105,13 +105,13 @@ func StreamAttachmentMenuHTML(qw422016 *qt422016.Writer, rq *http.Request, h *hy
`)
//line views/readers.qtpl:29
if h.BinaryPath != "" {
if h.BinaryPath() != "" {
//line views/readers.qtpl:29
qw422016.N().S(`
`)
//line views/readers.qtpl:31
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())
//line views/readers.qtpl:32
qw422016.N().S(`
@ -225,7 +225,7 @@ func StreamAttachmentMenuHTML(qw422016 *qt422016.Writer, rq *http.Request, h *hy
`)
//line views/readers.qtpl:65
if h.BinaryPath != "" && u.CanProceed("unattach-confirm") {
if h.BinaryPath() != "" && u.CanProceed("unattach-confirm") {
//line views/readers.qtpl:65
qw422016.N().S(`
<form action="/unattach-confirm/`)

View File

@ -265,8 +265,8 @@ sort.Strings(editors)
{% code hypha := hyphae.ByName(hyphaName) %}
<li class="hypha-list__entry">
<a class="hypha-list__link" href="/hypha/{%s hypha.Name %}">{%s util.BeautifulName(hypha.Name) %}</a>
{% if hypha.BinaryPath != "" %}
<span class="hypha-list__amnt-type">{%s filepath.Ext(hypha.BinaryPath)[1:] %}</span>
{% if hypha.BinaryPath() != "" %}
<span class="hypha-list__amnt-type">{%s filepath.Ext(hypha.BinaryPath())[1:] %}</span>
{% endif %}
</li>
{% endfor %}

View File

@ -1036,12 +1036,12 @@ func StreamHyphaListHTML(qw422016 *qt422016.Writer, lc *l18n.Localizer) {
qw422016.N().S(`</a>
`)
//line views/stuff.qtpl:268
if hypha.BinaryPath != "" {
if hypha.BinaryPath() != "" {
//line views/stuff.qtpl:268
qw422016.N().S(`
<span class="hypha-list__amnt-type">`)
//line views/stuff.qtpl:269
qw422016.E().S(filepath.Ext(hypha.BinaryPath)[1:])
qw422016.E().S(filepath.Ext(hypha.BinaryPath())[1:])
//line views/stuff.qtpl:269
qw422016.N().S(`</span>
`)

View File

@ -150,9 +150,9 @@ func handlerBinary(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
hyphaName := util.HyphaNameFromRq(rq, "binary")
if h := hyphae.ByName(hyphaName); h.Exists {
log.Println("Serving", h.BinaryPath)
w.Header().Set("Content-Type", mimetype.FromExtension(filepath.Ext(h.BinaryPath)))
http.ServeFile(w, rq, h.BinaryPath)
log.Println("Serving", h.BinaryPath())
w.Header().Set("Content-Type", mimetype.FromExtension(filepath.Ext(h.BinaryPath())))
http.ServeFile(w, rq, h.BinaryPath())
}
}
@ -169,7 +169,7 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
)
if h.Exists {
fileContentsT, errT := os.ReadFile(h.TextPath)
_, errB := os.Stat(h.BinaryPath)
_, errB := os.Stat(h.BinaryPath())
if errT == nil {
ctx, _ := mycocontext.ContextFromStringInput(hyphaName, string(fileContentsT))
ctx = mycocontext.WithWebSiteURL(ctx, cfg.URL)