2020-06-13 11:18:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-06-16 18:35:52 +00:00
|
|
|
"log"
|
2020-06-20 15:17:13 +00:00
|
|
|
"mime"
|
2020-06-16 18:35:52 +00:00
|
|
|
"net/http"
|
2020-06-19 13:03:31 +00:00
|
|
|
"strconv"
|
2020-06-19 15:17:00 +00:00
|
|
|
|
2020-06-19 14:30:19 +00:00
|
|
|
"github.com/gomarkdown/markdown"
|
2020-06-13 11:18:11 +00:00
|
|
|
)
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// Revision represents a revision, duh.
|
|
|
|
// A revision is a version of a hypha at a point in time.
|
2020-06-13 11:18:11 +00:00
|
|
|
type Revision struct {
|
2020-06-19 13:03:31 +00:00
|
|
|
Id int `json:"-"`
|
|
|
|
FullName string `json:"-"`
|
2020-06-16 18:35:52 +00:00
|
|
|
Tags []string `json:"tags"`
|
2020-06-17 09:40:51 +00:00
|
|
|
ShortName string `json:"name"`
|
2020-06-16 18:35:52 +00:00
|
|
|
Comment string `json:"comment"`
|
|
|
|
Author string `json:"author"`
|
|
|
|
Time int `json:"time"`
|
|
|
|
TextMime string `json:"text_mime"`
|
|
|
|
BinaryMime string `json:"binary_mime"`
|
2020-06-19 13:03:31 +00:00
|
|
|
TextPath string `json:"-"`
|
|
|
|
BinaryPath string `json:"-"`
|
2020-06-20 15:17:13 +00:00
|
|
|
TextName string `json:"text_name"`
|
|
|
|
BinaryName string `json:"binary_name"`
|
2020-06-19 13:03:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// IdAsStr returns revision's id as a string.
|
|
|
|
func (rev *Revision) IdAsStr() string {
|
|
|
|
return strconv.Itoa(rev.Id)
|
2020-06-13 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// hasBinaryData returns true if the revision has any binary data associated.
|
2020-06-16 18:35:52 +00:00
|
|
|
// During initialisation, it is guaranteed that r.BinaryMime is set to "" if the revision has no binary data.
|
2020-06-19 18:11:47 +00:00
|
|
|
func (rev *Revision) hasBinaryData() bool {
|
|
|
|
return rev.BinaryMime != ""
|
2020-06-16 18:35:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 15:17:13 +00:00
|
|
|
// desiredBinaryFilename returns string that represents filename to use when saving a binary content file of a new revison.
|
|
|
|
// It also sets the corresponding field in `rev`.
|
|
|
|
func (rev *Revision) desiredBinaryFilename() string {
|
|
|
|
ts, err := mime.ExtensionsByType(rev.BinaryMime)
|
|
|
|
if err != nil || ts == nil {
|
|
|
|
rev.BinaryName = rev.IdAsStr() + ".bin"
|
|
|
|
} else {
|
|
|
|
rev.BinaryName = rev.IdAsStr() + ts[0]
|
|
|
|
}
|
|
|
|
return rev.BinaryName
|
|
|
|
}
|
|
|
|
|
|
|
|
// desiredTextFilename returns string that represents filename to use when saving a text content file of a new revison.
|
|
|
|
// It also sets the corresponding field in `rev`.
|
|
|
|
func (rev *Revision) desiredTextFilename() string {
|
|
|
|
ts, err := mime.ExtensionsByType(rev.TextMime)
|
|
|
|
if err != nil || ts == nil {
|
|
|
|
log.Println("No idea how I should name this one:", rev.TextMime)
|
|
|
|
rev.TextName = rev.IdAsStr() + ".txt"
|
|
|
|
} else {
|
|
|
|
log.Println("A good extension would be one of these:", ts)
|
|
|
|
rev.TextName = rev.IdAsStr() + ts[0]
|
|
|
|
}
|
|
|
|
return rev.TextName
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// AsHtml returns HTML representation of the revision.
|
|
|
|
// If there is an error, it will be told about it in `w`.
|
|
|
|
func (rev *Revision) AsHtml(w http.ResponseWriter) (ret string, err error) {
|
2020-06-13 11:18:11 +00:00
|
|
|
ret += `<article class="page">
|
2020-06-19 18:11:47 +00:00
|
|
|
<h1 class="page__title">` + rev.FullName + `</h1>
|
2020-06-13 11:18:11 +00:00
|
|
|
`
|
2020-06-16 18:35:52 +00:00
|
|
|
// TODO: support things other than images
|
2020-06-19 18:11:47 +00:00
|
|
|
if rev.hasBinaryData() {
|
|
|
|
ret += fmt.Sprintf(`<img src="/%s?action=getBinary&rev=%d" class="page__amnt"/>`, rev.FullName, rev.Id)
|
2020-06-13 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
contents, err := ioutil.ReadFile(rev.TextPath)
|
2020-06-13 11:18:11 +00:00
|
|
|
if err != nil {
|
2020-06-19 18:11:47 +00:00
|
|
|
log.Println("Failed to render", rev.FullName)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2020-06-13 11:18:11 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support more markups.
|
|
|
|
// TODO: support mycorrhiza extensions like transclusion.
|
2020-06-19 18:11:47 +00:00
|
|
|
switch rev.TextMime {
|
2020-06-16 18:35:52 +00:00
|
|
|
case "text/markdown":
|
2020-06-13 11:18:11 +00:00
|
|
|
html := markdown.ToHTML(contents, nil, nil)
|
|
|
|
ret += string(html)
|
|
|
|
default:
|
2020-06-17 09:40:51 +00:00
|
|
|
ret += fmt.Sprintf(`<pre>%s</pre>`, contents)
|
2020-06-13 11:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret += `
|
|
|
|
</article>`
|
|
|
|
return ret, nil
|
|
|
|
}
|
2020-06-16 18:35:52 +00:00
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// ActionGetBinary is used with `?action=getBinary`.
|
|
|
|
// It writes binary data of the revision. It also sets the MIME-type.
|
|
|
|
func (rev *Revision) ActionGetBinary(w http.ResponseWriter) {
|
|
|
|
fileContents, err := ioutil.ReadFile(rev.BinaryPath)
|
2020-06-16 18:35:52 +00:00
|
|
|
if err != nil {
|
2020-06-19 18:11:47 +00:00
|
|
|
log.Println("Failed to load binary data of", rev.FullName, rev.Id)
|
2020-06-16 18:35:52 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2020-06-19 18:11:47 +00:00
|
|
|
w.Header().Set("Content-Type", rev.BinaryMime)
|
2020-06-16 18:35:52 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(fileContents)
|
2020-06-19 18:11:47 +00:00
|
|
|
log.Println("Serving binary data of", rev.FullName, rev.Id)
|
2020-06-16 18:35:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// ActionRaw is used with `?action=raw`.
|
|
|
|
// It writes text content of the revision without any parsing or rendering.
|
|
|
|
func (rev *Revision) ActionRaw(w http.ResponseWriter) {
|
|
|
|
fileContents, err := ioutil.ReadFile(rev.TextPath)
|
2020-06-16 18:35:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-19 18:11:47 +00:00
|
|
|
w.Header().Set("Content-Type", rev.TextMime)
|
2020-06-16 18:35:52 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(fileContents)
|
2020-06-19 18:11:47 +00:00
|
|
|
log.Println("Serving text data of", rev.FullName, rev.Id)
|
2020-06-16 18:35:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// ActionZen is used with `?action=zen`.
|
|
|
|
// It renders the revision without any layout or navigation.
|
|
|
|
func (rev *Revision) ActionZen(w http.ResponseWriter) {
|
|
|
|
html, err := rev.AsHtml(w)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Fprint(w, html)
|
|
|
|
log.Println("Rendering", rev.FullName, "in zen mode")
|
2020-06-16 18:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// ActionView is used with `?action=view` or without any action.
|
|
|
|
// It renders the revision with layout and navigation.
|
|
|
|
func (rev *Revision) ActionView(w http.ResponseWriter, layoutFun func(Revision, string) string) {
|
|
|
|
html, err := rev.AsHtml(w)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Fprint(w, layoutFun(*rev, html))
|
|
|
|
log.Println("Rendering", rev.FullName)
|
2020-06-16 18:35:52 +00:00
|
|
|
}
|
|
|
|
}
|