diff --git a/controllers/book.go b/controllers/book.go index 7ddb4e34..12b93469 100644 --- a/controllers/book.go +++ b/controllers/book.go @@ -19,6 +19,7 @@ import ( "github.com/astaxie/beego/logs" "github.com/lifei6671/godoc/conf" "github.com/lifei6671/godoc/graphics" + "github.com/lifei6671/godoc/commands" ) type BookController struct { @@ -302,7 +303,7 @@ func (c *BookController) UploadCover() { 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) @@ -312,7 +313,11 @@ func (c *BookController) UploadCover() { 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 diff --git a/controllers/document.go b/controllers/document.go index 18742504..91421985 100644 --- a/controllers/document.go +++ b/controllers/document.go @@ -25,6 +25,7 @@ import ( "github.com/lifei6671/godoc/models" "github.com/lifei6671/godoc/utils" "github.com/lifei6671/godoc/utils/wkhtmltopdf" + "github.com/lifei6671/godoc/commands" ) //DocumentController struct. @@ -430,7 +431,7 @@ func (c *DocumentController) Upload() { 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) @@ -447,7 +448,7 @@ func (c *DocumentController) Upload() { attachment.FileName = moreFile.Filename attachment.CreateAt = c.Member.MemberId attachment.FileExt = ext - attachment.FilePath = filePath + attachment.FilePath = strings.TrimPrefix(filePath,commands.WorkingDirectory) attachment.DocumentId = doc_id 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") { - 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 } @@ -487,19 +492,6 @@ func (c *DocumentController) Upload() { "is_attach": is_attach, "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.StopRun() } @@ -554,7 +546,7 @@ func (c *DocumentController) DownloadAttachment() { if attachment.BookId != book_id { 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() } @@ -595,6 +587,8 @@ func (c *DocumentController) RemoveAttachment() { beego.Error(err) c.JsonResult(6005, "删除失败") } + os.Remove(strings.Join(commands.WorkingDirectory,attach.FilePath)) + c.JsonResult(0, "ok", attach) } diff --git a/controllers/setting.go b/controllers/setting.go index 1a31d758..3eb07e22 100644 --- a/controllers/setting.go +++ b/controllers/setting.go @@ -13,6 +13,7 @@ import ( "github.com/lifei6671/godoc/utils" "github.com/lifei6671/godoc/graphics" "github.com/lifei6671/godoc/conf" + "github.com/lifei6671/godoc/commands" ) type SettingController struct { @@ -115,7 +116,7 @@ func (c *SettingController) Upload() { 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) @@ -144,7 +145,10 @@ func (c *SettingController) Upload() { 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 { member.Avatar = url diff --git a/models/attachment.go b/models/attachment.go index f691ffd4..102ca953 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -8,6 +8,7 @@ import ( "github.com/lifei6671/godoc/conf" "os" "github.com/astaxie/beego" + "strings" ) // Attachment struct .