Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(knowledge): allow xlsx and other file types in URL import (#2447)

wizardchen48756d13d60e
diff --git a/internal/application/service/knowledge_create.go b/internal/application/service/knowledge_create.go
index e12a7d9cc..4d2e099cf 100644
--- a/internal/application/service/knowledge_create.go
+++ b/internal/application/service/knowledge_create.go
@@ -289,7 +289,7 @@ func isFileURL(rawURL, fileName, fileType string) bool {
u, err := url.Parse(rawURL)
if err == nil {
ext := strings.ToLower(strings.TrimPrefix(path.Ext(u.Path), "."))
- if ext != "" && allowedFileURLExtensions[ext] {
+ if ext != "" && isAllowedFileURLExtension(ext) {
return true
}
}
@@ -467,20 +467,6 @@ func (s *knowledgeService) CreateKnowledgeFromURL(ctx context.Context,
return knowledge, nil
}
-// allowedFileURLExtensions defines the supported file extensions for file URL import
-var allowedFileURLExtensions = map[string]bool{
- "txt": true,
- "md": true,
- "pdf": true,
- "docx": true,
- "doc": true,
- "mp3": true,
- "wav": true,
- "m4a": true,
- "flac": true,
- "ogg": true,
-}
-
// maxFileURLSize is the maximum allowed file size for file URL import (10MB)
const maxFileURLSize = 10 * 1024 * 1024
@@ -564,9 +550,9 @@ func (s *knowledgeService) createKnowledgeFromFileURL(
// Validate file extension against whitelist (if we can determine it)
if fileType != "" {
- if !allowedFileURLExtensions[strings.ToLower(fileType)] {
+ if !isAllowedFileURLExtension(fileType) {
logger.Errorf(ctx, "Unsupported file type for file URL import: %s", fileType)
- return nil, werrors.NewBadRequestError(fmt.Sprintf("不支持的文件类型: %s,仅支持 txt, md, pdf, docx, doc", fileType))
+ return nil, werrors.NewBadRequestError(fmt.Sprintf("不支持的文件类型: %s", fileType))
}
}
diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go
index 8aaf47ffd..6e67c6139 100644
--- a/internal/application/service/knowledge_process.go
+++ b/internal/application/service/knowledge_process.go
@@ -3185,7 +3185,7 @@ func (s *knowledgeService) ProcessDocument(ctx context.Context, t *asynq.Task) e
return fmt.Errorf("failed to download file from URL: %w", err)
}
- if resolvedFileType != "" && !allowedFileURLExtensions[strings.ToLower(resolvedFileType)] {
+ if resolvedFileType != "" && !isAllowedFileURLExtension(resolvedFileType) {
logger.Errorf(ctx, "Unsupported file type resolved from file URL: %s", resolvedFileType)
knowledge.ParseStatus = "failed"
knowledge.ErrorMessage = fmt.Sprintf("unsupported file type: %s", resolvedFileType)
diff --git a/internal/application/service/knowledge_util.go b/internal/application/service/knowledge_util.go
index 080ae4540..bf84526e7 100644
--- a/internal/application/service/knowledge_util.go
+++ b/internal/application/service/knowledge_util.go
@@ -30,6 +30,15 @@ func isValidFileType(filename string) bool {
}
}
+// isAllowedFileURLExtension checks whether a file extension is supported for URL-based file import.
+func isAllowedFileURLExtension(ext string) bool {
+ ext = strings.ToLower(strings.TrimPrefix(ext, "."))
+ if ext == "" {
+ return false
+ }
+ return isValidFileType("file." + ext)
+}
+
// getFileType extracts the file extension from a filename
func getFileType(filename string) string {
ext := strings.Split(filename, ".")
diff --git a/internal/application/service/knowledge_util_filetype_test.go b/internal/application/service/knowledge_util_filetype_test.go
index e86a2da36..7771f53ae 100644
--- a/internal/application/service/knowledge_util_filetype_test.go
+++ b/internal/application/service/knowledge_util_filetype_test.go
@@ -22,3 +22,28 @@ func TestIsValidFileTypeHTML(t *testing.T) {
})
}
}
+
+func TestIsAllowedFileURLExtension(t *testing.T) {
+ tests := []struct {
+ name string
+ ext string
+ want bool
+ }{
+ {name: "xlsx", ext: "xlsx", want: true},
+ {name: "xls", ext: "xls", want: true},
+ {name: "csv", ext: "csv", want: true},
+ {name: "dot prefix", ext: ".xlsx", want: true},
+ {name: "uppercase", ext: "XLSX", want: true},
+ {name: "pdf", ext: "pdf", want: true},
+ {name: "unsupported", ext: "exe", want: false},
+ {name: "empty", ext: "", want: false},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := isAllowedFileURLExtension(tt.ext); got != tt.want {
+ t.Fatalf("isAllowedFileURLExtension(%q) = %v, want %v", tt.ext, got, tt.want)
+ }
+ })
+ }
+}