-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinspector.go
More file actions
35 lines (27 loc) · 834 Bytes
/
inspector.go
File metadata and controls
35 lines (27 loc) · 834 Bytes
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
package baraka
import "net/http"
// Inspector is a interface for finding the content type of the byte array
type Inspector interface {
Inspect(data []byte) string
}
// DefaultInspector is the default inspector
// uses http.DetectContentType function to find the content type
type DefaultInspector struct {
maxSampleSize int
}
// NewDefaultInspector creates a new default inspector
func NewDefaultInspector(maxSampleSize int) Inspector {
return &DefaultInspector{
maxSampleSize: maxSampleSize,
}
}
// Inspect finds the content type of the byte array
func (inspector *DefaultInspector) Inspect(data []byte) string {
maxSampleSize := inspector.maxSampleSize
if len(data) < maxSampleSize {
maxSampleSize = len(data)
}
sample := data[:maxSampleSize]
contentType := http.DetectContentType(sample)
return contentType
}