-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileutil.go
169 lines (136 loc) · 3.27 KB
/
fileutil.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"encoding/json"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
)
func ReadFileUnsafe(file string, removeNewline bool) string {
content, err := ReadFile(file)
if err != nil {
log.Printf("- Failed to read '%s'", file)
return content
}
lastByte, cb := LastByte(content)
if removeNewline && lastByte == 10 {
cb = cb[:len(cb)-1]
return string(cb)
}
return content
}
func ReadFile(file string) (string, error) {
dat, err := os.ReadFile(file)
return string(dat), err
}
func ReadUserTokens() map[string]UserToken {
dat, err := os.ReadFile("user_tokens.json")
if err != nil {
return map[string]UserToken{}
}
var tokens map[string]UserToken
if err := json.Unmarshal(dat, &tokens); err != nil || tokens == nil {
return map[string]UserToken{}
}
return tokens
}
func WriteToFile(file string, content string) error {
data := []byte(content)
err := os.WriteFile(file, data, 0600)
if err != nil {
log.Printf("- Failed to read '%s'", file)
}
return err
}
func IsDirectory(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
if fi.Mode().IsDir() {
return true, nil
} else {
return false, nil
}
}
func GetFileContentTypeExt(out *os.File, file string) (string, error) {
ext := filepath.Ext(file)
switch ext {
case ".txt", ".text":
return "text/plain; charset=utf-8", nil
case ".htm", ".html":
return "text/html", nil
case ".css":
return "text/css", nil
case ".js", ".mjs":
return "application/javascript", nil
case ".mov":
return "video/quicktime", nil
case ".json":
return "application/json; charset=utf-8", nil
}
return GetFileContentType(out)
}
// GetFileContentType detects the content type
// and returns a valid MIME type
func GetFileContentType(out *os.File) (string, error) {
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err := out.Read(buffer)
if err != nil {
return "", err
}
// Use the net/http package's handy DetectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)
return contentType, nil
}
// ReadLines reads a whole file into memory
// and returns a slice of its lines.
func ReadLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// ReadNonEmptyLines reads non-empty lines
// using ReadLines and a filter.
func ReadNonEmptyLines(file string, prefix string) (ret []string) {
lines, err := ReadLines(file)
if err != nil {
return []string{}
}
for _, s := range lines {
lastByte, _ := LastByte(s)
// If not a blank line
if len(s) > 1 || lastByte != 10 {
ret = append(ret, prefix+s)
}
}
return ret
}
func SafeMkdir(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, ownerPerm)
if err != nil {
log.Fatalf("- Error making '%s' - %v", dir, err)
}
}
}
func Filter(ss []fs.FileInfo, test func(fs.FileInfo) bool) (ret []fs.FileInfo) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}