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
43func userHasSpace(user string, newBytes int) bool {
44 userPath := path.Join(c.FilesDirectory, user)
45 size, err := dirSize(userPath)
46 if err != nil || size+int64(newBytes) > c.MaxUserBytes {
47 return false
48 }
49 return true
50}
51
52func dirSize(path string) (int64, error) {
53 var size int64
54 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
55 if err != nil {
56 return err
57 }
58 if !info.IsDir() {
59 size += info.Size()
60 }
61 return err
62 })
63 return size, err
64}
65
66/// Perform some checks to make sure the file is OK
67func checkIfValidFile(filename string, fileBytes []byte) error {
68 if len(filename) == 0 {
69 return fmt.Errorf("Please enter a filename")
70 }
71 if len(filename) > 256 { // arbitrarily chosen
72 return fmt.Errorf("Filename is too long")
73 }
74 ext := strings.ToLower(path.Ext(filename))
75 found := false
76 for _, mimetype := range c.OkExtensions {
77 if ext == mimetype {
78 found = true
79 }
80 }
81 if !found {
82 return fmt.Errorf("Invalid file extension: %s", ext)
83 }
84 if len(fileBytes) > c.MaxFileBytes {
85 return fmt.Errorf("File too large. File was %d bytes, Max file size is %d", len(fileBytes), c.MaxFileBytes)
86 }
87 //
88 return nil
89}
90
91func zipit(source string, target io.Writer) error {
92 archive := zip.NewWriter(target)
93
94 info, err := os.Stat(source)
95 if err != nil {
96 return nil
97 }
98
99 var baseDir string
100 if info.IsDir() {
101 baseDir = filepath.Base(source)
102 }
103
104 filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
105 if err != nil {
106 return err
107 }
108
109 header, err := zip.FileInfoHeader(info)
110 if err != nil {
111 return err
112 }
113
114 if baseDir != "" {
115 header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
116 }
117
118 if info.IsDir() {
119 header.Name += "/"
120 } else {
121 header.Method = zip.Deflate
122 }
123
124 writer, err := archive.CreateHeader(header)
125 if err != nil {
126 return err
127 }
128
129 if info.IsDir() {
130 return nil
131 }
132
133 file, err := os.Open(path)
134 if err != nil {
135 return err
136 }
137 defer file.Close()
138 _, err = io.Copy(writer, file)
139 return err
140 })
141
142 archive.Close()
143
144 return err
145}