-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary.go
More file actions
144 lines (122 loc) · 3.59 KB
/
binary.go
File metadata and controls
144 lines (122 loc) · 3.59 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package probe
import (
"crypto/rand"
"fmt"
"mime"
"os"
"path/filepath"
"strings"
)
// IsTextualMimeType determines if the given MIME type represents textual data
func IsTextualMimeType(contentType string) bool {
if contentType == "" {
return true // Default to text if no content type
}
// Extract the main MIME type (before semicolon if present)
mimeType := strings.Split(contentType, ";")[0]
mimeType = strings.TrimSpace(strings.ToLower(mimeType))
// Textual MIME types
textualTypes := []string{
"text/",
"application/json",
"application/xml",
"application/javascript",
"application/x-javascript",
"application/x-www-form-urlencoded",
"application/xhtml+xml",
"application/rss+xml",
"application/atom+xml",
"application/ld+json",
}
for _, textType := range textualTypes {
if strings.HasPrefix(mimeType, textType) {
return true
}
}
return false
}
// SaveBinaryToTempFile saves binary data to a temporary file and returns the file path
func SaveBinaryToTempFile(data []byte, contentType string) (string, error) {
if len(data) == 0 {
return "", nil
}
// Generate a unique filename
randBytes := make([]byte, 8)
if _, err := rand.Read(randBytes); err != nil {
return "", fmt.Errorf("failed to generate random filename: %w", err)
}
filename := fmt.Sprintf("probe_binary_%x", randBytes)
// Determine file extension from MIME type
if ext := getExtensionFromMimeType(contentType); ext != "" {
filename += ext
}
// Create file in temporary directory
tempDir := os.TempDir()
filePath := filepath.Join(tempDir, filename)
// Write data to file
file, err := os.Create(filePath)
if err != nil {
return "", fmt.Errorf("failed to create temp file: %w", err)
}
defer func() { _ = file.Close() }()
if _, err := file.Write(data); err != nil {
_ = os.Remove(filePath) // Clean up on error
return "", fmt.Errorf("failed to write data to temp file: %w", err)
}
return filePath, nil
}
// getExtensionFromMimeType returns the appropriate file extension for a MIME type
func getExtensionFromMimeType(contentType string) string {
if contentType == "" {
return ""
}
mimeType := strings.Split(contentType, ";")[0]
mimeType = strings.TrimSpace(strings.ToLower(mimeType))
// Preferred extensions for common types (override mime package defaults)
extensionMap := map[string]string{
"image/jpeg": ".jpg",
"image/jpg": ".jpg",
"image/png": ".png",
"image/gif": ".gif",
"image/webp": ".webp",
"image/svg+xml": ".svg",
"video/mp4": ".mp4",
"video/webm": ".webm",
"video/quicktime": ".mov",
"audio/mp3": ".mp3",
"audio/mpeg": ".mp3",
"audio/wav": ".wav",
"application/pdf": ".pdf",
"application/zip": ".zip",
"application/gzip": ".gz",
"text/plain": ".txt",
"application/json": ".json",
}
// Check our preferred extensions first
if ext, exists := extensionMap[mimeType]; exists {
return ext
}
// Fallback to mime package for other types
if exts, err := mime.ExtensionsByType(mimeType); err == nil && len(exts) > 0 {
return exts[0] // Return the first/most common extension
}
return ""
}
// ProcessHttpBody processes HTTP body data based on Content-Type
// Returns (bodyString, filePath, error)
func ProcessHttpBody(data []byte, contentType string) (string, string, error) {
if len(data) == 0 {
return "", "", nil
}
if IsTextualMimeType(contentType) {
// Return as text body
return string(data), "", nil
} else {
// Save as binary file
filePath, err := SaveBinaryToTempFile(data, contentType)
if err != nil {
return "", "", err
}
return "", filePath, nil
}
}