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