优化工作目录参数

pull/51/merge
Minho 2017-06-05 13:38:06 +08:00
parent 1d6f63972a
commit 8a4eabd158
4 changed files with 25 additions and 21 deletions

View File

@ -19,6 +19,7 @@ import (
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"github.com/lifei6671/godoc/conf" "github.com/lifei6671/godoc/conf"
"github.com/lifei6671/godoc/graphics" "github.com/lifei6671/godoc/graphics"
"github.com/lifei6671/godoc/commands"
) )
type BookController struct { type BookController struct {
@ -302,7 +303,7 @@ func (c *BookController) UploadCover() {
c.JsonResult(500,"图片剪切") c.JsonResult(500,"图片剪切")
} }
filePath = filepath.Join("uploads",time.Now().Format("200601"),fileName + "_small" + ext) filePath = filepath.Join(commands.WorkingDirectory,"uploads",time.Now().Format("200601"),fileName + "_small" + ext)
//生成缩略图并保存到磁盘 //生成缩略图并保存到磁盘
err = graphics.ImageResizeSaveFile(subImg,175,230,filePath) err = graphics.ImageResizeSaveFile(subImg,175,230,filePath)
@ -312,7 +313,11 @@ func (c *BookController) UploadCover() {
c.JsonResult(500,"保存图片失败") c.JsonResult(500,"保存图片失败")
} }
url := "/" + strings.Replace(filePath,"\\","/",-1) url := "/" + strings.Replace(strings.TrimPrefix(filePath,commands.WorkingDirectory),"\\","/",-1)
if strings.HasPrefix(url,"//") {
url = string(url[1:])
}
old_cover := book.Cover old_cover := book.Cover

View File

@ -25,6 +25,7 @@ import (
"github.com/lifei6671/godoc/models" "github.com/lifei6671/godoc/models"
"github.com/lifei6671/godoc/utils" "github.com/lifei6671/godoc/utils"
"github.com/lifei6671/godoc/utils/wkhtmltopdf" "github.com/lifei6671/godoc/utils/wkhtmltopdf"
"github.com/lifei6671/godoc/commands"
) )
//DocumentController struct. //DocumentController struct.
@ -430,7 +431,7 @@ func (c *DocumentController) Upload() {
fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16) fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16)
filePath := filepath.Join("uploads", time.Now().Format("200601"), fileName+ext) filePath := filepath.Join(commands.WorkingDirectory,"uploads", time.Now().Format("200601"), fileName+ext)
path := filepath.Dir(filePath) path := filepath.Dir(filePath)
@ -447,7 +448,7 @@ func (c *DocumentController) Upload() {
attachment.FileName = moreFile.Filename attachment.FileName = moreFile.Filename
attachment.CreateAt = c.Member.MemberId attachment.CreateAt = c.Member.MemberId
attachment.FileExt = ext attachment.FileExt = ext
attachment.FilePath = filePath attachment.FilePath = strings.TrimPrefix(filePath,commands.WorkingDirectory)
attachment.DocumentId = doc_id attachment.DocumentId = doc_id
if fileInfo, err := os.Stat(filePath); err == nil { if fileInfo, err := os.Stat(filePath); err == nil {
@ -458,7 +459,11 @@ func (c *DocumentController) Upload() {
} }
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, "png") || strings.EqualFold(ext, "gif") { if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, "png") || strings.EqualFold(ext, "gif") {
attachment.HttpPath = "/" + strings.Replace(filePath, "\\", "/", -1)
attachment.HttpPath = "/" + strings.Replace(strings.TrimPrefix(filePath,commands.WorkingDirectory), "\\", "/", -1)
if strings.HasPrefix(attachment.HttpPath,"//") {
attachment.HttpPath = string(attachment.HttpPath[1:])
}
is_attach = false is_attach = false
} }
@ -487,19 +492,6 @@ func (c *DocumentController) Upload() {
"is_attach": is_attach, "is_attach": is_attach,
"attach": attachment, "attach": attachment,
} }
//c.Data["json"] = result
//c.ServeJSON(true)
//c.StopRun()
//
//returnJSON, err := json.Marshal(result)
//
//if err != nil {
// beego.Error(err)
//}
//
//c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
//fmt.Fprint(c.Ctx.ResponseWriter,string(returnJSON))
c.Ctx.Output.JSON(result, true, false) c.Ctx.Output.JSON(result, true, false)
c.StopRun() c.StopRun()
} }
@ -554,7 +546,7 @@ func (c *DocumentController) DownloadAttachment() {
if attachment.BookId != book_id { if attachment.BookId != book_id {
c.Abort("404") c.Abort("404")
} }
c.Ctx.Output.Download(attachment.FilePath, attachment.FileName) c.Ctx.Output.Download(strings.Join(commands.WorkingDirectory,attachment.FilePath), attachment.FileName)
c.StopRun() c.StopRun()
} }
@ -595,6 +587,8 @@ func (c *DocumentController) RemoveAttachment() {
beego.Error(err) beego.Error(err)
c.JsonResult(6005, "删除失败") c.JsonResult(6005, "删除失败")
} }
os.Remove(strings.Join(commands.WorkingDirectory,attach.FilePath))
c.JsonResult(0, "ok", attach) c.JsonResult(0, "ok", attach)
} }

View File

@ -13,6 +13,7 @@ import (
"github.com/lifei6671/godoc/utils" "github.com/lifei6671/godoc/utils"
"github.com/lifei6671/godoc/graphics" "github.com/lifei6671/godoc/graphics"
"github.com/lifei6671/godoc/conf" "github.com/lifei6671/godoc/conf"
"github.com/lifei6671/godoc/commands"
) )
type SettingController struct { type SettingController struct {
@ -115,7 +116,7 @@ func (c *SettingController) Upload() {
fileName := "avatar_" + strconv.FormatInt(time.Now().UnixNano(), 16) fileName := "avatar_" + strconv.FormatInt(time.Now().UnixNano(), 16)
filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext filePath := filepath.Join(commands.WorkingDirectory,"uploads" , time.Now().Format("200601") , fileName + ext)
path := filepath.Dir(filePath) path := filepath.Dir(filePath)
@ -144,7 +145,10 @@ func (c *SettingController) Upload() {
c.JsonResult(500,"保存文件失败") c.JsonResult(500,"保存文件失败")
} }
url := "/" + filePath url := "/" + strings.Replace(strings.TrimPrefix(filePath,commands.WorkingDirectory),"\\","/",-1)
if strings.HasPrefix(url,"//") {
url = string(url[1:])
}
if member,err := models.NewMember().Find(c.Member.MemberId);err == nil { if member,err := models.NewMember().Find(c.Member.MemberId);err == nil {
member.Avatar = url member.Avatar = url

View File

@ -8,6 +8,7 @@ import (
"github.com/lifei6671/godoc/conf" "github.com/lifei6671/godoc/conf"
"os" "os"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"strings"
) )
// Attachment struct . // Attachment struct .