2018-04-28 04:32:08 +03:00
|
|
|
package server
|
2018-02-16 02:50:48 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/schollz/versionedtext"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Page is the basic struct
|
|
|
|
type Page struct {
|
2018-04-28 05:30:13 +03:00
|
|
|
Site *Site
|
|
|
|
|
2018-02-16 02:50:48 +03:00
|
|
|
Name string
|
|
|
|
Text versionedtext.VersionedText
|
|
|
|
Meta string
|
|
|
|
RenderedPage string
|
|
|
|
IsLocked bool
|
|
|
|
PassphraseToUnlock string
|
|
|
|
IsEncrypted bool
|
|
|
|
IsPrimedForSelfDestruct bool
|
|
|
|
IsPublished bool
|
2018-03-12 01:46:29 +03:00
|
|
|
UnlockedFor string
|
2018-02-16 02:50:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p Page) LastEditTime() time.Time {
|
|
|
|
return time.Unix(p.LastEditUnixTime(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Page) LastEditUnixTime() int64 {
|
|
|
|
return p.Text.LastEditTime() / 1000000000
|
|
|
|
}
|
|
|
|
|
2018-04-28 05:30:13 +03:00
|
|
|
func (s *Site) Open(name string) (p *Page) {
|
2018-02-16 02:50:48 +03:00
|
|
|
p = new(Page)
|
2018-04-28 05:30:13 +03:00
|
|
|
p.Site = s
|
2018-02-16 02:50:48 +03:00
|
|
|
p.Name = name
|
|
|
|
p.Text = versionedtext.NewVersionedText("")
|
|
|
|
p.Render()
|
2018-04-28 12:29:35 +03:00
|
|
|
bJSON, err := ioutil.ReadFile(path.Join(s.PathToData, encodeToBase32(strings.ToLower(name))+".json"))
|
2018-02-16 02:50:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(bJSON, &p)
|
|
|
|
if err != nil {
|
|
|
|
p = new(Page)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
type DirectoryEntry struct {
|
2018-04-01 04:31:26 +03:00
|
|
|
Path string
|
2018-02-16 02:50:48 +03:00
|
|
|
Length int
|
|
|
|
Numchanges int
|
|
|
|
LastEdited time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DirectoryEntry) LastEditTime() string {
|
|
|
|
return d.LastEdited.Format("Mon Jan 2 15:04:05 MST 2006")
|
|
|
|
}
|
|
|
|
|
2018-04-01 04:31:26 +03:00
|
|
|
func (d DirectoryEntry) Name() string {
|
|
|
|
return d.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DirectoryEntry) Size() int64 {
|
|
|
|
return int64(d.Length)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DirectoryEntry) Mode() os.FileMode {
|
|
|
|
return os.ModePerm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DirectoryEntry) ModTime() time.Time {
|
|
|
|
return d.LastEdited
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DirectoryEntry) IsDir() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DirectoryEntry) Sys() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-28 05:30:13 +03:00
|
|
|
func (s *Site) DirectoryList() []os.FileInfo {
|
2018-04-28 12:29:35 +03:00
|
|
|
files, _ := ioutil.ReadDir(s.PathToData)
|
2018-04-01 04:31:26 +03:00
|
|
|
entries := make([]os.FileInfo, len(files))
|
2018-02-16 02:50:48 +03:00
|
|
|
for i, f := range files {
|
|
|
|
name := DecodeFileName(f.Name())
|
2018-04-28 05:30:13 +03:00
|
|
|
p := s.Open(name)
|
2018-02-16 02:50:48 +03:00
|
|
|
entries[i] = DirectoryEntry{
|
2018-04-01 04:31:26 +03:00
|
|
|
Path: name,
|
2018-02-16 02:50:48 +03:00
|
|
|
Length: len(p.Text.GetCurrent()),
|
|
|
|
Numchanges: p.Text.NumEdits(),
|
|
|
|
LastEdited: time.Unix(p.Text.LastEditTime()/1000000000, 0),
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 04:31:26 +03:00
|
|
|
sort.Slice(entries, func(i, j int) bool { return entries[i].ModTime().After(entries[j].ModTime()) })
|
2018-02-16 02:50:48 +03:00
|
|
|
return entries
|
|
|
|
}
|
|
|
|
|
2018-04-01 04:31:26 +03:00
|
|
|
type UploadEntry struct {
|
|
|
|
os.FileInfo
|
|
|
|
}
|
|
|
|
|
2018-04-28 12:29:35 +03:00
|
|
|
func (s *Site) UploadList() ([]os.FileInfo, error) {
|
|
|
|
paths, err := filepath.Glob(path.Join(s.PathToData, "sha256*"))
|
2018-04-01 04:31:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := make([]os.FileInfo, len(paths))
|
|
|
|
for i := range paths {
|
|
|
|
result[i], err = os.Stat(paths[i])
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-02-16 02:50:48 +03:00
|
|
|
func DecodeFileName(s string) string {
|
|
|
|
s2, _ := decodeFromBase32(strings.Split(s, ".")[0])
|
|
|
|
return s2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update cleans the text and updates the versioned text
|
|
|
|
// and generates a new render
|
|
|
|
func (p *Page) Update(newText string) error {
|
|
|
|
// Trim space from end
|
|
|
|
newText = strings.TrimRight(newText, "\n\t ")
|
|
|
|
|
|
|
|
// Update the versioned text
|
|
|
|
p.Text.Update(newText)
|
|
|
|
|
|
|
|
// Render the new page
|
|
|
|
p.Render()
|
|
|
|
|
|
|
|
return p.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
var rBracketPage = regexp.MustCompile(`\[\[(.*?)\]\]`)
|
|
|
|
|
|
|
|
func (p *Page) Render() {
|
|
|
|
if p.IsEncrypted {
|
|
|
|
p.RenderedPage = "<code>" + p.Text.GetCurrent() + "</code>"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert [[page]] to [page](/page/view)
|
|
|
|
currentText := p.Text.GetCurrent()
|
|
|
|
for _, s := range rBracketPage.FindAllString(currentText, -1) {
|
|
|
|
currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 1)
|
|
|
|
}
|
|
|
|
p.Text.Update(currentText)
|
|
|
|
p.RenderedPage = MarkdownToHtml(p.Text.GetCurrent())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Save() error {
|
2018-04-28 12:32:48 +03:00
|
|
|
p.Site.saveMut.Lock()
|
|
|
|
defer p.Site.saveMut.Unlock()
|
2018-02-16 02:50:48 +03:00
|
|
|
bJSON, err := json.MarshalIndent(p, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-28 12:29:35 +03:00
|
|
|
return ioutil.WriteFile(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644)
|
2018-02-16 02:50:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) ChildPageNames() []string {
|
|
|
|
prefix := strings.ToLower(p.Name + ": ")
|
2018-04-28 12:29:35 +03:00
|
|
|
files, err := filepath.Glob(path.Join(p.Site.PathToData, "*"))
|
2018-02-16 02:50:48 +03:00
|
|
|
if err != nil {
|
|
|
|
panic("Filepath pattern cannot be malformed")
|
|
|
|
}
|
|
|
|
|
|
|
|
result := []string{}
|
|
|
|
for i := range files {
|
|
|
|
basename := filepath.Base(files[i])
|
|
|
|
if strings.HasSuffix(basename, ".json") {
|
|
|
|
cname, err := decodeFromBase32(basename[:len(basename)-len(".json")])
|
|
|
|
if err == nil && strings.HasPrefix(strings.ToLower(cname), prefix) {
|
|
|
|
result = append(result, cname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) IsNew() bool {
|
2018-04-28 12:29:35 +03:00
|
|
|
return !exists(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
|
2018-02-16 02:50:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Erase() error {
|
2018-04-28 05:30:13 +03:00
|
|
|
p.Site.Logger.Trace("Erasing " + p.Name)
|
2018-04-28 12:29:35 +03:00
|
|
|
return os.Remove(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
|
2018-02-16 02:50:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Published() bool {
|
|
|
|
return p.IsPublished
|
|
|
|
}
|