1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/http_readers.go

108 lines
3.1 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
2020-08-19 18:54:23 +00:00
"github.com/bouncepaw/mycorrhiza/history"
2021-02-17 18:41:35 +00:00
"github.com/bouncepaw/mycorrhiza/hyphae"
2020-10-30 13:25:48 +00:00
"github.com/bouncepaw/mycorrhiza/markup"
"github.com/bouncepaw/mycorrhiza/mimetype"
2020-08-31 17:52:26 +00:00
"github.com/bouncepaw/mycorrhiza/templates"
2021-01-24 07:30:14 +00:00
"github.com/bouncepaw/mycorrhiza/user"
2020-08-31 17:52:26 +00:00
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views"
)
func init() {
2021-01-30 18:29:56 +00:00
http.HandleFunc("/page/", handlerHypha)
http.HandleFunc("/hypha/", handlerHypha)
http.HandleFunc("/text/", handlerText)
http.HandleFunc("/binary/", handlerBinary)
http.HandleFunc("/rev/", handlerRevision)
}
// handlerRevision displays a specific revision of text part a page
func handlerRevision(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
var (
shorterUrl = strings.TrimPrefix(rq.URL.Path, "/rev/")
2020-09-29 15:04:22 +00:00
firstSlashIndex = strings.IndexRune(shorterUrl, '/')
revHash = shorterUrl[:firstSlashIndex]
2021-02-17 18:41:35 +00:00
hyphaName = util.CanonicalName(shorterUrl[firstSlashIndex+1:])
h = hyphae.ByName(hyphaName)
contents = fmt.Sprintf(`<p>This hypha had no text at this revision.</p>`)
textContents, err = history.FileAtRevision(h.TextPath, revHash)
2021-01-24 07:30:14 +00:00
u = user.FromRequest(rq)
)
if err == nil {
2020-12-17 12:59:59 +00:00
contents = markup.Doc(hyphaName, textContents).AsHTML()
}
2021-02-20 16:14:33 +00:00
page := views.RevisionHTML(
rq,
h,
contents,
2020-08-31 17:52:26 +00:00
revHash,
)
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(base(util.BeautifulName(hyphaName), page, u)))
2020-08-19 18:54:23 +00:00
}
// handlerText serves raw source text of the hypha.
func handlerText(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
hyphaName := HyphaNameFromRq(rq, "text")
2021-02-17 18:41:35 +00:00
if h := hyphae.ByName(hyphaName); h.Exists {
log.Println("Serving", h.TextPath)
2020-11-02 19:24:50 +00:00
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
2021-02-17 18:41:35 +00:00
http.ServeFile(w, rq, h.TextPath)
}
}
// handlerBinary serves binary part of the hypha.
func handlerBinary(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
hyphaName := HyphaNameFromRq(rq, "binary")
2021-02-17 18:41:35 +00:00
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)
}
}
2021-01-30 18:29:56 +00:00
// handlerHypha is the main hypha action that displays the hypha and the binary upload form along with some navigation.
func handlerHypha(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
var (
2021-02-17 18:41:35 +00:00
hyphaName = HyphaNameFromRq(rq, "page", "hypha")
h = hyphae.ByName(hyphaName)
contents string
openGraph string
u = user.FromRequest(rq)
)
2021-02-17 18:41:35 +00:00
if h.Exists {
fileContentsT, errT := ioutil.ReadFile(h.TextPath)
_, errB := os.Stat(h.BinaryPath)
if errT == nil {
2020-12-17 12:59:59 +00:00
md := markup.Doc(hyphaName, string(fileContentsT))
contents = md.AsHTML()
openGraph = md.OpenGraphHTML()
}
if !os.IsNotExist(errB) {
contents = views.AttachmentHTML(h) + contents
}
}
2020-12-17 12:59:59 +00:00
util.HTTP200Page(w,
templates.BaseHTML(
util.BeautifulName(hyphaName),
2021-02-20 16:14:33 +00:00
views.HyphaHTML(rq, h, contents),
2021-01-24 07:30:14 +00:00
u,
2020-12-17 12:59:59 +00:00
openGraph))
}