timeago
alex wennerberg alex@alexwennerberg.com
Sat, 24 Oct 2020 15:00:16 -0700
3 files changed,
44 insertions(+),
22 deletions(-)
M
main.go
→
main.go
@@ -3,7 +3,6 @@
import ( "database/sql" "flag" - "fmt" "github.com/gorilla/sessions" "io/ioutil" "log"@@ -11,7 +10,6 @@ "os"
"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 @@ }
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 @@ }
// 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
M
templates/index.html
→
templates/index.html
@@ -11,7 +11,7 @@ {{ range .Files }}
<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>
A
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 +}