-
Notifications
You must be signed in to change notification settings - Fork 16
/
file_helper.go
107 lines (100 loc) · 2.63 KB
/
file_helper.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
package cloudstorage
import (
"fmt"
"mime"
"os"
"path"
"path/filepath"
"strings"
)
// CleanETag transforms a string into the full etag spec, removing
// extra quote-marks, whitespace from etag.
//
// per Etag spec https://tools.ietf.org/html/rfc7232#section-2.3 the etag value (<ETAG VALUE>) may:
// - W/"<ETAG VALUE>"
// - "<ETAG VALUE>"
// - ""
func CleanETag(etag string) string {
for {
// loop through checking for extra-characters and removing
if strings.HasPrefix(etag, `\"`) {
etag = strings.Trim(etag, `\"`)
} else if strings.HasPrefix(etag, `"`) {
etag = strings.Trim(etag, `"`)
} else if strings.HasPrefix(etag, `W/`) {
etag = strings.Replace(etag, `W/`, "", 1)
} else {
// as soon as no condition matches, we are done
// return
return etag
}
}
}
// ContentType check content type of file by looking
// at extension (.html, .png) uses package mime for global types.
// Use mime.AddExtensionType to add new global types.
func ContentType(name string) string {
contenttype := ""
ext := filepath.Ext(name)
if contenttype == "" {
contenttype = mime.TypeByExtension(ext)
if contenttype == "" {
contenttype = "application/octet-stream"
}
}
return contenttype
}
// EnsureContextType read Type of metadata
func EnsureContextType(o string, md map[string]string) string {
ctype, ok := md[ContentTypeKey]
if !ok {
ext := filepath.Ext(o)
if ctype == "" {
ctype = mime.TypeByExtension(ext)
if ctype == "" {
ctype = "application/octet-stream"
}
}
md[ContentTypeKey] = ctype
}
return ctype
}
// Exists does this file path exists on the local file-system?
func Exists(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}
// CachePathObj check the cache path.
func CachePathObj(cachepath, oname, storeid string) string {
obase := path.Base(oname)
opath := path.Dir(oname)
ext := path.Ext(oname)
ext2 := fmt.Sprintf("%s.%s%s", ext, storeid, StoreCacheFileExt)
var obase2 string
if ext == "" {
obase2 = obase + ext2
} else {
obase2 = strings.Replace(obase, ext, ext2, 1)
}
return path.Join(cachepath, opath, obase2)
}
// EnsureDir ensure directory exists
func EnsureDir(filename string) error {
fdir := path.Dir(filename)
if fdir != "" && fdir != filename {
d, err := os.Stat(fdir)
if err == nil {
if !d.IsDir() {
return fmt.Errorf("filename's dir exists but isn't' a directory: filename:%v dir:%v", filename, fdir)
}
} else if os.IsNotExist(err) {
err := os.MkdirAll(fdir, 0775)
if err != nil {
return fmt.Errorf("unable to create path. : filename:%v dir:%v err:%v", filename, fdir, err)
}
}
}
return nil
}