Skip to content

Commit 75299f5

Browse files
committed
Improve upload file type detection
1 parent 511befa commit 75299f5

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed

controller/user_file_upload.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package controller
22

33
import (
44
"bytes"
5-
"github.com/ego008/goutils/json"
6-
"github.com/ego008/sdb"
7-
"github.com/valyala/fasthttp"
85
"goyoubbs/model"
96
"goyoubbs/util"
107
"image"
118
"image/jpeg"
129
"io"
1310
"os"
14-
"path"
1511
"strconv"
12+
13+
"github.com/ego008/goutils/json"
14+
"github.com/ego008/sdb"
15+
"github.com/valyala/fasthttp"
1616
)
1717

1818
const (
@@ -89,9 +89,19 @@ func (h *BaseHandler) FileUpload(ctx *fasthttp.RequestCtx) {
8989
showPath = "/static/upload/" + saveName
9090
}
9191
} else {
92-
fileSuffix = path.Ext(file.Filename) // source file suffix
93-
saveName = imgKeyS + fileSuffix
94-
showPath = "/static/upload/" + saveName
92+
// is mp3 or mp4
93+
mediaType := util.CheckMediaType(buff)
94+
if len(mediaType) > 0 {
95+
fileSuffix = "." + mediaType
96+
saveName = imgKeyS + fileSuffix
97+
showPath = "/static/upload/" + saveName
98+
} else {
99+
_, _ = ctx.WriteString(`{"Code":400,"Msg":"unknown image or media format"}`)
100+
return
101+
}
102+
//fileSuffix = path.Ext(file.Filename) // source file suffix
103+
//saveName = imgKeyS + fileSuffix
104+
//showPath = "/static/upload/" + saveName
95105
}
96106

97107
saveFullPath := h.App.Cf.Site.UploadDir + "/" + saveName

upload/11600925034256400078.jpg

461 KB
Loading

util/image_tool.go

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@ package util
33
import (
44
"bytes"
55
"errors"
6-
"github.com/disintegration/imaging"
76
"image"
87
"image/gif"
98
"image/jpeg"
109
"image/png"
1110
"net/http"
11+
12+
"github.com/disintegration/imaging"
1213
)
1314

14-
var imgTable = map[string]string{
15-
"image/jpeg": "jpg",
16-
"image/jpg": "jpg",
17-
"image/gif": "gif",
18-
"image/png": "png",
19-
}
15+
var (
16+
imgTable = map[string]string{
17+
"image/jpeg": "jpg",
18+
"image/jpg": "jpg",
19+
"image/gif": "gif",
20+
"image/png": "png",
21+
}
22+
23+
mediaTable = map[string]string{
24+
"audio/mpeg": "mp3",
25+
"video/mp4": "mp4",
26+
}
27+
)
2028

2129
func CheckImageType(buff []byte) string {
2230
// why 512 bytes ? see http://golang.org/pkg/net/http/#DetectContentType
@@ -28,6 +36,64 @@ func CheckImageType(buff []byte) string {
2836
return ""
2937
}
3038

39+
func CheckMediaType(buff []byte) string {
40+
// why 512 bytes ? see http://golang.org/pkg/net/http/#DetectContentType
41+
//buff := make([]byte, 512)
42+
fileType := http.DetectContentType(buff)
43+
if v, ok := mediaTable[fileType]; ok {
44+
return v
45+
}
46+
if isMP3(buff) {
47+
return "mp3"
48+
}
49+
if isMP4(buff) {
50+
return "mp4"
51+
}
52+
return ""
53+
}
54+
55+
// 检测MP4文件的魔数
56+
func isMP4(buffer []byte) bool {
57+
if len(buffer) < 8 {
58+
return false
59+
}
60+
61+
// MP4文件通常以ftyp开头
62+
if string(buffer[4:8]) == "ftyp" {
63+
return true
64+
}
65+
66+
return false
67+
}
68+
69+
// 检测MP3文件的魔数
70+
func isMP3(buffer []byte) bool {
71+
if len(buffer) < 3 {
72+
return false
73+
}
74+
75+
// MP3文件帧头检测
76+
// 检查ID3v2标签(以"ID3"开头)
77+
if len(buffer) >= 3 && string(buffer[0:3]) == "ID3" {
78+
return true
79+
}
80+
81+
// 检查MPEG帧同步位
82+
if len(buffer) >= 2 {
83+
// 检查FF FB (MPEG版本1, 层3)
84+
if buffer[0] == 0xFF && (buffer[1]&0xE0) == 0xE0 {
85+
// 检查具体的MPEG音频帧
86+
if (buffer[1]&0x18)>>3 == 0x03 { // MPEG版本1
87+
if (buffer[1]&0x06)>>1 == 0x01 { // 层3
88+
return true
89+
}
90+
}
91+
}
92+
}
93+
94+
return false
95+
}
96+
3197
func GetImageObj(buff *bytes.Buffer) (image.Image, error) {
3298
var img image.Image
3399
var err error

0 commit comments

Comments
 (0)