Skip to content

Commit a2c90f5

Browse files
authored
refactor: code structure (#554)
* refactor: code structure * wip
1 parent bd0e6cd commit a2c90f5

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
517517
}
518518

519519
// 获取文件的类型
520-
mediaType := getMessageType(filename)
520+
mediaType := messageType(filename)
521521

522522
path, err := url.Parse(c.Domain.FileHost() + webwxuploadmedia)
523523
if err != nil {

global.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,27 @@ const (
110110
)
111111

112112
const (
113-
// 分块上传时每次上传的文件的大小
114-
chunkSize int64 = (1 << 20) / 2 // 0.5m
115-
// 需要检测的文件大小
116-
needCheckSize int64 = 25 << 20 // nolint:unused
117-
// 最大文件上传大小
118-
maxFileUploadSize int64 = 50 << 20 // nolint:unused
119-
// 最大图片上传大小
120-
maxImageUploadSize int64 = 20 << 20 // nolint:unused
113+
// 文件大小单位
114+
_ = 1 << (10 * iota) // 1 << 0 = 1B
115+
KB // 1 << 10 = 1KB
116+
MB // 1 << 20 = 1MB
121117
)
122118

123-
const TimeFormat = "Mon Jan 02 2006 15:04:05 GMT+0800 (中国标准时间)"
119+
const (
120+
// ChunkSize 分块上传时每次上传的文件大小 (512KB)
121+
chunkSize = 512 * KB
122+
123+
// needCheckSize 需要检测的文件大小 (25MB)
124+
needCheckSize = 25 * MB // nolint:unused
124125

125-
var imageType = map[string]struct{}{
126-
"bmp": {},
127-
"png": {},
128-
"jpeg": {},
129-
"jpg": {},
130-
"gif": {},
131-
}
126+
// maxFileUploadSize 最大文件上传大小 (50MB)
127+
maxFileUploadSize = 50 * MB // nolint:unused
132128

133-
const videoType = "mp4"
129+
// maxImageUploadSize 最大图片上传大小 (20MB)
130+
maxImageUploadSize = 20 * MB // nolint:unused
131+
)
132+
133+
const TimeFormat = "Mon Jan 02 2006 15:04:05 GMT+0800 (中国标准时间)"
134134

135135
// FileHelper 文件传输助手
136136
const FileHelper = "filehelper"

message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ func newFileAppMessage(stat os.FileInfo, attachId string) *appmsg {
714714
m.AppAttach.AttachId = attachId
715715
m.AppAttach.TotalLen = stat.Size()
716716
m.Type = 6
717-
m.AppAttach.FileExt = getFileExt(stat.Name())
717+
m.AppAttach.FileExt = fileExtension(stat.Name())
718718
return m
719719
}
720720

parser.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,33 @@ func GetFileContentType(file io.Reader) (string, error) {
4545
return http.DetectContentType(data), nil
4646
}
4747

48-
func getFileExt(name string) string {
49-
ext := filepath.Ext(name)
48+
// fileExtension
49+
func fileExtension(name string) string {
50+
ext := strings.ToLower(filepath.Ext(name))
5051
if len(ext) == 0 {
51-
ext = "undefined"
52+
return "undefined"
5253
}
5354
return strings.TrimPrefix(ext, ".")
5455
}
5556

56-
const (
57-
pic = "pic"
58-
video = "video"
59-
doc = "doc"
60-
)
57+
// 判断是否是图片
58+
func isImageType(imageType string) bool {
59+
switch imageType {
60+
case "bmp", "png", "jpeg", "jpg", "gif":
61+
return true
62+
default:
63+
return false
64+
}
65+
}
6166

6267
// 微信匹配文件类型策略
63-
func getMessageType(filename string) string {
64-
ext := getFileExt(filename)
65-
if _, ok := imageType[ext]; ok {
66-
return pic
68+
func messageType(filename string) string {
69+
ext := fileExtension(filename)
70+
if isImageType(ext) {
71+
return "pic"
6772
}
68-
if ext == videoType {
69-
return video
73+
if ext == "mp4" {
74+
return "video"
7075
}
71-
return doc
76+
return "doc"
7277
}

0 commit comments

Comments
 (0)