Skip to content

Commit

Permalink
Revert "chore: Synchronizing updates from the main branch"
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh1n3zZ authored Jun 22, 2024
1 parent 9d4ff72 commit 7f817f1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 23 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ sdk
logs

chat
*.exe
chat.exe

# for reverse engine
Expand Down
7 changes: 3 additions & 4 deletions manager/images.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package manager

import (
adaptercommon "chat/adapter/common"
"chat/adapter/common"
"chat/admin"
"chat/auth"
"chat/channel"
"chat/globals"
"chat/utils"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"time"

"github.com/gin-gonic/gin"
)

func ImagesRelayAPI(c *gin.Context) {
Expand Down Expand Up @@ -75,7 +74,7 @@ func getImageProps(form RelayImageForm, messages []globals.Message, buffer *util
func getUrlFromBuffer(buffer *utils.Buffer) string {
content := buffer.Read()

urls := utils.ExtractImagesFromMarkdown(content)
_, urls := utils.ExtractImages(content, true)
if len(urls) > 0 {
return urls[len(urls)-1]
}
Expand Down
17 changes: 2 additions & 15 deletions utils/char.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package utils

import (
"fmt"
"github.com/goccy/go-json"
"regexp"
"strconv"
"strings"
"time"

"github.com/goccy/go-json"
)

func GetRandomInt(min int, max int) int {
Expand Down Expand Up @@ -221,18 +220,6 @@ func ExtractImages(data string, includeBase64 bool) (content string, images []st
return content, images
}

func ExtractImagesFromMarkdown(data string) (images []string) {
// extract images like `![image](https://xxx.com/xxx?xxx=xxx&xxx=xxx)` and return urls
re := regexp.MustCompile(`!\[.*\]\((https?://\S+)\)`)
matches := re.FindAllStringSubmatch(data, -1)

for _, match := range matches {
images = append(images, match[1])
}

return images
}

func ExtractBase64Images(data string) []string {
// get base64 images from data (data:image/png;base64,xxxxxx) (\n \\n [space] \\t \\r \\v \\f break the base64 string)
re := regexp.MustCompile(`(data:image/\w+;base64,[\w+/=]+)`)
Expand All @@ -242,7 +229,7 @@ func ExtractBase64Images(data string) []string {
func ExtractExternalImages(data string) []string {
// https://platform.openai.com/docs/guides/vision/what-type-of-files-can-i-upload

re := regexp.MustCompile(`(https?://\S+\.(?:png|jpg|jpeg|gif|webp|heif|heic|bmp|svg|ico)(?:\?\S+)?)`)
re := regexp.MustCompile(`(https?://\S+\.(?:png|jpg|jpeg|gif|webp|heif|heic)(?:\s\S+)?)`)
return re.FindAllString(data, -1)
}

Expand Down
5 changes: 2 additions & 3 deletions utils/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"chat/globals"
"fmt"
"github.com/chai2010/webp"
"image"
"image/gif"
"image/jpeg"
Expand All @@ -12,8 +13,6 @@ import (
"os"
"path"
"strings"

"github.com/chai2010/webp"
)

type Image struct {
Expand Down Expand Up @@ -227,7 +226,7 @@ func DownloadImage(url string, path string) error {

func StoreImage(url string) string {
if globals.AcceptImageStore {
hash := Md5Encrypt(url) + path.Ext(url)
hash := Md5Encrypt(url)

if err := DownloadImage(url, fmt.Sprintf("storage/attachments/%s", hash)); err != nil {
globals.Warn(fmt.Sprintf("[utils] save image error: %s", err.Error()))
Expand Down

0 comments on commit 7f817f1

Please sign in to comment.