diff --git a/client.go b/client.go index f36231c..5c2fd33 100644 --- a/client.go +++ b/client.go @@ -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 { diff --git a/global.go b/global.go index c671d7f..6ff5387 100644 --- a/global.go +++ b/global.go @@ -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" diff --git a/message.go b/message.go index 0a07445..c4013d6 100644 --- a/message.go +++ b/message.go @@ -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 } diff --git a/parser.go b/parser.go index e3b7407..6ad6315 100644 --- a/parser.go +++ b/parser.go @@ -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" }