Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2208d3

Browse files
committed
图片转换程序识别后缀
1 parent 0dd5b50 commit f2208d3

File tree

1 file changed

+13
-2
lines changed
  • websites/code/studygolang/src/controller

1 file changed

+13
-2
lines changed

websites/code/studygolang/src/controller/ajax.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ import (
1313
"io"
1414
"io/ioutil"
1515
"math/rand"
16+
"mime"
1617
"net/http"
1718
"os"
1819
"path/filepath"
1920
"strconv"
2021

21-
"github.com/studygolang/mux"
2222
"logger"
2323
"model"
2424
"service"
2525
"util"
26+
27+
"github.com/studygolang/mux"
2628
)
2729

2830
// 侧边栏的内容通过异步请求获取
@@ -292,7 +294,16 @@ func UploadImageHandler(rw http.ResponseWriter, req *http.Request) {
292294

293295
buf, err := ioutil.ReadAll(resp.Body)
294296

295-
uri = util.DateNow() + "/" + util.Md5Buf(buf) + filepath.Ext(origUrl)
297+
ext := filepath.Ext(origUrl)
298+
if ext == "" {
299+
contentType := http.DetectContentType(buf)
300+
exts, _ := mime.ExtensionsByType(contentType)
301+
if len(exts) > 0 {
302+
ext = exts[0]
303+
}
304+
}
305+
306+
uri = util.DateNow() + "/" + util.Md5Buf(buf) + ext
296307

297308
reader = bytes.NewReader(buf)
298309
} else {

0 commit comments

Comments
 (0)