This commit is contained in:
Daniel 2024-11-25 21:51:40 +08:00
parent 4335a464f6
commit 497b7829f1
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -249,7 +249,9 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
ret = []*cache.Asset{}
keywords := strings.Split(keyword, " ")
pathHitCount := map[string]int{}
count := 0
filterByExt := 0 < len(exts)
for _, asset := range cache.GetAssets() {
@ -269,16 +271,24 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
lowerHName := strings.ToLower(asset.HName)
lowerPath := strings.ToLower(asset.Path)
lowerKeyword := strings.ToLower(keyword)
hitName := strings.Contains(lowerHName, lowerKeyword)
hitPath := strings.Contains(lowerPath, lowerKeyword)
if !hitName && !hitPath {
continue
var hitNameCount, hitPathCount int
for _, k := range keywords {
lowerKeyword := strings.ToLower(k)
hitNameCount += strings.Count(lowerHName, lowerKeyword)
hitPathCount += strings.Count(lowerPath, lowerKeyword)
if 1 > hitNameCount && 1 > hitPathCount {
continue
}
}
if 1 > hitNameCount+hitPathCount {
continue
}
pathHitCount[asset.Path] += hitNameCount + hitPathCount
hName := asset.HName
if hitName {
_, hName = search.MarkText(asset.HName, keyword, 64, Conf.Search.CaseSensitive)
if 0 < hitNameCount {
_, hName = search.MarkText(asset.HName, strings.Join(keywords, search.TermSep), 64, Conf.Search.CaseSensitive)
}
ret = append(ret, &cache.Asset{
HName: hName,
@ -291,9 +301,15 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
}
}
sort.Slice(ret, func(i, j int) bool {
return ret[i].Updated > ret[j].Updated
})
if 0 < len(pathHitCount) {
sort.Slice(ret, func(i, j int) bool {
return pathHitCount[ret[i].Path] > pathHitCount[ret[j].Path]
})
} else {
sort.Slice(ret, func(i, j int) bool {
return ret[i].Updated > ret[j].Updated
})
}
return
}