forked from UlricQin/beego-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_controller.go
85 lines (71 loc) · 1.57 KB
/
api_controller.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package controllers
import (
"fmt"
"github.com/vincentruan/beego-blog/g"
"github.com/vincentruan/beego-blog/models/catalog"
"github.com/ulricqin/goutils/filetool"
"github.com/ulricqin/goutils/strtool"
"strings"
"time"
)
const (
EDITOR_IMG_DIR = "static/uploads/editor"
)
type ApiController struct {
BaseController
}
func (this *ApiController) Health() {
fmt.Println(catalog.All()[0])
this.Ctx.WriteString("ok")
}
func (this *ApiController) Md5() {
p := this.GetString("p")
this.Ctx.WriteString(strtool.Md5(strings.TrimSpace(p)))
}
func (this *ApiController) Upload() {
result := map[string]interface{}{
"success": false,
}
defer func() {
this.Data["json"] = &result
this.ServeJson()
}()
_, header, err := this.GetFile("image")
if err != nil {
return
}
ext := filetool.Ext(header.Filename)
imgPath := fmt.Sprintf("%s/%d%s", EDITOR_IMG_DIR, time.Now().Unix(), ext)
filetool.InsureDir(EDITOR_IMG_DIR)
err = this.SaveToFile("image", imgPath)
if err != nil {
return
}
imgUrl := "/" + imgPath
if g.UseQiniu {
if addr, er := g.UploadFile(imgPath, imgPath); er != nil {
return
} else {
imgUrl = addr
filetool.Unlink(imgPath)
}
}
result["link"] = imgUrl
result["success"] = true
}
func (this *ApiController) Markdown() {
if this.IsAjax() {
result := map[string]interface{}{
"success": false,
}
action := this.GetString("action")
switch action {
case "preview":
content := this.GetString("content")
result["preview"] = g.RenderMarkdown(content)
result["success"] = true
}
this.Data["json"] = result
this.ServeJson()
}
}