/
demo.go
58 lines (51 loc) · 1.37 KB
/
demo.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
50
51
52
53
54
55
56
57
58
/**
* Copyright 2015 @ z3q.net.
* name : demo.go
* author : jarryliu
* date : 2015-08-18 23:19
* description :
* history :
*/
package go_kindeditor
import (
"encoding/json"
"errors"
"github.com/jsix/gof/web"
"github.com/jsix/gof/web/mvc"
"strings"
)
var _ mvc.Filter = new(editorController)
type editorController struct {
}
func (this *editorController) Requesting(*web.Context) bool {
//todo: check permission
return true
}
func (this *editorController) RequestEnd(*web.Context) {
}
func (this *editorController) File_manager(ctx *web.Context) {
d, err := fileManager(ctx.Request, "./uploads/", "http://img.abc.com/uploads/")
ctx.Response.Header().Add("Content-Type", "application/json")
if err != nil {
ctx.Response.Write([]byte("{error:'" + strings.Replace(err.Error(), "'", "\\'", -1) + "'}"))
} else {
ctx.Response.Write(d)
}
}
func (this *editorController) File_upload(ctx *web.Context) {
if ctx.Request().Method != "POST" {
return errors.New("error request method")
}
fileUrl, err := fileUpload(ctx.Request, "./uploads/", "http://img.abc.com/uploads/")
var hash map[string]interface{} = make(map[string]interface{})
if err == nil {
hash["error"] = 0
hash["url"] = fileUrl
} else {
hash["error"] = 1
hash["message"] = err.Error()
}
ctx.Response.Header().Add("Content-Type", "application/json")
d, _ := json.Marshal(hash)
ctx.Response.Write(d)
}