Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code structure #554

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
Loading