2020-08-05 15:08:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
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"
|
2020-10-30 13:25:48 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/markup"
|
2020-08-31 17:52:26 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/templates"
|
2020-08-05 20:19:14 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/tree"
|
2020-08-31 17:52:26 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2020-08-05 15:08:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
http.HandleFunc("/page/", handlerPage)
|
|
|
|
http.HandleFunc("/text/", handlerText)
|
|
|
|
http.HandleFunc("/binary/", handlerBinary)
|
2020-08-19 18:54:23 +00:00
|
|
|
http.HandleFunc("/history/", handlerHistory)
|
2020-08-20 17:20:13 +00:00
|
|
|
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]
|
|
|
|
hyphaName = CanonicalName(shorterUrl[firstSlashIndex+1:])
|
2020-08-20 17:20:13 +00:00
|
|
|
contents = fmt.Sprintf(`<p>This hypha had no text at this revision.</p>`)
|
2020-10-25 13:50:14 +00:00
|
|
|
textPath = hyphaName + ".myco"
|
2020-08-20 17:20:13 +00:00
|
|
|
textContents, err = history.FileAtRevision(textPath, revHash)
|
|
|
|
)
|
|
|
|
if err == nil {
|
2020-10-30 13:25:48 +00:00
|
|
|
contents = markup.ToHtml(hyphaName, textContents)
|
2020-08-20 17:20:13 +00:00
|
|
|
}
|
2020-11-29 11:32:52 +00:00
|
|
|
treeHTML, _, _ := tree.Tree(hyphaName, IterateHyphaNamesWith)
|
2020-08-31 17:52:26 +00:00
|
|
|
page := templates.RevisionHTML(
|
2020-11-16 15:26:03 +00:00
|
|
|
rq,
|
2020-08-31 17:52:26 +00:00
|
|
|
hyphaName,
|
2020-08-20 17:20:13 +00:00
|
|
|
naviTitle(hyphaName),
|
|
|
|
contents,
|
2020-11-29 11:32:52 +00:00
|
|
|
treeHTML,
|
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)
|
2020-08-31 17:52:26 +00:00
|
|
|
w.Write([]byte(base(hyphaName, page)))
|
2020-08-19 18:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlerHistory lists all revisions of a hypha
|
|
|
|
func handlerHistory(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
log.Println(rq.URL)
|
|
|
|
hyphaName := HyphaNameFromRq(rq, "history")
|
2020-11-29 17:06:45 +00:00
|
|
|
var list string
|
2020-09-29 15:04:22 +00:00
|
|
|
|
|
|
|
// History can be found for files that do not exist anymore.
|
|
|
|
revs, err := history.Revisions(hyphaName)
|
|
|
|
if err == nil {
|
2020-11-29 17:06:45 +00:00
|
|
|
list = history.HistoryWithRevisions(hyphaName, revs)
|
2020-08-19 18:54:23 +00:00
|
|
|
}
|
2020-09-29 15:04:22 +00:00
|
|
|
log.Println("Found", len(revs), "revisions for", hyphaName)
|
2020-08-19 18:54:23 +00:00
|
|
|
|
2020-08-31 17:52:26 +00:00
|
|
|
util.HTTP200Page(w,
|
2020-11-29 17:06:45 +00:00
|
|
|
base(hyphaName, templates.HistoryHTML(rq, hyphaName, list)))
|
2020-08-05 15:08:59 +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")
|
|
|
|
if data, ok := HyphaStorage[hyphaName]; ok {
|
|
|
|
log.Println("Serving", data.textPath)
|
2020-11-02 19:24:50 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
2020-08-05 15:08:59 +00:00
|
|
|
http.ServeFile(w, rq, data.textPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerBinary serves binary part of the hypha.
|
|
|
|
func handlerBinary(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
log.Println(rq.URL)
|
|
|
|
hyphaName := HyphaNameFromRq(rq, "binary")
|
|
|
|
if data, ok := HyphaStorage[hyphaName]; ok {
|
|
|
|
log.Println("Serving", data.binaryPath)
|
2020-10-22 17:12:12 +00:00
|
|
|
w.Header().Set("Content-Type", ExtensionToMime(filepath.Ext(data.binaryPath)))
|
2020-08-05 15:08:59 +00:00
|
|
|
http.ServeFile(w, rq, data.binaryPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerPage is the main hypha action that displays the hypha and the binary upload form along with some navigation.
|
|
|
|
func handlerPage(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
log.Println(rq.URL)
|
|
|
|
var (
|
|
|
|
hyphaName = HyphaNameFromRq(rq, "page")
|
|
|
|
data, hyphaExists = HyphaStorage[hyphaName]
|
2020-08-31 17:52:26 +00:00
|
|
|
contents string
|
2020-08-05 15:08:59 +00:00
|
|
|
)
|
|
|
|
if hyphaExists {
|
|
|
|
fileContentsT, errT := ioutil.ReadFile(data.textPath)
|
|
|
|
_, errB := os.Stat(data.binaryPath)
|
|
|
|
if errT == nil {
|
2020-10-30 13:25:48 +00:00
|
|
|
contents = markup.ToHtml(hyphaName, string(fileContentsT))
|
2020-08-05 15:08:59 +00:00
|
|
|
}
|
|
|
|
if !os.IsNotExist(errB) {
|
|
|
|
contents = binaryHtmlBlock(hyphaName, data) + contents
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 11:32:52 +00:00
|
|
|
treeHTML, prevHypha, nextHypha := tree.Tree(hyphaName, IterateHyphaNamesWith)
|
2020-11-16 15:26:03 +00:00
|
|
|
util.HTTP200Page(w, base(hyphaName, templates.PageHTML(rq, hyphaName,
|
2020-08-05 20:19:14 +00:00
|
|
|
naviTitle(hyphaName),
|
|
|
|
contents,
|
2020-11-29 11:32:52 +00:00
|
|
|
treeHTML, prevHypha, nextHypha)))
|
2020-08-05 15:08:59 +00:00
|
|
|
}
|