mirror of
https://github.com/coaidev/coai.git
synced 2025-06-04 04:40:22 +09:00
Merge pull request #212 from Deeptrain-Community/main
chore: Synchronizing updates from the `main` branch
This commit is contained in:
commit
9d4ff7285a
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,6 +14,7 @@ sdk
|
|||||||
logs
|
logs
|
||||||
|
|
||||||
chat
|
chat
|
||||||
|
*.exe
|
||||||
chat.exe
|
chat.exe
|
||||||
|
|
||||||
# for reverse engine
|
# for reverse engine
|
||||||
|
@ -1,17 +1,18 @@
|
|||||||
package manager
|
package manager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"chat/adapter/common"
|
adaptercommon "chat/adapter/common"
|
||||||
"chat/admin"
|
"chat/admin"
|
||||||
"chat/auth"
|
"chat/auth"
|
||||||
"chat/channel"
|
"chat/channel"
|
||||||
"chat/globals"
|
"chat/globals"
|
||||||
"chat/utils"
|
"chat/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ImagesRelayAPI(c *gin.Context) {
|
func ImagesRelayAPI(c *gin.Context) {
|
||||||
@ -74,7 +75,7 @@ func getImageProps(form RelayImageForm, messages []globals.Message, buffer *util
|
|||||||
func getUrlFromBuffer(buffer *utils.Buffer) string {
|
func getUrlFromBuffer(buffer *utils.Buffer) string {
|
||||||
content := buffer.Read()
|
content := buffer.Read()
|
||||||
|
|
||||||
_, urls := utils.ExtractImages(content, true)
|
urls := utils.ExtractImagesFromMarkdown(content)
|
||||||
if len(urls) > 0 {
|
if len(urls) > 0 {
|
||||||
return urls[len(urls)-1]
|
return urls[len(urls)-1]
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,12 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/goccy/go-json"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRandomInt(min int, max int) int {
|
func GetRandomInt(min int, max int) int {
|
||||||
@ -220,6 +221,18 @@ func ExtractImages(data string, includeBase64 bool) (content string, images []st
|
|||||||
return content, images
|
return content, images
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExtractImagesFromMarkdown(data string) (images []string) {
|
||||||
|
// extract images like `` 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 {
|
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)
|
// 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+/=]+)`)
|
re := regexp.MustCompile(`(data:image/\w+;base64,[\w+/=]+)`)
|
||||||
@ -229,7 +242,7 @@ func ExtractBase64Images(data string) []string {
|
|||||||
func ExtractExternalImages(data string) []string {
|
func ExtractExternalImages(data string) []string {
|
||||||
// https://platform.openai.com/docs/guides/vision/what-type-of-files-can-i-upload
|
// 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)(?:\s\S+)?)`)
|
re := regexp.MustCompile(`(https?://\S+\.(?:png|jpg|jpeg|gif|webp|heif|heic|bmp|svg|ico)(?:\?\S+)?)`)
|
||||||
return re.FindAllString(data, -1)
|
return re.FindAllString(data, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"chat/globals"
|
"chat/globals"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/chai2010/webp"
|
|
||||||
"image"
|
"image"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
@ -13,6 +12,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/chai2010/webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Image struct {
|
type Image struct {
|
||||||
@ -226,7 +227,7 @@ func DownloadImage(url string, path string) error {
|
|||||||
|
|
||||||
func StoreImage(url string) string {
|
func StoreImage(url string) string {
|
||||||
if globals.AcceptImageStore {
|
if globals.AcceptImageStore {
|
||||||
hash := Md5Encrypt(url)
|
hash := Md5Encrypt(url) + path.Ext(url)
|
||||||
|
|
||||||
if err := DownloadImage(url, fmt.Sprintf("storage/attachments/%s", hash)); err != nil {
|
if err := DownloadImage(url, fmt.Sprintf("storage/attachments/%s", hash)); err != nil {
|
||||||
globals.Warn(fmt.Sprintf("[utils] save image error: %s", err.Error()))
|
globals.Warn(fmt.Sprintf("[utils] save image error: %s", err.Error()))
|
||||||
|
Loading…
Reference in New Issue
Block a user