Skip to content

Commit

Permalink
refactor: code structure (#554)
Browse files Browse the repository at this point in the history
* refactor: code structure

* wip
  • Loading branch information
eatmoreapple authored Dec 20, 2024
1 parent bd0e6cd commit a2c90f5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
}

// 获取文件的类型
mediaType := getMessageType(filename)
mediaType := messageType(filename)

path, err := url.Parse(c.Domain.FileHost() + webwxuploadmedia)
if err != nil {
Expand Down
34 changes: 17 additions & 17 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,27 @@ const (
)

const (
// 分块上传时每次上传的文件的大小
chunkSize int64 = (1 << 20) / 2 // 0.5m
// 需要检测的文件大小
needCheckSize int64 = 25 << 20 // nolint:unused
// 最大文件上传大小
maxFileUploadSize int64 = 50 << 20 // nolint:unused
// 最大图片上传大小
maxImageUploadSize int64 = 20 << 20 // nolint:unused
// 文件大小单位
_ = 1 << (10 * iota) // 1 << 0 = 1B
KB // 1 << 10 = 1KB
MB // 1 << 20 = 1MB
)

const TimeFormat = "Mon Jan 02 2006 15:04:05 GMT+0800 (中国标准时间)"
const (
// ChunkSize 分块上传时每次上传的文件大小 (512KB)
chunkSize = 512 * KB

// needCheckSize 需要检测的文件大小 (25MB)
needCheckSize = 25 * MB // nolint:unused

var imageType = map[string]struct{}{
"bmp": {},
"png": {},
"jpeg": {},
"jpg": {},
"gif": {},
}
// maxFileUploadSize 最大文件上传大小 (50MB)
maxFileUploadSize = 50 * MB // nolint:unused

const videoType = "mp4"
// maxImageUploadSize 最大图片上传大小 (20MB)
maxImageUploadSize = 20 * MB // nolint:unused
)

const TimeFormat = "Mon Jan 02 2006 15:04:05 GMT+0800 (中国标准时间)"

// FileHelper 文件传输助手
const FileHelper = "filehelper"
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ func newFileAppMessage(stat os.FileInfo, attachId string) *appmsg {
m.AppAttach.AttachId = attachId
m.AppAttach.TotalLen = stat.Size()
m.Type = 6
m.AppAttach.FileExt = getFileExt(stat.Name())
m.AppAttach.FileExt = fileExtension(stat.Name())
return m
}

Expand Down
35 changes: 20 additions & 15 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,33 @@ func GetFileContentType(file io.Reader) (string, error) {
return http.DetectContentType(data), nil
}

func getFileExt(name string) string {
ext := filepath.Ext(name)
// fileExtension
func fileExtension(name string) string {
ext := strings.ToLower(filepath.Ext(name))
if len(ext) == 0 {
ext = "undefined"
return "undefined"
}
return strings.TrimPrefix(ext, ".")
}

const (
pic = "pic"
video = "video"
doc = "doc"
)
// 判断是否是图片
func isImageType(imageType string) bool {
switch imageType {
case "bmp", "png", "jpeg", "jpg", "gif":
return true
default:
return false
}
}

// 微信匹配文件类型策略
func getMessageType(filename string) string {
ext := getFileExt(filename)
if _, ok := imageType[ext]; ok {
return pic
func messageType(filename string) string {
ext := fileExtension(filename)
if isImageType(ext) {
return "pic"
}
if ext == videoType {
return video
if ext == "mp4" {
return "video"
}
return doc
return "doc"
}

0 comments on commit a2c90f5

Please sign in to comment.