@@ -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
6257func 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 )
0 commit comments