1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00
cowyo/routes.go

364 lines
11 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
2016-03-11 22:11:00 +03:00
"os"
2016-02-08 18:03:00 +03:00
"path"
2016-02-07 21:21:24 +03:00
"regexp"
2016-02-07 19:05:38 +03:00
"strconv"
"strings"
2016-02-11 17:26:02 +03:00
"time"
"github.com/boltdb/bolt"
"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
)
func newNote(c *gin.Context) {
title := randomAlliterateCombo()
c.Redirect(302, "/"+title)
}
func editNote(c *gin.Context) {
title := c.Param("title")
if title == "ws" {
wshandler(c.Writer, c.Request)
2016-02-09 01:25:01 +03:00
} else if title == "robots.txt" {
2016-02-10 17:21:24 +03:00
c.Data(200, "text/plain", []byte(robotsTxt))
2016-02-07 19:05:38 +03:00
} else if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view")
} else {
2016-02-08 16:00:07 +03:00
locked, _ := hasPassword(title)
if locked {
c.Redirect(302, "/"+title+"/view")
} else {
2016-02-10 02:38:25 +03:00
version := c.DefaultQuery("version", "-1")
versionNum, _ := strconv.Atoi(version)
2016-02-11 17:26:02 +03:00
currentText, versions, currentVersion, totalTime := getCurrentText(title, versionNum)
if strings.Contains(currentText, "self-destruct\n") || strings.Contains(currentText, "\nself-destruct") {
c.Redirect(302, "/"+title+"/view")
}
2016-02-08 16:42:19 +03:00
numRows := len(strings.Split(currentText, "\n")) + 10
2016-02-10 02:38:25 +03:00
if currentVersion {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title,
2016-02-11 00:47:02 +03:00
"WikiName": RuntimeArgs.WikiName,
2016-02-10 02:38:25 +03:00
"ExternalIP": RuntimeArgs.ExternalIP,
"CurrentText": currentText,
"NumRows": numRows,
"Versions": versions,
2016-02-11 17:26:02 +03:00
"TotalTime": totalTime,
"SocketType": RuntimeArgs.Socket,
2016-02-10 02:38:25 +03:00
})
} else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title,
2016-02-11 00:47:02 +03:00
"WikiName": RuntimeArgs.WikiName,
2016-02-10 02:38:25 +03:00
"ExternalIP": RuntimeArgs.ExternalIP,
"CurrentText": currentText,
"NumRows": numRows,
"Versions": versions,
2016-02-11 17:26:02 +03:00
"TotalTime": totalTime,
"SocketType": RuntimeArgs.Socket,
2016-02-10 02:38:25 +03:00
"NoEdit": true,
})
}
2016-02-08 16:00:07 +03:00
}
}
}
func everythingElse(c *gin.Context) {
option := c.Param("option")
title := c.Param("title")
if option == "/view" {
2016-02-10 02:44:00 +03:00
version := c.DefaultQuery("version", "-1")
versionNum, _ := strconv.Atoi(version)
2016-02-10 02:59:01 +03:00
if strings.ToLower(title) == "about" {
versionNum = -1
}
2016-02-11 17:26:02 +03:00
currentText, versions, _, totalTime := getCurrentText(title, versionNum)
if (strings.Contains(currentText, "self-destruct\n") || strings.Contains(currentText, "\nself-destruct")) && strings.ToLower(title) != "about" {
currentText = strings.Replace(currentText, "self-destruct\n", `> *This page has been deleted, you cannot return after closing.*`+"\n", 1)
currentText = strings.Replace(currentText, "\nself-destruct", "\n"+`> *This page has been deleted, you cannot return after closing.*`, 1)
p := WikiData{strings.ToLower(title), "", []string{}, []string{}}
p.save("")
}
2016-02-11 17:26:02 +03:00
renderMarkdown(c, currentText, title, versions, "", totalTime)
2016-02-10 00:59:30 +03:00
} else if title == "ls" && option == "/"+RuntimeArgs.AdminKey && len(RuntimeArgs.AdminKey) > 1 {
2016-02-11 17:26:02 +03:00
renderMarkdown(c, listEverything(), "ls", nil, RuntimeArgs.AdminKey, time.Now().Sub(time.Now()))
} else if option == "/list" {
renderList(c, title)
} else if title == "static" {
serveStaticFile(c, option)
} else {
c.Redirect(302, "/"+title)
}
}
func serveStaticFile(c *gin.Context, option string) {
2016-02-08 18:03:00 +03:00
staticFile, err := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "static") + option)
if err != nil {
c.AbortWithStatus(404)
} else {
c.Data(200, contentType(option), []byte(staticFile))
}
}
2016-02-11 17:26:02 +03:00
func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string, totalTime time.Duration) {
2016-02-11 02:49:45 +03:00
r, _ := regexp.Compile("\\[\\[(.*?)\\]\\]")
for _, s := range r.FindAllString(currentText, -1) {
currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 1)
}
unsafe := blackfriday.MarkdownCommon([]byte(currentText))
2016-02-09 04:05:29 +03:00
pClean := bluemonday.UGCPolicy()
pClean.AllowElements("img")
pClean.AllowAttrs("alt").OnElements("img")
pClean.AllowAttrs("src").OnElements("img")
2016-02-11 05:16:54 +03:00
pClean.AllowAttrs("class").OnElements("a")
pClean.AllowAttrs("href").OnElements("a")
pClean.AllowAttrs("id").OnElements("a")
2016-02-09 04:05:29 +03:00
pClean.AllowDataURIImages()
html := pClean.SanitizeBytes(unsafe)
2016-02-07 21:21:24 +03:00
html2 := string(html)
2016-02-11 02:49:45 +03:00
r, _ = regexp.Compile("\\$\\$(.*?)\\$\\$")
2016-02-07 21:21:24 +03:00
for _, s := range r.FindAllString(html2, -1) {
2016-02-07 21:49:46 +03:00
html2 = strings.Replace(html2, s, "<span class='texp' data-expr='"+s[2:len(s)-2]+"'></span>", 1)
2016-02-07 21:38:39 +03:00
}
r, _ = regexp.Compile("\\$(.*?)\\$")
for _, s := range r.FindAllString(html2, -1) {
2016-02-07 21:49:46 +03:00
html2 = strings.Replace(html2, s, "<span class='texi' data-expr='"+s[1:len(s)-1]+"'></span>", 1)
2016-02-07 21:21:24 +03:00
}
2016-02-07 22:00:54 +03:00
html2 = strings.Replace(html2, "&amp;#36;", "&#36;", -1)
2016-02-11 02:49:45 +03:00
html2 = strings.Replace(html2, "&amp;#91;", "&#91;", -1)
html2 = strings.Replace(html2, "&amp;#93;", "&#93;", -1)
2016-02-11 05:16:54 +03:00
html2 = strings.Replace(html2, "&amp35;", "&#35;", -1)
if AdminKey == "" {
c.HTML(http.StatusOK, "view.tmpl", gin.H{
2016-02-11 17:26:02 +03:00
"Title": title,
"WikiName": RuntimeArgs.WikiName,
"Body": template.HTML([]byte(html2)),
"TotalTime": totalTime.String(),
"Versions": versions,
2016-02-11 05:16:54 +03:00
})
} else {
c.HTML(http.StatusOK, "view.tmpl", gin.H{
2016-02-11 17:26:02 +03:00
"Title": title,
"WikiName": RuntimeArgs.WikiName,
"Body": template.HTML([]byte(html2)),
"Versions": versions,
"TotalTime": totalTime.String(),
"AdminKey": AdminKey,
2016-02-11 05:16:54 +03:00
})
}
}
2016-02-07 19:05:38 +03:00
func reorderList(text string) ([]template.HTML, []string) {
listItemsString := ""
for _, lineString := range strings.Split(text, "\n") {
if len(lineString) > 1 {
if string(lineString[0]) != "-" {
listItemsString += "- " + lineString + "\n"
} else {
listItemsString += lineString + "\n"
}
}
}
// get ordering of template.HTML for rendering
renderedListString := string(blackfriday.MarkdownCommon([]byte(listItemsString)))
listItems := []template.HTML{}
endItems := []template.HTML{}
for _, lineString := range strings.Split(renderedListString, "\n") {
if len(lineString) > 1 {
if strings.Contains(lineString, "<del>") || strings.Contains(lineString, "</ul>") {
endItems = append(endItems, template.HTML(lineString))
} else {
listItems = append(listItems, template.HTML(lineString))
}
}
}
// get ordering of strings for deleting
listItemsStringArray := []string{}
endItemsStringArray := []string{}
for _, lineString := range strings.Split(listItemsString, "\n") {
if len(lineString) > 1 {
if strings.Contains(lineString, "~~") {
endItemsStringArray = append(endItemsStringArray, lineString)
} else {
listItemsStringArray = append(listItemsStringArray, lineString)
}
}
}
return append(listItems, endItems...), append(listItemsStringArray, endItemsStringArray...)
}
func renderList(c *gin.Context, title string) {
2016-02-07 19:05:38 +03:00
if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view")
}
2016-02-10 17:13:21 +03:00
var p WikiData
err := p.load(strings.ToLower(title))
if err != nil {
panic(err)
}
2016-02-07 19:05:38 +03:00
currentText := p.CurrentText
if strings.Contains(currentText, "self-destruct\n") || strings.Contains(currentText, "\nself-destruct") {
c.Redirect(302, "/"+title+"/view")
}
2016-02-14 07:10:34 +03:00
pClean := bluemonday.UGCPolicy()
pClean.AllowElements("img")
pClean.AllowAttrs("alt").OnElements("img")
pClean.AllowAttrs("src").OnElements("img")
pClean.AllowAttrs("class").OnElements("a")
pClean.AllowAttrs("href").OnElements("a")
pClean.AllowAttrs("id").OnElements("a")
pClean.AllowDataURIImages()
text := pClean.SanitizeBytes([]byte(p.CurrentText))
listItems, _ := reorderList(string(text))
fmt.Println(string(text))
c.HTML(http.StatusOK, "list.tmpl", gin.H{
"Title": title,
2016-02-11 00:47:02 +03:00
"WikiName": RuntimeArgs.WikiName,
"ListItems": listItems,
})
}
2016-02-07 19:05:38 +03:00
func deleteListItem(c *gin.Context) {
lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None"))
title := c.Query("title") // shortcut for c.Request.URL.Query().Get("lastname")
if err == nil {
2016-02-10 17:13:21 +03:00
var p WikiData
err := p.load(strings.ToLower(title))
2016-02-07 19:05:38 +03:00
if err != nil {
panic(err)
}
_, listItems := reorderList(p.CurrentText)
newText := p.CurrentText
2016-02-07 19:05:38 +03:00
for i, lineString := range listItems {
2016-02-07 19:11:39 +03:00
// fmt.Println(i, lineString, lineNum)
2016-02-07 19:05:38 +03:00
if i+1 == lineNum {
2016-02-07 19:11:39 +03:00
// fmt.Println("MATCHED")
2016-02-07 19:05:38 +03:00
if strings.Contains(lineString, "~~") == false {
2016-02-07 19:11:39 +03:00
// fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n")
2016-02-11 04:47:43 +03:00
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", "~~"+strings.TrimSpace(lineString[2:])+"~~"+"\n", 1)
2016-02-07 19:05:38 +03:00
} else {
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
2016-02-07 19:05:38 +03:00
}
p.save(newText)
2016-02-07 19:05:38 +03:00
break
}
}
c.JSON(200, gin.H{
"message": "Done.",
})
} else {
c.JSON(404, gin.H{
"message": "?",
})
}
}
2016-02-07 19:05:38 +03:00
2016-02-11 05:16:54 +03:00
func deletePage(c *gin.Context) {
deleteName := c.DefaultQuery("DeleteName", "None")
adminKey := c.DefaultQuery("AdminKey", "None")
fmt.Println(adminKey)
fmt.Println(deleteName)
// if adminKey == RuntimeArgs.AdminKey || true == true {
if strings.ToLower(deleteName) != "about" {
p := WikiData{strings.ToLower(deleteName), "", []string{}, []string{}}
p.save("")
}
// // remove from program data
// var deleteKey []byte
// foundKey := false
// err := db.View(func(tx *bolt.Tx) error {
// b := tx.Bucket([]byte("programdata"))
// c := b.Cursor()
// for k, v := c.First(); k != nil; k, v = c.Next() {
// if strings.ToLower(string(v)) == strings.ToLower(deleteName) {
// fmt.Println("FOUND " + string(v))
// deleteKey = k
// foundKey = true
// break
// }
// }
// return nil
// })
// if err != nil {
// panic(err)
// }
// if foundKey == true {
// fmt.Println(len([]string{}))
// fmt.Println(deleteKey)
// db.View(func(tx *bolt.Tx) error {
// b := tx.Bucket([]byte("programdata"))
// err := b.Delete(deleteKey)
// return err
// })
// }
// return OKAY
c.JSON(200, gin.H{
"message": "Done.",
})
// } else {
// c.JSON(404, gin.H{
// "message": "?",
// })
// }
2016-02-11 05:16:54 +03:00
}
func listEverything() string {
2016-02-11 05:16:54 +03:00
everything := `| Title | Current size | Changes | Total Size | |
| --------- |-------------| -----| ------------- | ------------- |
2016-02-10 00:59:30 +03:00
`
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("datas"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
2016-02-10 17:13:21 +03:00
var p WikiData
2016-02-10 00:59:30 +03:00
p.load(string(k))
if len(p.CurrentText) > 1 {
contentSize := strconv.Itoa(len(p.CurrentText))
numChanges := strconv.Itoa(len(p.Diffs))
totalSize := strconv.Itoa(len(v))
2016-02-11 05:16:54 +03:00
everything += "| [" + p.Title + "](/" + p.Title + "/view) | " + contentSize + " | " + numChanges + " | " + totalSize + ` | <a class="deleteable" id="` + p.Title + `">Delete</a> | ` + "\n"
}
}
return nil
})
return everything
2016-02-07 19:05:38 +03:00
}
2016-03-11 22:11:00 +03:00
func dumpEverything() {
err := os.MkdirAll("dump", 0777)
if err != nil {
fmt.Println("Already exists")
}
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("datas"))
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
var p WikiData
p.load(string(k))
fmt.Println(string(k), len(p.CurrentText))
ioutil.WriteFile(path.Join("dump", string(k)+".md"), []byte(p.CurrentText), 0644)
}
return nil
})
}