diff options
author | Justin Nuß <nuss.justin@gmail.com> | 2014-07-24 19:15:35 +0200 |
---|---|---|
committer | Justin Nuß <nuss.justin@gmail.com> | 2014-07-24 19:15:35 +0200 |
commit | 620c0ef297ceca370f8e2946a72a2a1c40c16347 (patch) | |
tree | 8f9d0911db7d2920b6cf6d0de18ad61b1042b543 /routers/repo | |
parent | cbcd08aa17e95a72dcb294da836aa7f2b966d5c0 (diff) | |
parent | c20f5dc2ea1b27e80c28e00831278c7451ba6cce (diff) | |
download | gitea-620c0ef297ceca370f8e2946a72a2a1c40c16347.tar.gz gitea-620c0ef297ceca370f8e2946a72a2a1c40c16347.zip |
Merge branch 'dev' of https://github.com/gogits/Gogs into trello/244
Diffstat (limited to 'routers/repo')
-rw-r--r-- | routers/repo/issue.go | 115 |
1 files changed, 113 insertions, 2 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 402660ecd2..f3d9280644 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -5,7 +5,11 @@ package repo import ( + "errors" "fmt" + "io" + "io/ioutil" + "mime" "net/url" "strings" "time" @@ -32,6 +36,11 @@ const ( MILESTONE_EDIT base.TplName = "repo/issue/milestone_edit" ) +var ( + ErrFileTypeForbidden = errors.New("File type is not allowed") + ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded") +) + func Issues(ctx *middleware.Context) { ctx.Data["Title"] = "Issues" ctx.Data["IsRepoToolbarIssues"] = true @@ -151,6 +160,7 @@ func CreateIssue(ctx *middleware.Context, params martini.Params) { ctx.Data["Title"] = "Create issue" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = false + ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled var err error // Get all milestones. @@ -170,7 +180,10 @@ func CreateIssue(ctx *middleware.Context, params martini.Params) { ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) return } + + ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes ctx.Data["Collaborators"] = us + ctx.HTML(200, ISSUE_CREATE) } @@ -178,6 +191,7 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C ctx.Data["Title"] = "Create issue" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = false + ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled var err error // Get all milestones. @@ -227,6 +241,10 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C return } + if setting.AttachmentEnabled { + uploadFiles(ctx, issue.Id, 0) + } + // Update mentions. ms := base.MentionPattern.FindAllString(issue.Content, -1) if len(ms) > 0 { @@ -298,6 +316,8 @@ func checkLabels(labels, allLabels []*models.Label) { } func ViewIssue(ctx *middleware.Context, params martini.Params) { + ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled + idx, _ := base.StrTo(params["index"]).Int64() if idx == 0 { ctx.Handle(404, "issue.ViewIssue", nil) @@ -398,6 +418,8 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { } } + ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes + ctx.Data["Title"] = issue.Name ctx.Data["Issue"] = issue ctx.Data["Comments"] = comments @@ -610,6 +632,71 @@ func UpdateAssignee(ctx *middleware.Context) { }) } +func uploadFiles(ctx *middleware.Context, issueId, commentId int64) { + if !setting.AttachmentEnabled { + return + } + + allowedTypes := strings.Split(setting.AttachmentAllowedTypes, "|") + attachments := ctx.Req.MultipartForm.File["attachments"] + + if len(attachments) > setting.AttachmentMaxFiles { + ctx.Handle(400, "issue.Comment", ErrTooManyFiles) + return + } + + for _, header := range attachments { + file, err := header.Open() + + if err != nil { + ctx.Handle(500, "issue.Comment(header.Open)", err) + return + } + + defer file.Close() + + allowed := false + fileType := mime.TypeByExtension(header.Filename) + + for _, t := range allowedTypes { + t := strings.Trim(t, " ") + + if t == "*/*" || t == fileType { + allowed = true + break + } + } + + if !allowed { + ctx.Handle(400, "issue.Comment", ErrFileTypeForbidden) + return + } + + out, err := ioutil.TempFile(setting.AttachmentPath, "attachment_") + + if err != nil { + ctx.Handle(500, "issue.Comment(ioutil.TempFile)", err) + return + } + + defer out.Close() + + _, err = io.Copy(out, file) + + if err != nil { + ctx.Handle(500, "issue.Comment(io.Copy)", err) + return + } + + _, err = models.CreateAttachment(issueId, commentId, header.Filename, out.Name()) + + if err != nil { + ctx.Handle(500, "issue.Comment(io.Copy)", err) + return + } + } +} + func Comment(ctx *middleware.Context, params martini.Params) { index, err := base.StrTo(ctx.Query("issueIndex")).Int64() if err != nil { @@ -656,7 +743,7 @@ func Comment(ctx *middleware.Context, params martini.Params) { cmtType = models.REOPEN } - if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, ""); err != nil { + if _, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, "", nil); err != nil { ctx.Handle(200, "issue.Comment(create status change comment)", err) return } @@ -664,12 +751,14 @@ func Comment(ctx *middleware.Context, params martini.Params) { } } + var comment *models.Comment + var ms []string content := ctx.Query("content") if len(content) > 0 { switch params["action"] { case "new": - if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.COMMENT, content); err != nil { + if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.COMMENT, content, nil); err != nil { ctx.Handle(500, "issue.Comment(create comment)", err) return } @@ -694,6 +783,10 @@ func Comment(ctx *middleware.Context, params martini.Params) { } } + if comment != nil { + uploadFiles(ctx, issue.Id, comment.Id) + } + // Notify watchers. act := &models.Action{ ActUserId: ctx.User.Id, @@ -970,3 +1063,21 @@ func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form au ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") } + +func IssueGetAttachment(ctx *middleware.Context, params martini.Params) { + id, err := base.StrTo(params["id"]).Int64() + + if err != nil { + ctx.Handle(400, "issue.IssueGetAttachment(base.StrTo.Int64)", err) + return + } + + attachment, err := models.GetAttachmentById(id) + + if err != nil { + ctx.Handle(404, "issue.IssueGetAttachment(models.GetAttachmentById)", err) + return + } + + ctx.ServeFile(attachment.Path, attachment.Name) +} |