2021-05-09 10:42:12 +00:00
|
|
|
package web
|
2020-08-05 15:08:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-10-22 17:12:12 +00:00
|
|
|
"path/filepath"
|
2020-08-20 17:20:13 +00:00
|
|
|
"strings"
|
2020-08-05 15:08:59 +00:00
|
|
|
|
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"
|
2021-01-28 19:07:21 +00:00
|
|
|
"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"
|
2021-02-20 15:48:51 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
2020-08-05 15:08:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-01-30 18:29:56 +00:00
|
|
|
http.HandleFunc("/page/", handlerHypha)
|
|
|
|
http.HandleFunc("/hypha/", handlerHypha)
|
2020-08-05 15:08:59 +00:00
|
|
|
http.HandleFunc("/text/", handlerText)
|
|
|
|
http.HandleFunc("/binary/", handlerBinary)
|
2020-08-20 17:20:13 +00:00
|
|
|
http.HandleFunc("/rev/", handlerRevision)
|
2021-03-14 15:01:32 +00:00
|
|
|
http.HandleFunc("/primitive-diff/", handlerPrimitiveDiff)
|
2021-02-24 17:34:42 +00:00
|
|
|
http.HandleFunc("/attachment/", handlerAttachment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAttachment(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-02-24 17:34:42 +00:00
|
|
|
var (
|
2021-05-09 10:42:12 +00:00
|
|
|
hyphaName = util.HyphaNameFromRq(rq, "attachment")
|
2021-02-24 17:34:42 +00:00
|
|
|
h = hyphae.ByName(hyphaName)
|
|
|
|
u = user.FromRequest(rq)
|
|
|
|
)
|
|
|
|
util.HTTP200Page(w,
|
|
|
|
views.BaseHTML(
|
|
|
|
fmt.Sprintf("Attachment of %s", util.BeautifulName(hyphaName)),
|
|
|
|
views.AttachmentMenuHTML(rq, h, u),
|
|
|
|
u))
|
2020-08-20 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 15:01:32 +00:00
|
|
|
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-03-14 15:01:32 +00:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
util.HTTP200Page(w,
|
|
|
|
views.BaseHTML(
|
|
|
|
fmt.Sprintf("Diff of %s at %s", hyphaName, revHash),
|
|
|
|
views.PrimitiveDiffHTML(rq, h, u, revHash),
|
|
|
|
u))
|
|
|
|
}
|
|
|
|
|
2020-08-20 17:20:13 +00:00
|
|
|
// handlerRevision displays a specific revision of text part a page
|
|
|
|
func handlerRevision(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2020-08-20 17:20:13 +00:00
|
|
|
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:])
|
2021-02-20 15:48:51 +00:00
|
|
|
h = hyphae.ByName(hyphaName)
|
2020-08-20 17:20:13 +00:00
|
|
|
contents = fmt.Sprintf(`<p>This hypha had no text at this revision.</p>`)
|
2021-02-20 15:48:51 +00:00
|
|
|
textContents, err = history.FileAtRevision(h.TextPath, revHash)
|
2021-01-24 07:30:14 +00:00
|
|
|
u = user.FromRequest(rq)
|
2020-08-20 17:20:13 +00:00
|
|
|
)
|
|
|
|
if err == nil {
|
2020-12-17 12:59:59 +00:00
|
|
|
contents = markup.Doc(hyphaName, textContents).AsHTML()
|
2020-08-20 17:20:13 +00:00
|
|
|
}
|
2021-02-20 16:14:33 +00:00
|
|
|
page := views.RevisionHTML(
|
2020-11-16 15:26:03 +00:00
|
|
|
rq,
|
2021-02-20 15:48:51 +00:00
|
|
|
h,
|
2020-08-20 17:20:13 +00:00
|
|
|
contents,
|
2020-08-31 17:52:26 +00:00
|
|
|
revHash,
|
|
|
|
)
|
2020-08-20 17:20:13 +00:00
|
|
|
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-05-09 10:42:12 +00:00
|
|
|
w.Write([]byte(views.BaseHTML(util.BeautifulName(hyphaName), page, u)))
|
2020-08-19 18:54:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 15:08:59 +00:00
|
|
|
// handlerText serves raw source text of the hypha.
|
|
|
|
func handlerText(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
|
|
|
hyphaName := util.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)
|
2020-08-05 15:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerBinary serves binary part of the hypha.
|
|
|
|
func handlerBinary(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
|
|
|
hyphaName := util.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)
|
2020-08-05 15:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2020-08-05 15:08:59 +00:00
|
|
|
var (
|
2021-05-09 10:42:12 +00:00
|
|
|
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)
|
2020-08-05 15:08:59 +00:00
|
|
|
)
|
2021-02-17 18:41:35 +00:00
|
|
|
if h.Exists {
|
|
|
|
fileContentsT, errT := ioutil.ReadFile(h.TextPath)
|
|
|
|
_, errB := os.Stat(h.BinaryPath)
|
2020-08-05 15:08:59 +00:00
|
|
|
if errT == nil {
|
2020-12-17 12:59:59 +00:00
|
|
|
md := markup.Doc(hyphaName, string(fileContentsT))
|
|
|
|
contents = md.AsHTML()
|
|
|
|
openGraph = md.OpenGraphHTML()
|
2020-08-05 15:08:59 +00:00
|
|
|
}
|
|
|
|
if !os.IsNotExist(errB) {
|
2021-02-20 15:48:51 +00:00
|
|
|
contents = views.AttachmentHTML(h) + contents
|
2020-08-05 15:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-05 12:20:51 +00:00
|
|
|
if contents == "" {
|
|
|
|
util.HTTP404Page(w,
|
|
|
|
views.BaseHTML(
|
|
|
|
util.BeautifulName(hyphaName),
|
|
|
|
views.HyphaHTML(rq, h, contents),
|
|
|
|
u,
|
|
|
|
openGraph))
|
|
|
|
} else {
|
|
|
|
util.HTTP200Page(w,
|
|
|
|
views.BaseHTML(
|
|
|
|
util.BeautifulName(hyphaName),
|
|
|
|
views.HyphaHTML(rq, h, contents),
|
|
|
|
u,
|
|
|
|
openGraph))
|
|
|
|
}
|
2020-08-05 15:08:59 +00:00
|
|
|
}
|