Skip to content

Commit b338b49

Browse files
committed
fix mimetypes... just detect images/pdfs... everything else is text. text files get scanned for binary chars. so we can upload dotfiles more effectively instead of dying because we cant figure out the mimetype.
1 parent dc3c2b5 commit b338b49

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

cmd/wsh/cmd/wshcmd-ai.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"encoding/base64"
88
"fmt"
99
"io"
10-
"mime"
10+
"net/http"
1111
"os"
1212
"path/filepath"
13+
"strings"
1314

1415
"github.com/spf13/cobra"
16+
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
1517
"github.com/wavetermdev/waveterm/pkg/wshrpc"
1618
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient"
1719
"github.com/wavetermdev/waveterm/pkg/wshutil"
@@ -47,23 +49,16 @@ func init() {
4749
aiCmd.Flags().BoolVarP(&aiNewBlockFlag, "new", "n", false, "create a new AI chat instead of using existing")
4850
}
4951

50-
func getMimeType(filename string) string {
51-
ext := filepath.Ext(filename)
52-
if ext == "" {
53-
return "text/plain"
54-
}
55-
mimeType := mime.TypeByExtension(ext)
56-
if mimeType == "" {
57-
return "text/plain"
58-
}
59-
return mimeType
52+
func detectMimeType(data []byte) string {
53+
mimeType := http.DetectContentType(data)
54+
return strings.Split(mimeType, ";")[0]
6055
}
6156

6257
func getMaxFileSize(mimeType string) (int, string) {
6358
if mimeType == "application/pdf" {
6459
return 5 * 1024 * 1024, "5MB"
6560
}
66-
if mimeType[:6] == "image/" {
61+
if strings.HasPrefix(mimeType, "image/") {
6762
return 7 * 1024 * 1024, "7MB"
6863
}
6964
return 200 * 1024, "200KB"
@@ -123,7 +118,17 @@ func aiRun(cmd *cobra.Command, args []string) (rtnErr error) {
123118
return fmt.Errorf("reading file %s: %w", filePath, err)
124119
}
125120
fileName = filepath.Base(filePath)
126-
mimeType = getMimeType(filePath)
121+
mimeType = detectMimeType(data)
122+
}
123+
124+
isPDF := mimeType == "application/pdf"
125+
isImage := strings.HasPrefix(mimeType, "image/")
126+
127+
if !isPDF && !isImage {
128+
mimeType = "text/plain"
129+
if utilfn.ContainsBinaryData(data) {
130+
return fmt.Errorf("file %s contains binary data and cannot be uploaded as text", fileName)
131+
}
127132
}
128133

129134
maxSize, sizeStr := getMaxFileSize(mimeType)

pkg/util/utilfn/marshal.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,17 @@ func DecodeDataURL(dataURL string) (mimeType string, data []byte, err error) {
201201
}
202202
return mimeType, []byte(decoded), nil
203203
}
204+
205+
206+
// ContainsBinaryData checks if the provided data contains binary (non-text) content
207+
func ContainsBinaryData(data []byte) bool {
208+
for _, b := range data {
209+
if b == 0 {
210+
return true
211+
}
212+
if b < 32 && b != 9 && b != 10 && b != 13 {
213+
return true
214+
}
215+
}
216+
return false
217+
}

0 commit comments

Comments
 (0)