forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_image_handler.go
49 lines (44 loc) · 1.37 KB
/
upload_image_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package uadmin
import (
"bufio"
"encoding/base64"
"io"
"net/http"
)
// UploadImageHandler handles files sent from Tiny MCE's photo uploader
func UploadImageHandler(w http.ResponseWriter, r *http.Request, session *Session) {
r.ParseMultipartForm(32 << 20)
for _, f := range r.MultipartForm.File["file"] {
src, _ := f.Open()
reader := bufio.NewReader(src)
content, _ := io.ReadAll(reader)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(base64.StdEncoding.EncodeToString(content)))
}
}
//// UploadImageHandler handles files sent from Tiny MCE's photo uploader
//func UploadImageHandler(w http.ResponseWriter, r *http.Request, session *Session) {
// r.ParseMultipartForm(32 << 20)
//
// for _, f := range r.MultipartForm.File["file"] {
// src, _ := f.Open()
// folderPath := "./media/htmlimages/" + GenerateBase64(24) + "/"
// for {
// if _, err := os.Stat(folderPath); os.IsNotExist(err) {
// break
// }
// folderPath = "./media/htmlimages/" + GenerateBase64(24) + "/"
// }
// os.MkdirAll(folderPath, 0744)
//
// fileName := strings.Replace(f.Filename, "/", " ", -1)
//
// dst, _ := os.Create(folderPath + fileName)
// io.Copy(dst, src)
// src.Close()
// dst.Close()
// res := `{ "location" : "` + strings.TrimPrefix(folderPath+fileName, ".") + `" }`
// w.Header().Set("Content-Type", "application/json")
// w.Write([]byte(res))
// }
//}