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

Add route /ls for viewing directory

Former-commit-id: 53ace40488
This commit is contained in:
Zack Scholl 2017-03-24 07:58:37 -06:00
parent 8e6b798be5
commit 82b16ec121
4 changed files with 69 additions and 0 deletions

View File

@ -140,6 +140,13 @@ func handlePageRequest(c *gin.Context) {
command[0:2] != "/l" && command[0:2] != "/l" &&
command[0:2] != "/h") command[0:2] != "/h")
var FileNames, FileLastEdited []string
var FileSizes, FileNumChanges []int
if page == "ls" {
command = "/view"
FileNames, FileSizes, FileNumChanges, FileLastEdited = DirectoryList()
}
c.HTML(http.StatusOK, "index.tmpl", gin.H{ c.HTML(http.StatusOK, "index.tmpl", gin.H{
"EditPage": command[0:2] == "/e", // /edit "EditPage": command[0:2] == "/e", // /edit
"ViewPage": command[0:2] == "/v", // /view "ViewPage": command[0:2] == "/v", // /view
@ -149,6 +156,11 @@ func handlePageRequest(c *gin.Context) {
command[0:2] != "/v" && command[0:2] != "/v" &&
command[0:2] != "/l" && command[0:2] != "/l" &&
command[0:2] != "/h", command[0:2] != "/h",
"DirectoryPage": page == "ls",
"FileNames": FileNames,
"FileSizes": FileSizes,
"FileNumChanges": FileNumChanges,
"FileLastEdited": FileLastEdited,
"Page": page, "Page": page,
"RenderedPage": template.HTML([]byte(rawHTML)), "RenderedPage": template.HTML([]byte(rawHTML)),
"RawPage": rawText, "RawPage": rawText,

22
page.go
View File

@ -7,6 +7,7 @@ import (
"path" "path"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/schollz/versionedtext" "github.com/schollz/versionedtext"
) )
@ -38,6 +39,27 @@ func Open(name string) (p *Page) {
return p return p
} }
func DirectoryList() (names []string, lengths []int, numchanges []int, lastEdited []string) {
files, _ := ioutil.ReadDir(pathToData)
names = make([]string, len(files))
lengths = make([]int, len(files))
numchanges = make([]int, len(files))
lastEdited = make([]string, len(files))
for i, f := range files {
names[i] = DecodeFileName(f.Name())
p := Open(names[i])
lengths[i] = len(p.Text.GetCurrent())
numchanges[i] = p.Text.NumEdits()
lastEdited[i] = time.Unix(p.Text.LastEditTime()/1000000000, 0).Format("Mon Jan 2 15:04:05 MST 2006")
}
return
}
func DecodeFileName(s string) string {
s2, _ := decodeFromBase32(strings.Split(s, ".")[0])
return s2
}
func (p *Page) Update(newText string) error { func (p *Page) Update(newText string) error {
p.Text.Update(newText) p.Text.Update(newText)
p.Render() p.Render()

View File

@ -7,6 +7,22 @@ import (
"testing" "testing"
) )
func TestListFiles(t *testing.T) {
pathToData = "testdata"
os.MkdirAll(pathToData, 0755)
defer os.RemoveAll(pathToData)
p := Open("testpage")
p.Update("Some data")
p = Open("testpage2")
p.Update("A different bunch of data")
p = Open("testpage3")
p.Update("Not much else")
n, l := DirectoryList()
if strings.Join(n, " ") != "testpage testpage2 testpage3" {
t.Errorf("Names: %s, Lengths: %d", n, l)
}
}
func TestGeneral(t *testing.T) { func TestGeneral(t *testing.T) {
pathToData = "testdata" pathToData = "testdata"
os.MkdirAll(pathToData, 0755) os.MkdirAll(pathToData, 0755)

View File

@ -453,6 +453,25 @@
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if .DirectoryPage }}
<table style="width:100%">
<tr>
<th>Document</th>
<th>Current size</th>
<th>Num Edits</th>
<th>Last Edited</th>
</tr>
{{range $i, $e := .FileNames}}
<tr>
<td><a href="/{{ $e }}/view">{{ $e }}</a></td>
<td>{{index $.FileSizes $i}}</td>
<td>{{index $.FileNumChanges $i}}</td>
<td>{{index $.FileLastEdited $i}}</td>
</tr>
{{ end }}
</table>
{{end}}
</div> </div>
</article> </article>