@@ -9,14 +9,14 @@ import (
99 "image/png"
1010 "io"
1111 "mime/multipart"
12- "net/http"
1312 "os"
1413 "path"
1514 "strconv"
1615 "strings"
1716 "time"
1817
1918 "github.com/Bajahaw/ai-ui/cmd/providers"
19+ "github.com/gabriel-vasile/mimetype"
2020
2121 "github.com/google/uuid"
2222)
@@ -135,27 +135,24 @@ func saveUploadedFile(file multipart.File, handler *multipart.FileHeader, user s
135135}
136136
137137func detectFileType (file multipart.File ) (string , error ) {
138- buf := make ([]byte , 512 )
139- n , err := file .Read (buf )
140- if err != nil && err != io .EOF {
138+ mtype , err := mimetype .DetectReader (file )
139+ if err != nil {
141140 return "" , err
142141 }
143- if n == 0 {
142+ if mtype . String () == "" {
144143 return "" , fmt .Errorf ("empty file" )
145144 }
146145
147- fileType := http .DetectContentType (buf [:n ])
148- log .Debug ("Uploaded file type" , "type" , fileType )
146+ log .Debug ("Uploaded file type" , "type" , mtype .String ())
149147
150- // Reset file read pointer: prefer Seek if available
148+ // Reset file read pointer
151149 if seeker , ok := file .(io.Seeker ); ok {
152- _ , err = seeker .Seek (0 , io .SeekStart )
153- if err != nil {
150+ if _ , err := seeker .Seek (0 , io .SeekStart ); err != nil {
154151 return "" , err
155152 }
156153 }
157154
158- return fileType , nil
155+ return mtype . String () , nil
159156}
160157
161158// compressImage reads an image from r and re-encodes it with conservative
@@ -210,21 +207,21 @@ func extractFileContent(file File, model string) (string, error) {
210207 return string (fileContent ), nil
211208 }
212209
213- if file .Type == "application/pdf" {
214- pages , err := readPDFPages (file .Path , file .ID )
210+ if IsRetrievableDoc ( file .Type ) {
211+ pages , err := readDocPages (file .Path , file .ID )
215212 if err != nil {
216- log .Error ("Error reading PDF pages" , "err" , err )
213+ log .Error ("Error reading document pages" , "err" , err , "type" , file . Type )
217214 return "" , err
218215 }
219216
220217 // indexing all pages individually to allow retrieval of specific pages later.
221218 err = repo .SavePages (pages )
222219 if err != nil {
223- log .Error ("Error saving PDF pages" , "err" , err )
220+ log .Error ("Error saving document pages" , "err" , err )
224221 return "" , err
225222 }
226223
227- return "PDF content page 1: \n \n " + pages [0 ].Content + "... retrieve rest of content using tools" , nil
224+ return "Document content page 1: \n \n " + pages [0 ].Content + "... retrieve rest of content using tools" , nil
228225 }
229226
230227 if strings .HasPrefix (file .Type , "image/" ) {
@@ -259,3 +256,20 @@ func extractFileContent(file File, model string) (string, error) {
259256
260257 return "" , fmt .Errorf ("unsupported file type for content extraction: %s" , file .Type )
261258}
259+
260+ // IsRetrievableDoc returns true for document types supported by go-fitz
261+ // that can be page-extracted and searched via agentic retrieval tools.
262+ func IsRetrievableDoc (mimeType string ) bool {
263+ switch mimeType {
264+ case "application/pdf" ,
265+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
266+ "application/vnd.openxmlformats-officedocument.presentationml.presentation" ,
267+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
268+ "application/vnd.oasis.opendocument.text" ,
269+ "application/vnd.oasis.opendocument.presentation" ,
270+ "application/vnd.oasis.opendocument.spreadsheet" ,
271+ "application/epub+zip" :
272+ return true
273+ }
274+ return false
275+ }
0 commit comments