flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 8834a9bcd3e4c3fc0511cda6c34defd6126405a9
parent 04c6d55b8d4967f11c4f7e795c7379469e0587d4
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 24 Oct 2020 15:00:16 -0700

timeago

Diffstat:
Mmain.go | 25++++---------------------
Mtemplates/index.html | 2+-
Autils.go | 39+++++++++++++++++++++++++++++++++++++++
3 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/main.go b/main.go @@ -3,7 +3,6 @@ package main import ( "database/sql" "flag" - "fmt" "github.com/gorilla/sessions" "io/ioutil" "log" @@ -11,7 +10,6 @@ import ( "path" "path/filepath" "sort" - "strings" "sync" "time" ) @@ -22,6 +20,7 @@ type File struct { Creator string Name string UpdatedTime time.Time + TimeAgo string } func getUsers() ([]string, error) { @@ -41,24 +40,6 @@ func getUsers() ([]string, error) { return users, nil } -/// Perform some checks to make sure the file is OK -func checkIfValidFile(filename string, fileBytes []byte) error { - ext := strings.ToLower(path.Ext(filename)) - found := false - for _, mimetype := range c.OkExtensions { - if ext == mimetype { - found = true - } - } - if !found { - return fmt.Errorf("Invalid file extension: %s", ext) - } - if len(fileBytes) > c.MaxFileSize { - return fmt.Errorf("File too large. File was %s bytes, Max file size is %s", len(fileBytes), c.MaxFileSize) - } - return nil -} - func getIndexFiles() ([]*File, error) { // cache this function result := []*File{} err := filepath.Walk(c.FilesDirectory, func(thepath string, info os.FileInfo, err error) error { @@ -69,10 +50,12 @@ func getIndexFiles() ([]*File, error) { // cache this function // make this do what it should if !info.IsDir() { creatorFolder, _ := path.Split(thepath) + updatedTime := info.ModTime() result = append(result, &File{ Name: info.Name(), Creator: path.Base(creatorFolder), - UpdatedTime: info.ModTime(), + UpdatedTime: updatedTime, + TimeAgo: timeago(&updatedTime), }) } return nil diff --git a/templates/index.html b/templates/index.html @@ -11,7 +11,7 @@ <div> <a href="https://{{.Creator}}.{{$domain}}" class='person-link'> {{ .Creator }}</a> - <em>{{.UpdatedTime}}</em> + <em>{{.TimeAgo}}</em> <a href="https://{{.Creator}}.{{$domain}}/{{.Name}}"> {{ .Name}} </a> diff --git a/utils.go b/utils.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "path" + "strings" + "time" +) + +func timeago(t *time.Time) string { + d := time.Since(*t) + if d.Seconds() < 60 { + return fmt.Sprintf("%d seconds ago", int(d.Seconds())) + } else if d.Minutes() < 60 { + return fmt.Sprintf("%d minutes ago", int(d.Minutes())) + } else if d.Hours() < 24 { + return fmt.Sprintf("%d hours ago", int(d.Hours())) + } else { + return fmt.Sprintf("%d days ago", int(d.Hours())/24) + } +} + +/// Perform some checks to make sure the file is OK +func checkIfValidFile(filename string, fileBytes []byte) error { + ext := strings.ToLower(path.Ext(filename)) + found := false + for _, mimetype := range c.OkExtensions { + if ext == mimetype { + found = true + } + } + if !found { + return fmt.Errorf("Invalid file extension: %s", ext) + } + if len(fileBytes) > c.MaxFileSize { + return fmt.Errorf("File too large. File was %s bytes, Max file size is %s", len(fileBytes), c.MaxFileSize) + } + return nil +}