DownloadAttachment 下载附件.
()
| 897 | |
| 898 | // DownloadAttachment 下载附件. |
| 899 | func (this *DocumentController) DownloadAttachment() { |
| 900 | identify := this.Ctx.Input.Param(":key") |
| 901 | attachId, _ := strconv.Atoi(this.Ctx.Input.Param(":attach_id")) |
| 902 | token := this.GetString("token") |
| 903 | |
| 904 | memberId := 0 |
| 905 | |
| 906 | if this.Member != nil { |
| 907 | memberId = this.Member.MemberId |
| 908 | } |
| 909 | bookId := 0 |
| 910 | |
| 911 | //判断用户是否参与了书籍 |
| 912 | bookResult, err := models.NewBookResult().FindByIdentify(identify, memberId) |
| 913 | |
| 914 | if err != nil { |
| 915 | //判断书籍公开状态 |
| 916 | book, err := models.NewBook().FindByFieldFirst("identify", identify) |
| 917 | if err != nil { |
| 918 | this.Abort("404") |
| 919 | } |
| 920 | //如果不是超级管理员则判断权限 |
| 921 | if this.Member == nil || this.Member.Role != conf.MemberSuperRole { |
| 922 | //如果书籍是私有的,并且token不正确 |
| 923 | if (book.PrivatelyOwned == 1 && token == "") || (book.PrivatelyOwned == 1 && book.PrivateToken != token) { |
| 924 | this.Abort("404") |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | bookId = book.BookId |
| 929 | } else { |
| 930 | bookId = bookResult.BookId |
| 931 | } |
| 932 | //查找附件 |
| 933 | attachment, err := models.NewAttachment().Find(attachId) |
| 934 | |
| 935 | if err != nil { |
| 936 | beego.Error("DownloadAttachment => ", err) |
| 937 | if err == orm.ErrNoRows { |
| 938 | this.Abort("404") |
| 939 | } else { |
| 940 | this.Abort("404") |
| 941 | } |
| 942 | } |
| 943 | if attachment.BookId != bookId { |
| 944 | this.Abort("404") |
| 945 | } |
| 946 | this.Ctx.Output.Download(strings.TrimLeft(attachment.FilePath, "./"), attachment.FileName) |
| 947 | |
| 948 | this.StopRun() |
| 949 | } |
| 950 | |
| 951 | // 删除附件. |
| 952 | func (this *DocumentController) RemoveAttachment() { |
nothing calls this directly
no test coverage detected