utils.go (view raw)
1package main
2
3import (
4 "fmt"
5 "path"
6 "strings"
7 "time"
8)
9
10func timeago(t *time.Time) string {
11 d := time.Since(*t)
12 if d.Seconds() < 60 {
13 return fmt.Sprintf("%d seconds ago", int(d.Seconds()))
14 } else if d.Minutes() < 60 {
15 return fmt.Sprintf("%d minutes ago", int(d.Minutes()))
16 } else if d.Hours() < 24 {
17 return fmt.Sprintf("%d hours ago", int(d.Hours()))
18 } else {
19 return fmt.Sprintf("%d days ago", int(d.Hours())/24)
20 }
21}
22
23/// Perform some checks to make sure the file is OK
24func checkIfValidFile(filename string, fileBytes []byte) error {
25 if len(filename) == 0 {
26 return fmt.Errorf("Please enter a filename")
27 }
28 ext := strings.ToLower(path.Ext(filename))
29 found := false
30 for _, mimetype := range c.OkExtensions {
31 if ext == mimetype {
32 found = true
33 }
34 }
35 if !found {
36 return fmt.Errorf("Invalid file extension: %s", ext)
37 }
38 fmt.Println(len(fileBytes))
39 if len(fileBytes) > c.MaxFileSize {
40 return fmt.Errorf("File too large. File was %d bytes, Max file size is %d", len(fileBytes), c.MaxFileSize)
41 }
42 return nil
43}