-
Notifications
You must be signed in to change notification settings - Fork 158
/
utils.go
48 lines (41 loc) · 1.1 KB
/
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
package utils
import (
"os"
"strings"
"sync"
"go.uber.org/zap"
"github.com/dreadl0ck/netcap/defaults"
)
// keep track which paths for content types of extracted files have already been created.
var (
contentTypeMap = make(map[string]struct{})
contentTypeMapMu sync.Mutex
)
// createContentTypePathIfRequired will create the passed in filesystem path once
// it is safe for concurrent access and will block until the path has been created on disk.
func createContentTypePathIfRequired(fsPath string) {
contentTypeMapMu.Lock()
if _, ok := contentTypeMap[fsPath]; !ok { // the path has not been created yet
// add to map
contentTypeMap[fsPath] = struct{}{}
// create path
err := os.MkdirAll(fsPath, defaults.DirectoryPermission)
if err != nil {
reassemblyLog.Error(
"failed to create folder",
zap.String("path", fsPath),
zap.Error(err),
)
}
}
// free lock again
contentTypeMapMu.Unlock()
}
// trimEncoding removes encoding from a MIME type.
func trimEncoding(ctype string) string {
parts := strings.Split(ctype, ";")
if len(parts) > 1 {
return parts[0]
}
return ctype
}