From c34923031da16df9c8a22db57c3b03db63f4f510 Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Wed, 20 Sep 2023 11:53:23 +0800 Subject: [PATCH 1/3] feat(plugin): update plugin url --- script/plugin_list | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/plugin_list b/script/plugin_list index b3ea84c2..a7cfc670 100644 --- a/script/plugin_list +++ b/script/plugin_list @@ -1 +1 @@ -github.com/answerdev/answer-basic-connector@latest \ No newline at end of file +github.com/answerdev/plugins/connector/basic@latest \ No newline at end of file From 5ce51874228842bf21af13201e89765c782318c4 Mon Sep 17 00:00:00 2001 From: Lev Zakharov Date: Fri, 6 Oct 2023 19:14:53 +0300 Subject: [PATCH 2/3] refactor: close used resources --- internal/base/conf/conf.go | 1 + internal/cli/dump.go | 1 + internal/cli/install_check.go | 2 ++ internal/migrations/migrations.go | 1 + internal/service/uploader/upload.go | 21 ++++++++++++--------- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/internal/base/conf/conf.go b/internal/base/conf/conf.go index b0401e1e..b441c471 100644 --- a/internal/base/conf/conf.go +++ b/internal/base/conf/conf.go @@ -60,6 +60,7 @@ func ReadConfig(configFilePath string) (c *AllConfig, err error) { func RewriteConfig(configFilePath string, allConfig *AllConfig) error { buf := bytes.Buffer{} enc := yaml.NewEncoder(&buf) + defer enc.Close() enc.SetIndent(2) if err := enc.Encode(allConfig); err != nil { return err diff --git a/internal/cli/dump.go b/internal/cli/dump.go index 7b84d862..3696a432 100644 --- a/internal/cli/dump.go +++ b/internal/cli/dump.go @@ -15,6 +15,7 @@ func DumpAllData(dataConf *data.Database, dumpDataPath string) error { if err != nil { return err } + defer db.Close() if err = db.Ping(); err != nil { return err } diff --git a/internal/cli/install_check.go b/internal/cli/install_check.go index 6cb25987..a8926060 100644 --- a/internal/cli/install_check.go +++ b/internal/cli/install_check.go @@ -23,6 +23,7 @@ func CheckDBConnection(dataConf *data.Database) bool { fmt.Printf("connection database failed: %s\n", err) return false } + defer db.Close() if err = db.Ping(); err != nil { fmt.Printf("connection ping database failed: %s\n", err) return false @@ -38,6 +39,7 @@ func CheckDBTableExist(dataConf *data.Database) bool { fmt.Printf("connection database failed: %s\n", err) return false } + defer db.Close() if err = db.Ping(); err != nil { fmt.Printf("connection ping database failed: %s\n", err) return false diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index a4ebeba8..de28c0c5 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -116,6 +116,7 @@ func Migrate(debug bool, dbConf *data.Database, cacheConf *data.CacheConf, upgra fmt.Println("new database failed: ", err.Error()) return err } + defer engine.Close() currentDBVersion, err := GetCurrentDBVersion(engine) if err != nil { diff --git a/internal/service/uploader/upload.go b/internal/service/uploader/upload.go index 19bf6322..01abec43 100644 --- a/internal/service/uploader/upload.go +++ b/internal/service/uploader/upload.go @@ -91,18 +91,19 @@ func (us *uploaderService) UploadAvatarFile(ctx *gin.Context) (url string, err e // max size ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 5*1024*1024) - _, file, err := ctx.Request.FormFile("file") + file, fileHeader, err := ctx.Request.FormFile("file") if err != nil { return "", errors.BadRequest(reason.RequestFormatError).WithError(err) } - fileExt := strings.ToLower(path.Ext(file.Filename)) + file.Close() + fileExt := strings.ToLower(path.Ext(fileHeader.Filename)) if _, ok := FormatExts[fileExt]; !ok { return "", errors.BadRequest(reason.RequestFormatError).WithError(err) } newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt) avatarFilePath := path.Join(avatarSubPath, newFilename) - return us.uploadFile(ctx, file, avatarFilePath) + return us.uploadFile(ctx, fileHeader, avatarFilePath) } func (us *uploaderService) AvatarThumbFile(ctx *gin.Context, fileName string, size int) (url string, err error) { @@ -170,18 +171,19 @@ func (us *uploaderService) UploadPostFile(ctx *gin.Context) ( // max size ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 10*1024*1024) - _, file, err := ctx.Request.FormFile("file") + file, fileHeader, err := ctx.Request.FormFile("file") if err != nil { return "", errors.BadRequest(reason.RequestFormatError).WithError(err) } - fileExt := strings.ToLower(path.Ext(file.Filename)) + defer file.Close() + fileExt := strings.ToLower(path.Ext(fileHeader.Filename)) if _, ok := FormatExts[fileExt]; !ok { return "", errors.BadRequest(reason.RequestFormatError).WithError(err) } newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt) avatarFilePath := path.Join(postSubPath, newFilename) - return us.uploadFile(ctx, file, avatarFilePath) + return us.uploadFile(ctx, fileHeader, avatarFilePath) } func (us *uploaderService) UploadBrandingFile(ctx *gin.Context) ( @@ -196,11 +198,12 @@ func (us *uploaderService) UploadBrandingFile(ctx *gin.Context) ( // max size ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 10*1024*1024) - _, file, err := ctx.Request.FormFile("file") + file, fileHeader, err := ctx.Request.FormFile("file") if err != nil { return "", errors.BadRequest(reason.RequestFormatError).WithError(err) } - fileExt := strings.ToLower(path.Ext(file.Filename)) + file.Close() + fileExt := strings.ToLower(path.Ext(fileHeader.Filename)) _, ok := FormatExts[fileExt] if !ok && fileExt != ".ico" { return "", errors.BadRequest(reason.RequestFormatError).WithError(err) @@ -208,7 +211,7 @@ func (us *uploaderService) UploadBrandingFile(ctx *gin.Context) ( newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt) avatarFilePath := path.Join(brandingSubPath, newFilename) - return us.uploadFile(ctx, file, avatarFilePath) + return us.uploadFile(ctx, fileHeader, avatarFilePath) } func (us *uploaderService) uploadFile(ctx *gin.Context, file *multipart.FileHeader, fileSubPath string) ( From 5101c12ec13b240b118ba6f8f1d7068e125e31c9 Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Sun, 8 Oct 2023 15:11:02 +0800 Subject: [PATCH 3/3] fix(search): filter all special characters when searching --- internal/repo/answer/answer_repo.go | 2 +- internal/repo/question/question_repo.go | 2 +- internal/schema/search_schema.go | 12 ++++++++++++ internal/service/search_service.go | 6 ++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.go index 31092477..18ae72f7 100644 --- a/internal/repo/answer/answer_repo.go +++ b/internal/repo/answer/answer_repo.go @@ -422,7 +422,7 @@ func (ar *answerRepo) updateSearch(ctx context.Context, answerID string) (err er ObjectID: answerID, Title: question.Title, Type: constant.AnswerObjectType, - Content: answer.ParsedText, + Content: answer.OriginalText, Answers: 0, Status: plugin.SearchContentStatus(answer.Status), Tags: tags, diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.go index 966541e4..59b187d6 100644 --- a/internal/repo/question/question_repo.go +++ b/internal/repo/question/question_repo.go @@ -468,7 +468,7 @@ func (qr *questionRepo) updateSearch(ctx context.Context, questionID string) (er ObjectID: questionID, Title: question.Title, Type: constant.QuestionObjectType, - Content: question.ParsedText, + Content: question.OriginalText, Answers: int64(question.AnswerCount), Status: plugin.SearchContentStatus(question.Status), Tags: tags, diff --git a/internal/schema/search_schema.go b/internal/schema/search_schema.go index e30e6997..e1cb0bf2 100644 --- a/internal/schema/search_schema.go +++ b/internal/schema/search_schema.go @@ -2,7 +2,10 @@ package schema import ( "github.com/answerdev/answer/internal/base/constant" + "github.com/answerdev/answer/internal/base/validator" "github.com/answerdev/answer/plugin" + "regexp" + "strings" ) type SearchDTO struct { @@ -15,6 +18,15 @@ type SearchDTO struct { CaptchaCode string `json:"captcha_code"` } +func (s *SearchDTO) Check() (errField []*validator.FormErrorField, err error) { + // Replace special characters. + // Special characters will cause the search abnormal, such as search for "#" will get nearly all the content that Markdown format. + s.Query = regexp.MustCompile(`[+#.<>\-_()*]`).ReplaceAllString(s.Query, " ") + s.Query = regexp.MustCompile(`\s+`).ReplaceAllString(s.Query, " ") + s.Query = strings.TrimSpace(s.Query) + return nil, nil +} + type SearchCondition struct { // search target type: all/question/answer TargetType string diff --git a/internal/service/search_service.go b/internal/service/search_service.go index d4f74e92..f89bd9cd 100644 --- a/internal/service/search_service.go +++ b/internal/service/search_service.go @@ -28,6 +28,12 @@ func (ss *SearchService) Search(ctx context.Context, dto *schema.SearchDTO) (res if dto.Page < 1 { dto.Page = 1 } + if len(dto.Query) == 0 { + return &schema.SearchResp{ + Total: 0, + SearchResults: make([]*schema.SearchResult, 0), + }, nil + } // search type cond := ss.searchParser.ParseStructure(ctx, dto)