@@ -291,6 +291,15 @@ func (this *DocumentController) Read() {
291291 attach , err := models .NewAttachment ().FindListByDocumentId (doc .DocumentId )
292292 if err == nil {
293293 doc .AttachList = attach
294+ if len (attach ) > 0 {
295+ content := bytes .NewBufferString ("<div class=\" attach-list\" ><strong>附件</strong><ul>" )
296+ for _ , item := range attach {
297+ li := fmt .Sprintf ("<li><a href=\" %s\" target=\" _blank\" title=\" %s\" >%s</a></li>" , item .HttpPath , item .FileName , item .FileName )
298+ content .WriteString (li )
299+ }
300+ content .WriteString ("</ul></div>" )
301+ doc .Release += content .String ()
302+ }
294303 }
295304
296305 //文档阅读人次+1
@@ -632,7 +641,6 @@ func (this *DocumentController) Upload() {
632641 identify := this .GetString ("identify" )
633642 docId , _ := this .GetInt ("doc_id" )
634643 fileType := this .GetString ("type" )
635- isAttach := true
636644
637645 if identify == "" {
638646 this .JsonResult (6001 , "参数错误" )
@@ -717,105 +725,58 @@ func (this *DocumentController) Upload() {
717725
718726 fileName := strconv .FormatInt (time .Now ().UnixNano (), 16 )
719727
720- filePath := filepath . Join ( "uploads/projects" , bookIdentify , time . Now (). Format ( "200601" ), fileName + ext )
721-
722- path := filepath . Dir ( filePath )
723-
724- os . MkdirAll ( path , os . ModePerm )
725-
726- err = this . SaveToFile ( name , filePath )
728+ prefix := "uploads"
729+ savePath := filepath . Join ( "projects" , bookIdentify , time . Now (). Format ( "200601" ), fileName + ext )
730+ if utils . StoreType != utils . StoreOss {
731+ savePath = filepath . Join ( prefix , savePath )
732+ }
733+ savePath = strings . ReplaceAll ( savePath , " \\ " , "/" )
734+ os . MkdirAll ( filepath . Dir ( savePath ), os . ModePerm )
727735
736+ err = this .SaveToFile (name , savePath )
728737 if err != nil {
729738 beego .Error ("SaveToFile => " , err )
730739 this .JsonResult (6005 , "保存文件失败" )
731740 }
741+
732742 attachment := models .NewAttachment ()
733743 attachment .BookId = bookId
734744 attachment .FileName = moreFile .Filename
735745 attachment .CreateAt = this .Member .MemberId
736746 attachment .FileExt = ext
737- attachment .FilePath = filePath
747+ attachment .FilePath = "/" + savePath
748+ attachment .HttpPath = attachment .FilePath
738749 attachment .DocumentId = docId
739750
740751 // 非附件
741752 if name != "editormd-file-file" {
742753 attachment .DocumentId = 0
743754 }
744755
745- if fileInfo , err := os .Stat (filePath ); err == nil {
756+ if fileInfo , err := os .Stat (savePath ); err == nil {
746757 attachment .FileSize = float64 (fileInfo .Size ())
747758 }
748759
749- if strings .EqualFold (ext , ".jpg" ) || strings .EqualFold (ext , ".jpeg" ) || strings .EqualFold (ext , ".png" ) || strings .EqualFold (ext , ".gif" ) {
750-
751- attachment .HttpPath = "/" + strings .Replace (filePath , "\\ " , "/" , - 1 )
752- if strings .HasPrefix (attachment .HttpPath , "//" ) {
753- attachment .HttpPath = string (attachment .HttpPath [1 :])
754- }
755- isAttach = false
756- }
757-
758- err = attachment .Insert ()
759-
760- if err != nil {
761- os .Remove (filePath )
760+ // 数据入库
761+ if err = attachment .Insert (); err != nil {
762+ os .Remove (savePath )
762763 beego .Error ("Attachment Insert => " , err )
763764 this .JsonResult (6006 , "文件保存失败" )
764765 }
765- if attachment .HttpPath == "" {
766- attachment .HttpPath = beego .URLFor ("DocumentController.DownloadAttachment" , ":key" , identify , ":attach_id" , attachment .AttachmentId )
767-
768- if err := attachment .Update (); err != nil {
769- beego .Error ("SaveToFile => " , err )
770- this .JsonResult (6005 , "保存文件失败" )
771- }
772- }
773766
774- //文件和图片分开放在书籍文件夹内
775- var osspath = ""
776- if strings .EqualFold (ext , ".jpg" ) || strings .EqualFold (ext , ".jpeg" ) || strings .EqualFold (ext , ".png" ) || strings .EqualFold (ext , ".gif" ) {
777- osspath = fmt .Sprintf ("projects/%v/%v" , identify , fileName + filepath .Ext (attachment .HttpPath ))
778- } else {
779- osspath = strings .Replace (filepath .Join ("projects" , identify , "files" , fileName + ext ), "\\ " , "/" , - 1 )
780- }
781-
782- switch utils .StoreType {
783- case utils .StoreOss :
784- if err := store .ModelStoreOss .MoveToOss ("." + attachment .HttpPath , osspath , true , false ); err != nil {
767+ if utils .StoreType == utils .StoreOss {
768+ if err := store .ModelStoreOss .MoveToOss (savePath , savePath , true , false ); err != nil {
785769 beego .Error (err .Error ())
786770 }
787- //attachment.HttpPath = this.OssDomain + "/" + osspath
788- attachment .HttpPath = "/" + osspath
789- case utils .StoreLocal :
790- osspath = "uploads/" + osspath
791- //图片是正确的,先不修改
792- if strings .EqualFold (ext , ".jpg" ) || strings .EqualFold (ext , ".jpeg" ) || strings .EqualFold (ext , ".png" ) || strings .EqualFold (ext , ".gif" ) {
793- if err := store .ModelStoreLocal .MoveToStore ("." + attachment .HttpPath , osspath ); err != nil {
794- beego .Error (err .Error ())
795- }
796- attachment .HttpPath = "/" + osspath
797- attachment .FilePath = attachment .HttpPath
798- } else {
799- if err := store .ModelStoreLocal .MoveToStore (filePath , osspath ); err != nil {
800- beego .Error (err .Error ())
801- }
802- attachment .FilePath = osspath
803- }
804- if err := attachment .Update (); err != nil {
805- beego .Error ("SaveToFile => " , err )
806- this .JsonResult (6005 , "保存文件失败" )
807- }
808771 }
809772
810- attachment .HttpPath = "/" + strings .TrimLeft (attachment .FilePath , "./" )
811-
812773 result := map [string ]interface {}{
813774 "errcode" : 0 ,
814775 "success" : 1 ,
815776 "message" : "ok" ,
816777 "url" : attachment .HttpPath ,
817778 "alt" : attachment .FileName ,
818- "is_attach" : isAttach ,
779+ "is_attach" : attachment . DocumentId > 0 ,
819780 "attach" : attachment ,
820781 }
821782 this .Ctx .Output .JSON (result , true , false )
@@ -973,7 +934,7 @@ func (this *DocumentController) Delete() {
973934 this .JsonResult (0 , "ok" )
974935}
975936
976- //获取或更新文档内容.
937+ // 获取或更新文档内容.
977938func (this * DocumentController ) Content () {
978939 identify := this .Ctx .Input .Param (":key" )
979940 docId , err := this .GetInt ("doc_id" )
0 commit comments