File tree 1 file changed +13
-2
lines changed
websites/code/studygolang/src/controller
1 file changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -13,16 +13,18 @@ import (
13
13
"io"
14
14
"io/ioutil"
15
15
"math/rand"
16
+ "mime"
16
17
"net/http"
17
18
"os"
18
19
"path/filepath"
19
20
"strconv"
20
21
21
- "github.com/studygolang/mux"
22
22
"logger"
23
23
"model"
24
24
"service"
25
25
"util"
26
+
27
+ "github.com/studygolang/mux"
26
28
)
27
29
28
30
// 侧边栏的内容通过异步请求获取
@@ -292,7 +294,16 @@ func UploadImageHandler(rw http.ResponseWriter, req *http.Request) {
292
294
293
295
buf , err := ioutil .ReadAll (resp .Body )
294
296
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
296
307
297
308
reader = bytes .NewReader (buf )
298
309
} else {
You can’t perform that action at this time.
0 commit comments