Skip to content

Commit

Permalink
update & fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakurasan committed Apr 7, 2017
1 parent f5c900b commit cbf9a10
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 4 deletions.
41 changes: 40 additions & 1 deletion controllers/atopic.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package controllers

import (
"github.com/astaxie/beego"
"ONE/models"
"github.com/astaxie/beego"
"net/http"
"os"
"strconv"
"time"
)

type ATopicController struct {
Expand Down Expand Up @@ -131,3 +134,39 @@ func (this *ATopicController) Delete() {
this.Redirect("/topic", 302)
return
}

// 接收文件
func (this *ATopicController) Upload() {
// 获取本月日期
now := time.Now().Format("2006/01")
// 设置保存目录
mpath := "/upload/image/" + now + "/"
// 创建目录
os.MkdirAll(mpath, 0755)

_, h, err := this.GetFile("editormd-image-file")
if err != nil {
this.Data["json"] = map[string]interface{}{"success": 0, "message": err.Error()}
this.ServeJSON()
}

fpath := mpath + h.Filename

for i := 0; ; i++ {
_, err = os.Stat(fpath)
if err == nil {
fpath = mpath + strconv.Itoa(i) + h.Filename
} else {
break
}
}

err = this.SaveToFile("editormd-image-file", fpath)
if err != nil {
this.Data["json"] = map[string]interface{}{"success": 0, "message": err.Error()}
} else {
this.Data["json"] = map[string]interface{}{"success": 1, "message": "文件上传成功!", "url": fpath[1:]}
}

this.ServeJSON()
}
6 changes: 4 additions & 2 deletions controllers/tholeaf.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controllers

import (
"github.com/astaxie/beego"
"ONE/models"
"github.com/astaxie/beego"
"path"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func (this *TholeafController) Post() {
if fh != nil {
image = fh.Filename
beego.Info(image)
err := this.SaveToFile("file", path.Join("static/upload/image", image))
err := this.SaveToFile("file", path.Join("upload/image", image))
if err != nil {
beego.Error(err)
}
Expand All @@ -55,4 +55,6 @@ func (this *TholeafController) Post() {
if err != nil {
beego.Error(err)
}

this.Redirect("/tholeaf", 302)
}
41 changes: 40 additions & 1 deletion controllers/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package controllers
import (
"ONE/models"

//"strconv"
"net/http"

"github.com/astaxie/beego"
"os"
"strconv"
"time"
)

type TopicController struct {
Expand Down Expand Up @@ -95,6 +97,7 @@ func (this *TopicController) View() {

this.Data["Replies"] = replies
this.Data["IsLogin"] = checkAccount(this.Ctx)
this.Data["IsLogin2"] = checkAccount(this.Ctx)

}

Expand Down Expand Up @@ -134,3 +137,39 @@ func (this *TopicController) Delete() {
this.Redirect("/topic", 302)
return
}

// 接收文件
func (this *TopicController) Upload() {
// 获取本月日期
now := time.Now().Format("2006/01")
// 设置保存目录
mpath := "upload/image/" + now + "/"
// 创建目录
os.MkdirAll(mpath, 0755)

_, h, err := this.GetFile("editormd-image-file")
if err != nil {
this.Data["json"] = map[string]interface{}{"success": 0, "message": err.Error()}
this.ServeJSON()
}

fpath := mpath + h.Filename

for i := 0; ; i++ {
_, err = os.Stat(fpath)
if err == nil {
fpath = mpath + strconv.Itoa(i) + h.Filename
} else {
break
}
}

err = this.SaveToFile("editormd-image-file", fpath)
if err != nil {
this.Data["json"] = map[string]interface{}{"success": 0, "message": err.Error()}
} else {
this.Data["json"] = map[string]interface{}{"success": 1, "message": "文件上传成功!", "url": beego.AppConfig.String("url") + fpath[0:]}
}

this.ServeJSON()
}
Binary file added upload/image/2017/03/1f338.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added upload/image/2017/03/未名湖畔好读书.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added upload/image/2017/03/未名湖畔秋色浓.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added upload/image/2017/03/此间.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added upload/image/2017/03/那朵花.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added upload/image/2017/03/面码.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added upload/image/星游记.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cbf9a10

Please sign in to comment.