1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00
mycorrhiza/hyphae/view.go
2021-02-17 23:41:35 +05:00

73 lines
1.9 KiB
Go

package hyphae
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/bouncepaw/mycorrhiza/markup"
"github.com/bouncepaw/mycorrhiza/util"
)
// FetchTextPart tries to read text file of the given hypha. If there is no file, empty string is returned.
func (h *Hypha) FetchTextPart() (string, error) {
if h.TextPath == "" {
return "", nil
}
text, err := ioutil.ReadFile(h.TextPath)
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", err
}
return string(text), nil
}
// binaryHtmlBlock creates an html block for binary part of the hypha.
func (h *Hypha) BinaryHtmlBlock() string {
switch filepath.Ext(h.BinaryPath) {
case ".jpg", ".gif", ".png", ".webp", ".svg", ".ico":
return fmt.Sprintf(`
<div class="binary-container binary-container_with-img">
<a href="/binary/%[1]s"><img src="/binary/%[1]s"/></a>
</div>`, h.Name)
case ".ogg", ".webm", ".mp4":
return fmt.Sprintf(`
<div class="binary-container binary-container_with-video">
<video>
<source src="/binary/%[1]s"/>
<p>Your browser does not support video. <a href="/binary/%[1]s">Download video</a></p>
</video>
`, h.Name)
case ".mp3":
return fmt.Sprintf(`
<div class="binary-container binary-container_with-audio">
<audio>
<source src="/binary/%[1]s"/>
<p>Your browser does not support audio. <a href="/binary/%[1]s">Download audio</a></p>
</audio>
`, h.Name)
default:
return fmt.Sprintf(`
<div class="binary-container binary-container_with-nothing">
<p><a href="/binary/%s">Download media</a></p>
</div>
`, h.Name)
}
}
func SetHeaderLinks() {
if userLinksHypha := ByName(util.HeaderLinksHypha); !userLinksHypha.Exists {
util.SetDefaultHeaderLinks()
} else {
contents, err := ioutil.ReadFile(userLinksHypha.TextPath)
if err != nil || len(contents) == 0 {
util.SetDefaultHeaderLinks()
} else {
text := string(contents)
util.ParseHeaderLinks(text, markup.Rocketlink)
}
}
}