utils.go (view raw)
1package main
2
3import (
4 "archive/zip"
5 "fmt"
6 "io"
7 "os"
8 "path"
9 "path/filepath"
10 "strings"
11 "time"
12)
13
14func timeago(t *time.Time) string {
15 d := time.Since(*t)
16 if d.Seconds() < 60 {
17 seconds := int(d.Seconds())
18 if seconds == 1 {
19 return "1 second ago"
20 }
21 return fmt.Sprintf("%d seconds ago", seconds)
22 } else if d.Minutes() < 60 {
23 minutes := int(d.Minutes())
24 if minutes == 1 {
25 return "1 minute ago"
26 }
27 return fmt.Sprintf("%d minutes ago", minutes)
28 } else if d.Hours() < 24 {
29 hours := int(d.Hours())
30 if hours == 1 {
31 return "1 hour ago"
32 }
33 return fmt.Sprintf("%d hours ago", hours)
34 } else {
35 days := int(d.Hours()) / 24
36 if days == 1 {
37 return "1 day ago"
38 }
39 return fmt.Sprintf("%d days ago", days)
40 }
41}
42
43// TODO move into checkIfValidFile. rename it
44func userHasSpace(user string, newBytes int) bool {
45 userPath := path.Join(c.FilesDirectory, user)
46 size, err := dirSize(userPath)
47 if err != nil || size+int64(newBytes) > c.MaxUserBytes {
48 return false
49 }
50 return true
51}
52
53func dirSize(path string) (int64, error) {
54 var size int64
55 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
56 if err != nil {
57 return err
58 }
59 if !info.IsDir() {
60 size += info.Size()
61 }
62 return err
63 })
64 return size, err
65}
66
67/// Perform some checks to make sure the file is OK
68func checkIfValidFile(filename string, fileBytes []byte) error {
69 if len(filename) == 0 {
70 return fmt.Errorf("Please enter a filename")
71 }
72 if len(filename) > 256 { // arbitrarily chosen
73 return fmt.Errorf("Filename is too long")
74 }
75 ext := strings.ToLower(path.Ext(filename))
76 found := false
77 for _, mimetype := range c.OkExtensions {
78 if ext == mimetype {
79 found = true
80 }
81 }
82 if !found {
83 return fmt.Errorf("Invalid file extension: %s", ext)
84 }
85 if len(fileBytes) > c.MaxFileBytes {
86 return fmt.Errorf("File too large. File was %d bytes, Max file size is %d", len(fileBytes), c.MaxFileBytes)
87 }
88 //
89 return nil
90}
91
92func zipit(source string, target io.Writer) error {
93 archive := zip.NewWriter(target)
94
95 info, err := os.Stat(source)
96 if err != nil {
97 return nil
98 }
99
100 var baseDir string
101 if info.IsDir() {
102 baseDir = filepath.Base(source)
103 }
104
105 filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
106 if err != nil {
107 return err
108 }
109
110 header, err := zip.FileInfoHeader(info)
111 if err != nil {
112 return err
113 }
114
115 if baseDir != "" {
116 header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
117 }
118
119 if info.IsDir() {
120 header.Name += "/"
121 } else {
122 header.Method = zip.Deflate
123 }
124
125 writer, err := archive.CreateHeader(header)
126 if err != nil {
127 return err
128 }
129
130 if info.IsDir() {
131 return nil
132 }
133
134 file, err := os.Open(path)
135 if err != nil {
136 return err
137 }
138 defer file.Close()
139 _, err = io.Copy(writer, file)
140 return err
141 })
142
143 archive.Close()
144
145 return err
146}