feat: add DingTalk bot integration
This commit is contained in:
parent
d509a18d44
commit
f4980f1f22
|
|
@ -1,7 +1,17 @@
|
||||||
package handle
|
package handle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"ai_scheduler/internal/data/constants"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
|
"ai_scheduler/internal/pkg/l_request"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HandleRecognizeFile 这里的目的是无论将什么类型的file都转为二进制格式
|
// HandleRecognizeFile 这里的目的是无论将什么类型的file都转为二进制格式
|
||||||
|
|
@ -12,3 +22,63 @@ func HandleRecognizeFile(file *entitys.RecognizeFile) {
|
||||||
//Todo 仲云
|
//Todo 仲云
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 下载文件并返回二进制数据、MIME 类型
|
||||||
|
func downloadFile(fileUrl string) (fileBytes []byte, contentType string, err error) {
|
||||||
|
if len(fileUrl) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req := l_request.Request{
|
||||||
|
Method: "GET",
|
||||||
|
Url: fileUrl,
|
||||||
|
Headers: map[string]string{
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||||
|
"Accept": "image/webp,image/apng,image/*,*/*;q=0.8",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
res, err := req.Send()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var ex bool
|
||||||
|
if contentType, ex = res.Headers["Content-Type"]; !ex {
|
||||||
|
err = errors.New("Content-Type不存在")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
err = fmt.Errorf("server returned non-200 status: %d", res.StatusCode)
|
||||||
|
}
|
||||||
|
fileBytes = res.Content
|
||||||
|
|
||||||
|
return fileBytes, contentType, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// detectFileType 判断文件类型
|
||||||
|
func detectFileType(file io.ReadSeeker, filename string) constants.FileType {
|
||||||
|
// 1. 读取文件头检测 MIME
|
||||||
|
buffer := make([]byte, 512)
|
||||||
|
n, _ := file.Read(buffer)
|
||||||
|
file.Seek(0, io.SeekStart) // 重置读取位置
|
||||||
|
|
||||||
|
detectedMIME := mimetype.Detect(buffer[:n]).String()
|
||||||
|
for fileType, items := range constants.FileTypeMappings {
|
||||||
|
for _, item := range items {
|
||||||
|
if !strings.HasPrefix(item, ".") && item == detectedMIME {
|
||||||
|
return fileType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 备用:通过扩展名检测
|
||||||
|
ext := strings.ToLower(filepath.Ext(filename))
|
||||||
|
for fileType, items := range constants.FileTypeMappings {
|
||||||
|
for _, item := range items {
|
||||||
|
if strings.HasPrefix(item, ".") && item == ext {
|
||||||
|
return fileType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return constants.FileTypeUnknown
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,5 @@
|
||||||
package constants
|
package constants
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gabriel-vasile/mimetype"
|
|
||||||
"io"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FileType string
|
type FileType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -42,34 +36,3 @@ var FileTypeMappings = map[FileType][]string{
|
||||||
".txt",
|
".txt",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func DetectFileType(file io.ReadSeeker) (FileType, error) {
|
|
||||||
// 读取文件头(512字节足够检测大多数类型)
|
|
||||||
buffer := make([]byte, 512)
|
|
||||||
n, err := file.Read(buffer)
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
return FileTypeUnknown, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重置读取位置
|
|
||||||
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
|
||||||
return FileTypeUnknown, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检测 MIME 类型
|
|
||||||
detectedMIME := mimetype.Detect(buffer[:n]).String()
|
|
||||||
|
|
||||||
// 遍历映射表,匹配 MIME 或扩展名
|
|
||||||
for fileType, mimesOrExts := range FileTypeMappings {
|
|
||||||
for _, item := range mimesOrExts {
|
|
||||||
if strings.HasPrefix(item, ".") {
|
|
||||||
continue // 跳过扩展名(仅 MIME 检测)
|
|
||||||
}
|
|
||||||
if item == detectedMIME {
|
|
||||||
return fileType, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return FileTypeUnknown, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue