Skip to content

Commit

Permalink
🐞 fix: 尝试修复CPU占用问题
Browse files Browse the repository at this point in the history
  • Loading branch information
TIANLI0 committed Oct 26, 2023
1 parent c69ef43 commit 5a86fa7
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions nsfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ type Setu struct {
}

func detectNSFW(imageData []byte, pathAll string) (NSFWResult, error) {

imageType := http.DetectContentType(imageData)
if imageType == "image/webp" {

pngData, err := convertWebpToPng(imageData)
if err != nil {
return NSFWResult{}, fmt.Errorf("转换图片格式失败:%v", err)
Expand All @@ -34,14 +32,21 @@ func detectNSFW(imageData []byte, pathAll string) (NSFWResult, error) {
}

for len(imageData) > 1024*1024 {

compressedData, err := compressImage(imageData)
if err != nil {
return NSFWResult{}, fmt.Errorf("压缩图片失败:%v", err)
}

os.WriteFile("compressed.jpg", compressedData, 0644)
if err != nil {
return NSFWResult{}, fmt.Errorf("压缩图片失败:%v", err)
return NSFWResult{}, fmt.Errorf("写入压缩图片失败:%v", err)
}

imageData = compressedData

if len(imageData) <= 1024*1024 {
break
}
}

body := new(bytes.Buffer)
Expand Down Expand Up @@ -86,7 +91,6 @@ func detectNSFW(imageData []byte, pathAll string) (NSFWResult, error) {
}

if nsfwResponse.Porn > Porn {

setu := Setu{
Path: pathAll,
NSFW: nsfwResponse,
Expand Down

0 comments on commit 5a86fa7

Please sign in to comment.