Merge remote-tracking branch 'origin/feat/1.2.0/question' into test

This commit is contained in:
LinkinStars 2023-10-08 15:13:48 +08:00
commit d5cdb96073
9 changed files with 37 additions and 11 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -117,6 +117,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 {

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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)

View File

@ -87,18 +87,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 := plugin.DefaultFileTypeCheckMapping[plugin.UserAvatar][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) {
@ -165,18 +166,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 := plugin.DefaultFileTypeCheckMapping[plugin.UserPost][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) (
@ -191,18 +193,19 @@ 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))
if _, ok := plugin.DefaultFileTypeCheckMapping[plugin.AdminBranding][fileExt]; !ok {
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
}
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) (