forked from hairyhenderson/go-fsimpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.go
89 lines (75 loc) · 2.2 KB
/
extensions.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
package fsimpl
import (
"context"
"io/fs"
"mime"
"net/http"
"path/filepath"
"sync"
"github.com/helmwave/go-fsimpl/internal"
)
// WithContextFS injects a context into the filesystem fs, if the filesystem
// supports it (i.e. has a WithContext method). This can be used to propagate
// cancellation.
func WithContextFS(ctx context.Context, fsys fs.FS) fs.FS {
if cfsys, ok := fsys.(internal.WithContexter); ok {
return cfsys.WithContext(ctx)
}
return fsys
}
// WithHeaderFS injects the given HTTP header into the filesystem fs, if the
// filesystem supports it (i.e. has a WithHeader method).
func WithHeaderFS(headers http.Header, fsys fs.FS) fs.FS {
if cfsys, ok := fsys.(internal.WithHeaderer); ok {
return cfsys.WithHeader(headers)
}
return fsys
}
// WithHTTPClientFS injects an HTTP client into the filesystem fs, if the
// filesystem supports it (i.e. has a WithHTTPClient method).
func WithHTTPClientFS(client *http.Client, fsys fs.FS) fs.FS {
if cfsys, ok := fsys.(internal.WithHTTPClienter); ok {
return cfsys.WithHTTPClient(client)
}
return fsys
}
// common types we want to be able to handle which can be missing by default
//
//nolint:gochecknoglobals
var (
extraMimeTypes = map[string]string{
".yml": "application/yaml",
".yaml": "application/yaml",
".csv": "text/csv",
".toml": "application/toml",
".env": "application/x-env",
".txt": "text/plain",
}
extraMimeInit sync.Once
)
// ContentType returns the MIME content type for the given [io/fs.FileInfo]. If
// fi has a ContentType method, it will be used first, otherwise the filename's
// extension will be used. See the docs for [mime.TypeByExtension] for details
// on how extension lookup works.
//
// The returned value may have parameters (e.g.
// "application/json; charset=utf-8") which can be parsed with
// [mime.ParseMediaType].
func ContentType(fi fs.FileInfo) string {
ct := ""
if cf, ok := fi.(internal.ContentTypeFileInfo); ok {
ct = cf.ContentType()
}
if ct != "" {
return ct
}
extraMimeInit.Do(func() {
for k, v := range extraMimeTypes {
_ = mime.AddExtensionType(k, v)
}
})
// fall back to guessing based on extension
ext := filepath.Ext(fi.Name())
ct = mime.TypeByExtension(ext)
return ct
}