-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
executable file
·51 lines (40 loc) · 914 Bytes
/
utils.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
package gositemap
import (
"compress/gzip"
"fmt"
"os"
)
func writeFile(dir, fullPath string, data []byte, compress bool) error {
if compress {
fullPath = fullPath + ".gz"
}
fi, err := os.Stat(dir)
if err != nil {
_ = os.MkdirAll(dir, 0750)
} else if !fi.IsDir() {
return fmt.Errorf("[F] %s should be a directory", dir)
}
file, _ := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
fi, err = file.Stat()
if err != nil {
return fmt.Errorf("[F] %s file not exists", fullPath)
} else if !fi.Mode().IsRegular() {
return fmt.Errorf("[F] %s should be a filename", fullPath)
}
if compress {
return gzipSitemap(file, data)
}
_, err = file.Write(data)
if err != nil {
return err
}
return file.Close()
}
func gzipSitemap(file *os.File, data []byte) error {
gz := gzip.NewWriter(file)
_, err := gz.Write(data)
if err != nil {
return err
}
return gz.Close()
}