1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00
mycorrhiza/web/readers.go

215 lines
6.8 KiB
Go
Raw Normal View History

package web
import (
"fmt"
2021-09-23 09:36:54 +00:00
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gorilla/mux"
"github.com/bouncepaw/mycorrhiza/cfg"
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"
2021-09-06 17:46:34 +00:00
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/mimetype"
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"
2021-05-25 07:11:16 +00:00
"github.com/bouncepaw/mycomarkup/v3"
"github.com/bouncepaw/mycomarkup/v3/mycocontext"
"github.com/bouncepaw/mycomarkup/v3/tools"
)
func initReaders(r *mux.Router) {
r.PathPrefix("/page/").HandlerFunc(handlerHypha)
r.PathPrefix("/hypha/").HandlerFunc(handlerHypha)
r.PathPrefix("/text/").HandlerFunc(handlerText)
r.PathPrefix("/binary/").HandlerFunc(handlerBinary)
r.PathPrefix("/rev/").HandlerFunc(handlerRevision)
2021-09-23 09:36:54 +00:00
r.PathPrefix("/rev-text/").HandlerFunc(handlerRevisionText)
r.PathPrefix("/primitive-diff/").HandlerFunc(handlerPrimitiveDiff)
r.PathPrefix("/attachment/").HandlerFunc(handlerAttachment)
2021-02-24 17:34:42 +00:00
}
func handlerAttachment(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
2021-02-24 17:34:42 +00:00
var (
hyphaName = util.HyphaNameFromRq(rq, "attachment")
2021-02-24 17:34:42 +00:00
h = hyphae.ByName(hyphaName)
u = user.FromRequest(rq)
2021-09-06 17:46:34 +00:00
lc = l18n.FromRequest(rq)
2021-02-24 17:34:42 +00:00
)
util.HTTP200Page(w,
views.BaseHTML(
2021-09-06 17:46:34 +00:00
lc.Get("ui.attach_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName)}),
2022-02-04 17:06:37 +00:00
views.AttachmentMenuHTML(rq, h.(*hyphae.NonEmptyHypha), u),
2021-09-06 17:46:34 +00:00
lc,
2021-02-24 17:34:42 +00:00
u))
}
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
shorterURL = strings.TrimPrefix(rq.URL.Path, "/primitive-diff/")
firstSlashIndex = strings.IndexRune(shorterURL, '/')
revHash = shorterURL[:firstSlashIndex]
hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:])
h = hyphae.ByName(hyphaName)
u = user.FromRequest(rq)
2021-09-06 17:46:34 +00:00
lc = l18n.FromRequest(rq)
)
util.HTTP200Page(w,
views.BaseHTML(
2021-09-06 17:46:34 +00:00
lc.Get("ui.diff_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
views.PrimitiveDiffHTML(rq, h, u, revHash),
2021-09-06 17:46:34 +00:00
lc,
u))
}
2021-09-23 09:36:54 +00:00
// handlerRevisionText sends Mycomarkup text of the hypha at the given revision. See also: handlerRevision, handlerText.
//
// /rev-text/<revHash>/<hyphaName>
func handlerRevisionText(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
shorterURL = strings.TrimPrefix(rq.URL.Path, "/rev-text/")
firstSlashIndex = strings.IndexRune(shorterURL, '/')
revHash = shorterURL[:firstSlashIndex]
hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:])
2021-09-23 09:36:54 +00:00
h = hyphae.ByName(hyphaName)
textContents, err = history.FileAtRevision(h.TextPartPath(), revHash)
)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
if err != nil {
w.WriteHeader(http.StatusNotFound)
log.Printf("While serving text of %s at revision %s: %s\n", hyphaName, revHash, err.Error())
_, _ = io.WriteString(w, "Error: "+err.Error())
return
}
log.Printf("Serving text of %s from %s at revision %s\n", hyphaName, h.TextPartPath(), revHash)
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, textContents)
}
// handlerRevision displays a specific revision of the text part the hypha
func handlerRevision(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
2021-09-06 17:46:34 +00:00
lc = l18n.FromRequest(rq)
shorterURL = strings.TrimPrefix(rq.URL.Path, "/rev/")
firstSlashIndex = strings.IndexRune(shorterURL, '/')
revHash = shorterURL[:firstSlashIndex]
hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:])
h = hyphae.ByName(hyphaName)
2021-09-06 17:46:34 +00:00
contents = fmt.Sprintf(`<p>%s</p>`, lc.Get("ui.revision_no_text"))
2021-07-30 14:16:58 +00:00
textContents, err = history.FileAtRevision(h.TextPartPath(), revHash)
2021-01-24 07:30:14 +00:00
u = user.FromRequest(rq)
)
if err == nil {
2021-05-25 07:11:16 +00:00
ctx, _ := mycocontext.ContextFromStringInput(hyphaName, textContents)
contents = mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx))
}
2021-02-20 16:14:33 +00:00
page := views.RevisionHTML(
rq,
2021-09-06 17:46:34 +00:00
lc,
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)
2021-08-12 12:12:53 +00:00
_, _ = fmt.Fprint(
w,
views.BaseHTML(
2021-09-06 17:46:34 +00:00
lc.Get("ui.revision_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
2021-08-12 12:12:53 +00:00
page,
2021-09-06 17:46:34 +00:00
lc,
2021-08-12 12:12:53 +00:00
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) {
util.PrepareRq(rq)
hyphaName := util.HyphaNameFromRq(rq, "text")
switch h := hyphae.ByName(hyphaName).(type) {
case *hyphae.EmptyHypha:
2022-02-06 13:14:49 +00:00
case *hyphae.NonEmptyHypha:
if h.Kind() == hyphae.HyphaText {
log.Println("Serving", h.TextPartPath())
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.ServeFile(w, rq, h.TextPartPath())
}
}
}
// handlerBinary serves attachment of the hypha.
func handlerBinary(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
hyphaName := util.HyphaNameFromRq(rq, "binary")
switch h := hyphae.ByName(hyphaName).(type) {
case *hyphae.EmptyHypha:
default: // TODO: deindent
2022-02-04 17:06:37 +00:00
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())
default:
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) {
util.PrepareRq(rq)
var (
hyphaName = util.HyphaNameFromRq(rq, "page", "hypha")
2021-02-17 18:41:35 +00:00
h = hyphae.ByName(hyphaName)
contents string
openGraph string
u = user.FromRequest(rq)
2021-09-06 17:46:34 +00:00
lc = l18n.FromRequest(rq)
)
switch h := h.(type) {
case *hyphae.EmptyHypha:
util.HTTP404Page(w,
views.BaseHTML(
util.BeautifulName(hyphaName),
views.HyphaHTML(rq, lc, h, contents),
lc,
u,
openGraph))
2022-02-04 17:06:37 +00:00
case *hyphae.NonEmptyHypha:
fileContentsT, errT := os.ReadFile(h.TextPartPath())
if errT == nil {
2021-05-25 07:11:16 +00:00
ctx, _ := mycocontext.ContextFromStringInput(hyphaName, string(fileContentsT))
ctx = mycocontext.WithWebSiteURL(ctx, cfg.URL)
getOpenGraph, descVisitor, imgVisitor := tools.OpenGraphVisitors(ctx)
ast := mycomarkup.BlockTree(ctx, descVisitor, imgVisitor)
2021-05-25 07:11:16 +00:00
contents = mycomarkup.BlocksToHTML(ctx, ast)
openGraph = getOpenGraph()
}
if h.Kind() == hyphae.HyphaMedia {
contents = views.AttachmentHTML(h, lc) + contents
}
util.HTTP200Page(w,
views.BaseHTML(
util.BeautifulName(hyphaName),
2021-09-06 17:46:34 +00:00
views.HyphaHTML(rq, lc, h, contents),
lc,
u,
openGraph))
}
}