@@ -21,7 +21,6 @@ import (
2121 "fmt"
2222
2323 "github.com/PuerkitoBio/goquery"
24- "github.com/TruthHun/BookStack/commands"
2524 "github.com/TruthHun/BookStack/conf"
2625 "github.com/TruthHun/BookStack/models"
2726 "github.com/TruthHun/BookStack/models/store"
@@ -632,10 +631,12 @@ func (this *DocumentController) Upload() {
632631 }
633632
634633 name := "editormd-file-file"
635- if fileType != "" && ! strings .Contains (fileType , "/" ) {
636- name = "editormd-" + fileType + "-file"
637- } else {
638- fileType = strings .Split (fileType , "/" )[0 ]
634+ if docId == 0 {
635+ if fileType != "" && ! strings .Contains (fileType , "/" ) {
636+ name = "editormd-" + fileType + "-file"
637+ } else {
638+ fileType = strings .Split (fileType , "/" )[0 ]
639+ }
639640 }
640641
641642 file , moreFile , err := this .GetFile (name )
@@ -725,19 +726,21 @@ func (this *DocumentController) Upload() {
725726 attachment .FileName = moreFile .Filename
726727 attachment .CreateAt = this .Member .MemberId
727728 attachment .FileExt = ext
728- attachment .FilePath = strings . TrimPrefix ( filePath , commands . WorkingDirectory )
729+ attachment .FilePath = filePath
729730 attachment .DocumentId = docId
730731
732+ // 非附件
733+ if name != "editormd-file-file" {
734+ attachment .DocumentId = 0
735+ }
736+
731737 if fileInfo , err := os .Stat (filePath ); err == nil {
732738 attachment .FileSize = float64 (fileInfo .Size ())
733739 }
734- if docId > 0 {
735- attachment .DocumentId = docId
736- }
737740
738741 if strings .EqualFold (ext , ".jpg" ) || strings .EqualFold (ext , ".jpeg" ) || strings .EqualFold (ext , ".png" ) || strings .EqualFold (ext , ".gif" ) {
739742
740- attachment .HttpPath = "/" + strings .Replace (strings . TrimPrefix ( filePath , commands . WorkingDirectory ) , "\\ " , "/" , - 1 )
743+ attachment .HttpPath = "/" + strings .Replace (filePath , "\\ " , "/" , - 1 )
741744 if strings .HasPrefix (attachment .HttpPath , "//" ) {
742745 attachment .HttpPath = string (attachment .HttpPath [1 :])
743746 }
@@ -783,7 +786,7 @@ func (this *DocumentController) Upload() {
783786 beego .Error (err .Error ())
784787 }
785788 attachment .HttpPath = "/" + osspath
786- attachment .FilePath = filepath . Join ( commands . WorkingDirectory , osspath )
789+ attachment .FilePath = attachment . HttpPath
787790 } else {
788791 if err := store .ModelStoreLocal .MoveToStore (filePath , osspath ); err != nil {
789792 beego .Error (err .Error ())
@@ -796,7 +799,7 @@ func (this *DocumentController) Upload() {
796799 }
797800 }
798801
799- attachment .HttpPath = "/" + attachment .FilePath
802+ attachment .HttpPath = "/" + strings . TrimLeft ( attachment .FilePath , "./" )
800803
801804 result := map [string ]interface {}{
802805 "errcode" : 0 ,
@@ -859,7 +862,7 @@ func (this *DocumentController) DownloadAttachment() {
859862 if attachment .BookId != bookId {
860863 this .Abort ("404" )
861864 }
862- this .Ctx .Output .Download (filepath . Join ( commands . WorkingDirectory , attachment . FilePath ), attachment .FileName )
865+ this .Ctx .Output .Download (strings . TrimLeft ( attachment . FilePath , "./" ), attachment .FileName )
863866
864867 this .StopRun ()
865868}
@@ -899,7 +902,7 @@ func (this *DocumentController) RemoveAttachment() {
899902 this .JsonResult (6005 , "删除失败" )
900903 }
901904
902- os .Remove (filepath . Join ( commands . WorkingDirectory , attach . FilePath ))
905+ os .Remove (strings . TrimLeft ( attach . FilePath , "./" ))
903906 this .JsonResult (0 , "ok" , attach )
904907}
905908
0 commit comments