2017-03-24 02:47:41 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-10-15 16:49:40 +03:00
|
|
|
"fmt"
|
2017-03-24 02:47:41 +03:00
|
|
|
"html/template"
|
2017-10-15 16:49:40 +03:00
|
|
|
"io/ioutil"
|
2017-03-24 02:47:41 +03:00
|
|
|
"net/http"
|
2017-10-15 17:18:48 +03:00
|
|
|
"path"
|
2017-03-24 02:47:41 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
// "github.com/gin-contrib/static"
|
|
|
|
"github.com/gin-contrib/multitemplate"
|
2017-09-29 15:48:32 +03:00
|
|
|
"github.com/gin-contrib/sessions"
|
2017-03-24 02:47:41 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2017-06-30 00:24:34 +03:00
|
|
|
"github.com/schollz/cowyo/encrypt"
|
2017-03-24 02:47:41 +03:00
|
|
|
)
|
|
|
|
|
2017-06-23 16:40:35 +03:00
|
|
|
func serve(host, port, crt_path, key_path string, TLS bool) {
|
2017-03-24 02:47:41 +03:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
router := gin.Default()
|
2017-09-29 15:48:32 +03:00
|
|
|
store := sessions.NewCookieStore([]byte("secret"))
|
|
|
|
router.Use(sessions.Sessions("mysession", store))
|
2017-03-24 02:47:41 +03:00
|
|
|
router.HTMLRender = loadTemplates("index.tmpl")
|
|
|
|
// router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
|
|
|
|
router.GET("/", func(c *gin.Context) {
|
|
|
|
c.Redirect(302, "/"+randomAlliterateCombo())
|
|
|
|
})
|
|
|
|
router.GET("/:page", func(c *gin.Context) {
|
|
|
|
page := c.Param("page")
|
|
|
|
c.Redirect(302, "/"+page+"/edit")
|
|
|
|
})
|
|
|
|
router.GET("/:page/*command", handlePageRequest)
|
|
|
|
router.POST("/update", handlePageUpdate)
|
2017-06-28 06:32:38 +03:00
|
|
|
router.POST("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary)
|
2017-07-05 19:49:06 +03:00
|
|
|
router.POST("/exists", handlePageExists)
|
2017-03-24 02:47:41 +03:00
|
|
|
router.POST("/prime", handlePrime)
|
|
|
|
router.POST("/lock", handleLock)
|
2017-10-15 16:49:40 +03:00
|
|
|
router.POST("/publish", handlePublish)
|
2017-03-24 02:47:41 +03:00
|
|
|
router.POST("/encrypt", handleEncrypt)
|
|
|
|
router.DELETE("/oldlist", handleClearOldListItems)
|
|
|
|
router.DELETE("/listitem", deleteListItem)
|
|
|
|
|
2017-10-15 17:18:48 +03:00
|
|
|
// start long-processes as threads
|
|
|
|
go thread_SiteMap()
|
|
|
|
|
2017-06-23 16:40:35 +03:00
|
|
|
if TLS {
|
|
|
|
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
|
|
|
|
} else {
|
|
|
|
router.Run(host + ":" + port)
|
|
|
|
}
|
2017-03-24 02:47:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadTemplates(list ...string) multitemplate.Render {
|
|
|
|
r := multitemplate.New()
|
|
|
|
|
|
|
|
for _, x := range list {
|
|
|
|
templateString, err := Asset("templates/" + x)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmplMessage, err := template.New(x).Parse(string(templateString))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Add(x, tmplMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-06-28 06:32:38 +03:00
|
|
|
func handlePageRelinquish(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
}
|
|
|
|
var json QueryJSON
|
|
|
|
err := c.BindJSON(&json)
|
|
|
|
if err != nil {
|
|
|
|
log.Trace(err.Error())
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(json.Page) == 0 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Must specify `page`"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
message := "Relinquished"
|
|
|
|
p := Open(json.Page)
|
2017-08-17 17:36:07 +03:00
|
|
|
name := p.Meta
|
|
|
|
if name == "" {
|
|
|
|
name = json.Page
|
|
|
|
}
|
2017-06-28 06:32:38 +03:00
|
|
|
text := p.Text.GetCurrent()
|
2017-06-28 19:10:32 +03:00
|
|
|
isLocked := p.IsEncrypted
|
|
|
|
isEncrypted := p.IsEncrypted
|
|
|
|
destroyed := p.IsPrimedForSelfDestruct
|
2017-06-28 06:32:38 +03:00
|
|
|
if !p.IsLocked && p.IsPrimedForSelfDestruct {
|
|
|
|
p.Erase()
|
|
|
|
message = "Relinquished and erased"
|
|
|
|
}
|
2017-08-12 17:05:01 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true,
|
|
|
|
"name": name,
|
|
|
|
"message": message,
|
|
|
|
"text": text,
|
|
|
|
"locked": isLocked,
|
|
|
|
"encrypted": isEncrypted,
|
|
|
|
"destroyed": destroyed})
|
2017-06-28 06:32:38 +03:00
|
|
|
}
|
|
|
|
|
2017-10-15 17:18:48 +03:00
|
|
|
func thread_SiteMap() {
|
|
|
|
for {
|
|
|
|
log.Info("Generating sitemap...")
|
|
|
|
ioutil.WriteFile(path.Join(pathToData, "sitemap.xml"), []byte(generateSiteMap()), 0644)
|
|
|
|
log.Info("..finished generating sitemap")
|
|
|
|
time.Sleep(24 * time.Hour)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 16:49:40 +03:00
|
|
|
func generateSiteMap() (sitemap string) {
|
|
|
|
files, _ := ioutil.ReadDir(pathToData)
|
|
|
|
lastEdited := make([]string, len(files))
|
|
|
|
names := make([]string, len(files))
|
|
|
|
i := 0
|
|
|
|
for _, f := range files {
|
|
|
|
names[i] = DecodeFileName(f.Name())
|
|
|
|
p := Open(names[i])
|
|
|
|
if p.IsPublished {
|
|
|
|
lastEdited[i] = time.Unix(p.Text.LastEditTime()/1000000000, 0).Format("2006-01-02")
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
names = names[:i]
|
|
|
|
lastEdited = lastEdited[:i]
|
|
|
|
sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
2017-10-15 17:18:48 +03:00
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`
|
2017-10-15 16:49:40 +03:00
|
|
|
for i := range names {
|
|
|
|
sitemap += fmt.Sprintf(`
|
2017-10-15 17:18:48 +03:00
|
|
|
<url>
|
|
|
|
<loc>https://cowyo.com/%s/view</loc>
|
|
|
|
<lastmod>%s</lastmod>
|
|
|
|
<changefreq>monthly</changefreq>
|
|
|
|
<priority>0.8</priority>
|
|
|
|
</url>
|
|
|
|
`, names[i], lastEdited[i])
|
2017-10-15 16:49:40 +03:00
|
|
|
}
|
|
|
|
sitemap += "</urlset>"
|
|
|
|
return
|
|
|
|
}
|
2017-11-02 15:27:39 +03:00
|
|
|
|
2017-03-24 02:47:41 +03:00
|
|
|
func handlePageRequest(c *gin.Context) {
|
|
|
|
page := c.Param("page")
|
2017-10-15 16:58:43 +03:00
|
|
|
command := c.Param("command")
|
|
|
|
|
2017-10-15 16:49:40 +03:00
|
|
|
if page == "sitemap.xml" {
|
2017-10-15 17:18:48 +03:00
|
|
|
siteMap, err := ioutil.ReadFile(path.Join(pathToData, "sitemap.xml"))
|
|
|
|
if err != nil {
|
|
|
|
c.Data(http.StatusInternalServerError, contentType("sitemap.xml"), []byte(""))
|
|
|
|
} else {
|
|
|
|
c.Data(http.StatusOK, contentType("sitemap.xml"), siteMap)
|
|
|
|
}
|
2017-10-15 16:49:40 +03:00
|
|
|
return
|
2017-10-15 16:58:43 +03:00
|
|
|
} else if page == "favicon.ico" {
|
|
|
|
data, _ := Asset("/static/img/cowyo/favicon.ico")
|
|
|
|
c.Data(http.StatusOK, contentType("/static/img/cowyo/favicon.ico"), data)
|
2017-05-24 19:42:37 +03:00
|
|
|
return
|
2017-10-15 16:58:43 +03:00
|
|
|
} else if page == "static" {
|
2017-03-24 02:47:41 +03:00
|
|
|
filename := page + command
|
|
|
|
data, err := Asset(filename)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Could not find data")
|
|
|
|
}
|
|
|
|
c.Data(http.StatusOK, contentType(filename), data)
|
|
|
|
return
|
|
|
|
}
|
2017-11-02 15:27:39 +03:00
|
|
|
p := Open(page)
|
|
|
|
fmt.Println(command)
|
2017-10-15 16:58:43 +03:00
|
|
|
if len(command) < 2 {
|
2017-11-02 15:27:39 +03:00
|
|
|
fmt.Println(p.IsPublished)
|
|
|
|
if p.IsPublished {
|
|
|
|
c.Redirect(302, "/"+page+"/read")
|
|
|
|
} else {
|
|
|
|
c.Redirect(302, "/"+page+"/edit")
|
|
|
|
}
|
2017-10-15 16:58:43 +03:00
|
|
|
return
|
|
|
|
}
|
2017-03-24 02:47:41 +03:00
|
|
|
|
|
|
|
version := c.DefaultQuery("version", "ajksldfjl")
|
|
|
|
|
|
|
|
// Disallow anything but viewing locked/encrypted pages
|
|
|
|
if (p.IsEncrypted || p.IsLocked) &&
|
|
|
|
(command[0:2] != "/v" && command[0:2] != "/r") {
|
|
|
|
c.Redirect(302, "/"+page+"/view")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy page if it is opened and primed
|
|
|
|
if p.IsPrimedForSelfDestruct && !p.IsLocked && !p.IsEncrypted {
|
|
|
|
p.Update("*This page has self-destructed. You can not return to it.*\n\n" + p.Text.GetCurrent())
|
|
|
|
p.Erase()
|
2017-09-28 11:06:38 +03:00
|
|
|
command = "/view"
|
2017-03-24 02:47:41 +03:00
|
|
|
}
|
|
|
|
if command == "/erase" {
|
|
|
|
if !p.IsLocked && !p.IsEncrypted {
|
|
|
|
p.Erase()
|
|
|
|
c.Redirect(302, "/"+page+"/edit")
|
|
|
|
} else {
|
|
|
|
c.Redirect(302, "/"+page+"/view")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rawText := p.Text.GetCurrent()
|
|
|
|
rawHTML := p.RenderedPage
|
|
|
|
|
|
|
|
// Check to see if an old version is requested
|
|
|
|
versionInt, versionErr := strconv.Atoi(version)
|
|
|
|
if versionErr == nil && versionInt > 0 {
|
|
|
|
versionText, err := p.Text.GetPreviousByTimestamp(int64(versionInt))
|
|
|
|
if err == nil {
|
|
|
|
rawText = versionText
|
|
|
|
rawHTML = GithubMarkdownToHTML(rawText)
|
|
|
|
}
|
|
|
|
}
|
2017-03-24 16:09:41 +03:00
|
|
|
|
|
|
|
// Get history
|
|
|
|
var versionsInt64 []int64
|
|
|
|
var versionsChangeSums []int
|
|
|
|
var versionsText []string
|
|
|
|
if command[0:2] == "/h" {
|
2017-03-24 16:12:09 +03:00
|
|
|
versionsInt64, versionsChangeSums = p.Text.GetMajorSnapshotsAndChangeSums(60) // get snapshots 60 seconds apart
|
2017-03-24 16:09:41 +03:00
|
|
|
versionsText = make([]string, len(versionsInt64))
|
|
|
|
for i, v := range versionsInt64 {
|
|
|
|
versionsText[i] = time.Unix(v/1000000000, 0).Format("Mon Jan 2 15:04:05 MST 2006")
|
|
|
|
}
|
|
|
|
versionsText = reverseSliceString(versionsText)
|
|
|
|
versionsInt64 = reverseSliceInt64(versionsInt64)
|
|
|
|
versionsChangeSums = reverseSliceInt(versionsChangeSums)
|
2017-03-24 02:47:41 +03:00
|
|
|
}
|
|
|
|
|
2017-11-02 15:27:39 +03:00
|
|
|
if command[0:3] == "/ra" {
|
2017-03-24 02:47:41 +03:00
|
|
|
c.Writer.Header().Set("Content-Type", contentType(p.Name))
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Max")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
c.Data(200, contentType(p.Name), []byte(rawText))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug(command)
|
|
|
|
log.Debug("%v", command[0:2] != "/e" &&
|
|
|
|
command[0:2] != "/v" &&
|
|
|
|
command[0:2] != "/l" &&
|
2017-11-02 15:27:39 +03:00
|
|
|
command[0:2] != "/h" &&
|
|
|
|
command[0:2] != "/r")
|
2017-03-24 02:47:41 +03:00
|
|
|
|
2017-03-24 16:58:37 +03:00
|
|
|
var FileNames, FileLastEdited []string
|
|
|
|
var FileSizes, FileNumChanges []int
|
|
|
|
if page == "ls" {
|
|
|
|
command = "/view"
|
|
|
|
FileNames, FileSizes, FileNumChanges, FileLastEdited = DirectoryList()
|
|
|
|
}
|
|
|
|
|
2017-03-24 02:47:41 +03:00
|
|
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
|
|
|
"EditPage": command[0:2] == "/e", // /edit
|
|
|
|
"ViewPage": command[0:2] == "/v", // /view
|
|
|
|
"ListPage": command[0:2] == "/l", // /list
|
|
|
|
"HistoryPage": command[0:2] == "/h", // /history
|
2017-11-02 15:27:39 +03:00
|
|
|
"ReadPage": command[0:2] == "/r", // /history
|
2017-03-24 02:47:41 +03:00
|
|
|
"DontKnowPage": command[0:2] != "/e" &&
|
|
|
|
command[0:2] != "/v" &&
|
|
|
|
command[0:2] != "/l" &&
|
|
|
|
command[0:2] != "/h",
|
2017-03-24 16:58:37 +03:00
|
|
|
"DirectoryPage": page == "ls",
|
|
|
|
"FileNames": FileNames,
|
|
|
|
"FileSizes": FileSizes,
|
|
|
|
"FileNumChanges": FileNumChanges,
|
|
|
|
"FileLastEdited": FileLastEdited,
|
2017-03-24 16:09:41 +03:00
|
|
|
"Page": page,
|
|
|
|
"RenderedPage": template.HTML([]byte(rawHTML)),
|
|
|
|
"RawPage": rawText,
|
|
|
|
"Versions": versionsInt64,
|
|
|
|
"VersionsText": versionsText,
|
|
|
|
"VersionsChangeSums": versionsChangeSums,
|
|
|
|
"IsLocked": p.IsLocked,
|
|
|
|
"IsEncrypted": p.IsEncrypted,
|
|
|
|
"ListItems": renderList(rawText),
|
|
|
|
"Route": "/" + page + command,
|
|
|
|
"HasDotInName": strings.Contains(page, "."),
|
2017-09-29 15:48:32 +03:00
|
|
|
"RecentlyEdited": getRecentlyEdited(page, c),
|
2017-10-15 16:49:40 +03:00
|
|
|
"IsPublished": p.IsPublished,
|
2017-11-02 15:27:39 +03:00
|
|
|
"CustomCSS": "",
|
2017-03-24 02:47:41 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-29 15:48:32 +03:00
|
|
|
func getRecentlyEdited(title string, c *gin.Context) []string {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
var recentlyEdited string
|
|
|
|
v := session.Get("recentlyEdited")
|
|
|
|
editedThings := []string{}
|
|
|
|
if v == nil {
|
|
|
|
recentlyEdited = title
|
|
|
|
} else {
|
|
|
|
editedThings = strings.Split(v.(string), "|||")
|
|
|
|
if !stringInSlice(title, editedThings) {
|
|
|
|
recentlyEdited = v.(string) + "|||" + title
|
|
|
|
} else {
|
|
|
|
recentlyEdited = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session.Set("recentlyEdited", recentlyEdited)
|
|
|
|
session.Save()
|
|
|
|
editedThingsWithoutCurrent := make([]string, len(editedThings))
|
|
|
|
i := 0
|
|
|
|
for _, thing := range editedThings {
|
|
|
|
if thing == title {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-29 19:21:10 +03:00
|
|
|
if strings.Contains(thing, "icon-") {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-29 15:48:32 +03:00
|
|
|
editedThingsWithoutCurrent[i] = thing
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return editedThingsWithoutCurrent[:i]
|
|
|
|
}
|
|
|
|
|
2017-07-05 19:49:06 +03:00
|
|
|
func handlePageExists(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
}
|
|
|
|
var json QueryJSON
|
|
|
|
err := c.BindJSON(&json)
|
|
|
|
if err != nil {
|
|
|
|
log.Trace(err.Error())
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON", "exists": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Open(json.Page)
|
|
|
|
if len(p.Text.GetCurrent()) > 0 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": json.Page + " found", "exists": true})
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": json.Page + " not found", "exists": false})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-24 02:47:41 +03:00
|
|
|
func handlePageUpdate(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
2017-06-28 06:32:38 +03:00
|
|
|
Page string `json:"page"`
|
|
|
|
NewText string `json:"new_text"`
|
|
|
|
IsEncrypted bool `json:"is_encrypted"`
|
|
|
|
IsPrimed bool `json:"is_primed"`
|
2017-08-17 17:36:07 +03:00
|
|
|
Meta string `json:"meta"`
|
2017-03-24 02:47:41 +03:00
|
|
|
}
|
|
|
|
var json QueryJSON
|
2017-06-28 06:32:38 +03:00
|
|
|
err := c.BindJSON(&json)
|
|
|
|
if err != nil {
|
|
|
|
log.Trace(err.Error())
|
2017-03-24 02:47:41 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON"})
|
|
|
|
return
|
|
|
|
}
|
2017-08-17 17:36:07 +03:00
|
|
|
if len(json.NewText) > 100000000 {
|
2017-03-24 02:47:41 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Too much"})
|
|
|
|
return
|
|
|
|
}
|
2017-06-28 06:32:38 +03:00
|
|
|
if len(json.Page) == 0 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Must specify `page`"})
|
|
|
|
return
|
|
|
|
}
|
2017-03-24 02:47:41 +03:00
|
|
|
log.Trace("Update: %v", json)
|
|
|
|
p := Open(json.Page)
|
|
|
|
var message string
|
2017-07-05 19:49:06 +03:00
|
|
|
success := false
|
2017-03-24 02:47:41 +03:00
|
|
|
if p.IsLocked {
|
2017-06-28 06:32:38 +03:00
|
|
|
message = "Locked, must unlock first"
|
2017-03-24 02:47:41 +03:00
|
|
|
} else if p.IsEncrypted {
|
2017-06-28 06:32:38 +03:00
|
|
|
message = "Encrypted, must decrypt first"
|
2017-03-24 02:47:41 +03:00
|
|
|
} else {
|
2017-08-17 17:36:07 +03:00
|
|
|
p.Meta = json.Meta
|
2017-03-24 02:47:41 +03:00
|
|
|
p.Update(json.NewText)
|
2017-06-28 06:32:38 +03:00
|
|
|
if json.IsEncrypted {
|
|
|
|
p.IsEncrypted = true
|
|
|
|
}
|
|
|
|
if json.IsPrimed {
|
|
|
|
p.IsPrimedForSelfDestruct = true
|
|
|
|
}
|
2017-08-17 17:44:18 +03:00
|
|
|
p.Save()
|
2017-03-24 02:47:41 +03:00
|
|
|
message = "Saved"
|
2017-07-05 19:49:06 +03:00
|
|
|
success = true
|
2017-03-24 02:47:41 +03:00
|
|
|
}
|
2017-07-05 19:49:06 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": success, "message": message})
|
2017-03-24 02:47:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlePrime(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
}
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("Update: %v", json)
|
|
|
|
p := Open(json.Page)
|
|
|
|
if p.IsLocked {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"})
|
|
|
|
return
|
|
|
|
} else if p.IsEncrypted {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.IsPrimedForSelfDestruct = true
|
|
|
|
p.Save()
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": "Primed"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLock(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
Passphrase string `json:"passphrase"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Open(json.Page)
|
|
|
|
if p.IsEncrypted {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var message string
|
|
|
|
if p.IsLocked {
|
|
|
|
err2 := CheckPasswordHash(json.Passphrase, p.PassphraseToUnlock)
|
|
|
|
if err2 != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Can't unlock"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.IsLocked = false
|
|
|
|
message = "Unlocked"
|
|
|
|
} else {
|
|
|
|
p.IsLocked = true
|
|
|
|
p.PassphraseToUnlock = HashPassword(json.Passphrase)
|
|
|
|
message = "Locked"
|
|
|
|
}
|
|
|
|
p.Save()
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
|
2017-10-15 16:49:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlePublish(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
Publish bool `json:"publish"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Open(json.Page)
|
|
|
|
p.IsPublished = json.Publish
|
|
|
|
p.Save()
|
|
|
|
message := "Published"
|
|
|
|
if !p.IsPublished {
|
|
|
|
message = "Unpublished"
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
|
2017-03-24 02:47:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleEncrypt(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
Passphrase string `json:"passphrase"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Open(json.Page)
|
|
|
|
if p.IsLocked {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q := Open(json.Page)
|
|
|
|
var message string
|
|
|
|
if p.IsEncrypted {
|
2017-06-30 00:24:34 +03:00
|
|
|
decrypted, err2 := encrypt.DecryptString(p.Text.GetCurrent(), json.Passphrase)
|
2017-03-24 02:47:41 +03:00
|
|
|
if err2 != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong password"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q.Erase()
|
|
|
|
q = Open(json.Page)
|
|
|
|
q.Update(decrypted)
|
|
|
|
q.IsEncrypted = false
|
|
|
|
q.IsLocked = p.IsLocked
|
|
|
|
q.IsPrimedForSelfDestruct = p.IsPrimedForSelfDestruct
|
|
|
|
message = "Decrypted"
|
|
|
|
} else {
|
|
|
|
currentText := p.Text.GetCurrent()
|
2017-06-30 00:24:34 +03:00
|
|
|
encrypted, _ := encrypt.EncryptString(currentText, json.Passphrase)
|
2017-03-24 02:47:41 +03:00
|
|
|
q.Erase()
|
|
|
|
q = Open(json.Page)
|
|
|
|
q.Update(encrypted)
|
|
|
|
q.IsEncrypted = true
|
|
|
|
q.IsLocked = p.IsLocked
|
|
|
|
q.IsPrimedForSelfDestruct = p.IsPrimedForSelfDestruct
|
|
|
|
message = "Encrypted"
|
|
|
|
}
|
|
|
|
q.Save()
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteListItem(c *gin.Context) {
|
|
|
|
lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None"))
|
|
|
|
page := c.Query("page") // shortcut for c.Request.URL.Query().Get("lastname")
|
|
|
|
if err == nil {
|
|
|
|
p := Open(page)
|
|
|
|
|
|
|
|
_, listItems := reorderList(p.Text.GetCurrent())
|
|
|
|
newText := p.Text.GetCurrent()
|
|
|
|
for i, lineString := range listItems {
|
|
|
|
// fmt.Println(i, lineString, lineNum)
|
|
|
|
if i+1 == lineNum {
|
|
|
|
// fmt.Println("MATCHED")
|
|
|
|
if strings.Contains(lineString, "~~") == false {
|
|
|
|
// fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n")
|
|
|
|
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", "~~"+strings.TrimSpace(lineString[2:])+"~~"+"\n", 1)
|
|
|
|
} else {
|
|
|
|
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
|
|
|
|
}
|
|
|
|
p.Update(newText)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "Done.",
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleClearOldListItems(c *gin.Context) {
|
|
|
|
type QueryJSON struct {
|
|
|
|
Page string `json:"page"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var json QueryJSON
|
|
|
|
if c.BindJSON(&json) != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Problem binding keys")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Open(json.Page)
|
|
|
|
if p.IsEncrypted {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.IsLocked {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lines := strings.Split(p.Text.GetCurrent(), "\n")
|
|
|
|
newLines := make([]string, len(lines))
|
|
|
|
newLinesI := 0
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.Count(line, "~~") != 2 {
|
|
|
|
newLines[newLinesI] = line
|
|
|
|
newLinesI++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Update(strings.Join(newLines[0:newLinesI], "\n"))
|
|
|
|
p.Save()
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": "Cleared"})
|
|
|
|
}
|