Repository navigation
Expand file tree
/
Copy pathutils.go
More file actions
50 lines (41 loc) · 1.29 KB
/
Copy pathutils.go
File metadata and controls
50 lines (41 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package shortpaste
import (
"encoding/json"
"fmt"
"net/http"
"gopkg.in/go-playground/validator.v9"
)
func (link *Link) validate() error {
return validator.New().Struct(link)
}
func (file *File) validate() error {
return validator.New().Struct(file)
}
func (text *Text) validate() error {
return validator.New().Struct(text)
}
func onClientError(w http.ResponseWriter, err error, msg string) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("%v", err), "message": msg})
}
func onNotFound(w http.ResponseWriter, msg string) {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"message": msg})
}
func onServerError(w http.ResponseWriter, err error, msg string) {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("%v", err), "message": msg})
}
//IECFormat prints bytes in the International Electrotechnical Commission format
func IECFormat(sizeBytes int64) string {
suffix := "B"
num := float64(sizeBytes)
units := []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"}
for _, unit := range units {
if num < 1024.0 {
return fmt.Sprintf("%3.1f%s%s", num, unit, suffix)
}
num = (num / 1024)
}
return fmt.Sprintf("%.1f%s%s", num, "Yi", suffix)
}