7
7
"html/template"
8
8
"image/png"
9
9
"io"
10
+ "mime/multipart"
10
11
"net/http"
11
12
"net/url"
12
13
"os"
@@ -486,66 +487,30 @@ func (c *DocumentController) Upload() {
486
487
c .JsonResult (6001 , i18n .Tr (c .Lang , "message.param_error" ))
487
488
}
488
489
489
- name := "editormd-file-file"
490
-
491
- // file, moreFile, err := c.GetFile(name)
492
- // if err == http.ErrMissingFile || moreFile == nil {
493
- // name = "editormd-image-file"
494
- // file, moreFile, err = c.GetFile(name)
495
- // if err == http.ErrMissingFile || moreFile == nil {
496
- // c.JsonResult(6003, i18n.Tr(c.Lang, "message.upload_file_empty"))
497
- // return
498
- // }
499
- // }
500
- // ****3xxx
501
- files , err := c .GetFiles (name )
502
- if err == http .ErrMissingFile {
503
- name = "editormd-image-file"
504
- files , err = c .GetFiles (name )
505
- if err == http .ErrMissingFile {
506
- // c.JsonResult(6003, i18n.Tr(c.Lang, "message.upload_file_empty"))
507
- // return
508
- name = "file"
509
- files , err = c .GetFiles (name )
510
- // logs.Info(files)
511
- if err == http .ErrMissingFile {
512
- c .JsonResult (6003 , i18n .Tr (c .Lang , "message.upload_file_empty" ))
513
- return
514
- }
490
+ names := []string {"editormd-file-file" , "editormd-image-file" , "file" , "editormd-resource-file" }
491
+ var files []* multipart.FileHeader
492
+ for _ , name := range names {
493
+ file , err := c .GetFiles (name )
494
+ if err != nil {
495
+ continue
496
+ }
497
+ if len (file ) > 0 && err == nil {
498
+ files = append (files , file ... )
515
499
}
516
500
}
517
501
518
- // if err != nil {
519
- // http.Error(w, err.Error(), http.StatusNoContent)
520
- // return
521
- // }
522
- // jMap := make(map[string]interface{})
523
- // s := []map[int]interface{}{}
502
+ if len (files ) == 0 {
503
+ c .JsonResult (6003 , i18n .Tr (c .Lang , "message.upload_file_empty" ))
504
+ return
505
+ }
506
+
524
507
result2 := []map [string ]interface {}{}
525
508
var result map [string ]interface {}
526
509
for i , _ := range files {
527
510
//for each fileheader, get a handle to the actual file
528
511
file , err := files [i ].Open ()
529
512
530
513
defer file .Close ()
531
- // if err != nil {
532
- // http.Error(w, err.Error(), http.StatusInternalServerError)
533
- // return
534
- // }
535
- // //create destination file making sure the path is writeable.
536
- // dst, err := os.Create("upload/" + files[i].Filename)
537
- // defer dst.Close()
538
- // if err != nil {
539
- // http.Error(w, err.Error(), http.StatusInternalServerError)
540
- // return
541
- // }
542
- // //copy the uploaded file to the destination file
543
- // if _, err := io.Copy(dst, file); err != nil {
544
- // http.Error(w, err.Error(), http.StatusInternalServerError)
545
- // return
546
- // }
547
- // }
548
- // ****
549
514
550
515
if err != nil {
551
516
c .JsonResult (6002 , err .Error ())
@@ -619,19 +584,25 @@ func (c *DocumentController) Upload() {
619
584
filePath := filepath .Join (conf .WorkingDirectory , "uploads" , identify )
620
585
621
586
//将图片和文件分开存放
622
- // if filetil.IsImageExt(moreFile.Filename) {
587
+ attachment := models .NewAttachment ()
588
+ var strategy filetil.FileTypeStrategy
623
589
if filetil .IsImageExt (files [i ].Filename ) {
624
- filePath = filepath .Join (filePath , "images" , fileName + ext )
590
+ strategy = filetil.ImageStrategy {}
591
+ attachment .ResourceType = "image"
592
+ } else if filetil .IsVideoExt (files [i ].Filename ) {
593
+ strategy = filetil.VideoStrategy {}
594
+ attachment .ResourceType = "video"
625
595
} else {
626
- filePath = filepath .Join (filePath , "files" , fileName + ext )
596
+ strategy = filetil.DefaultStrategy {}
597
+ attachment .ResourceType = "file"
627
598
}
628
599
600
+ filePath = strategy .GetFilePath (filePath , fileName , ext )
601
+
629
602
path := filepath .Dir (filePath )
630
603
631
604
_ = os .MkdirAll (path , os .ModePerm )
632
605
633
- // err = c.SaveToFile(name, filePath) // frome beego controller.go: savetofile it only operates the first one of mutil-upload form file field.
634
-
635
606
//copy the uploaded file to the destination file
636
607
dst , err := os .Create (filePath )
637
608
defer dst .Close ()
@@ -640,12 +611,6 @@ func (c *DocumentController) Upload() {
640
611
c .JsonResult (6005 , i18n .Tr (c .Lang , "message.failed" ))
641
612
}
642
613
643
- // if err != nil {
644
- // logs.Error("保存文件失败 -> ", err)
645
- // c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
646
- // }
647
-
648
- attachment := models .NewAttachment ()
649
614
attachment .BookId = bookId
650
615
// attachment.FileName = moreFile.Filename
651
616
attachment .FileName = files [i ].Filename
@@ -662,8 +627,7 @@ func (c *DocumentController) Upload() {
662
627
attachment .DocumentId = docId
663
628
}
664
629
665
- // if filetil.IsImageExt(moreFile.Filename) {
666
- if filetil .IsImageExt (files [i ].Filename ) {
630
+ if filetil .IsImageExt (files [i ].Filename ) || filetil .IsVideoExt (files [i ].Filename ) {
667
631
attachment .HttpPath = "/" + strings .Replace (strings .TrimPrefix (filePath , conf .WorkingDirectory ), "\\ " , "/" , - 1 )
668
632
if strings .HasPrefix (attachment .HttpPath , "//" ) {
669
633
attachment .HttpPath = conf .URLForWithCdnImage (string (attachment .HttpPath [1 :]))
@@ -689,19 +653,20 @@ func (c *DocumentController) Upload() {
689
653
}
690
654
}
691
655
result = map [string ]interface {}{
692
- "errcode" : 0 ,
693
- "success" : 1 ,
694
- "message" : "ok" ,
695
- "url" : attachment .HttpPath ,
696
- "link" : attachment .HttpPath ,
697
- "alt" : attachment .FileName ,
698
- "is_attach" : isAttach ,
699
- "attach" : attachment ,
656
+ "errcode" : 0 ,
657
+ "success" : 1 ,
658
+ "message" : "ok" ,
659
+ "url" : attachment .HttpPath ,
660
+ "link" : attachment .HttpPath ,
661
+ "alt" : attachment .FileName ,
662
+ "is_attach" : isAttach ,
663
+ "attach" : attachment ,
664
+ "resource_type" : attachment .ResourceType ,
700
665
}
701
666
result2 = append (result2 , result )
702
667
}
703
- if name == "file" {
704
- // froala单图片上传
668
+ if len ( files ) == 1 {
669
+ // froala单文件上传
705
670
c .Ctx .Output .JSON (result , true , false )
706
671
} else {
707
672
c .Ctx .Output .JSON (result2 , true , false )
0 commit comments