diff --git a/internal/application/service/knowledge_create.go b/internal/application/service/knowledge_create.go index e12a7d9c..4d2e099c 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 8aaf47ff..6e67c613 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 080ae454..bf84526e 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 e86a2da3..7771f53a 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) + } + }) + } +}