summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorJustin Nuß <nuss.justin@gmail.com>2014-07-23 21:15:47 +0200
committerJustin Nuß <nuss.justin@gmail.com>2014-07-23 21:15:47 +0200
commit4617bef8954deeef5bd2ba36d84aba3b05a4dd83 (patch)
treea3583597828726617cfcf0b4d945e7bf6fb3471c /routers
parent6e9f1c52b18f112eecd5c72e295cfea1809f07fa (diff)
downloadgitea-4617bef8954deeef5bd2ba36d84aba3b05a4dd83.tar.gz
gitea-4617bef8954deeef5bd2ba36d84aba3b05a4dd83.zip
WIP: Allow attachments for comments
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/issue.go144
1 files changed, 142 insertions, 2 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index e71c835fd8..9a265e0959 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -6,6 +6,9 @@ package repo
import (
"fmt"
+ "io"
+ "io/ioutil"
+ "mime"
"net/url"
"strings"
"time"
@@ -396,6 +399,8 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) {
comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink))
}
+ ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes
+
ctx.Data["Title"] = issue.Name
ctx.Data["Issue"] = issue
ctx.Data["Comments"] = comments
@@ -670,7 +675,7 @@ func Comment(ctx *middleware.Context, params martini.Params) {
cmtType = models.IT_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
}
@@ -678,12 +683,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.IT_PLAIN, content); err != nil {
+ if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.IT_PLAIN, content, nil); err != nil {
ctx.Handle(500, "issue.Comment(create comment)", err)
return
}
@@ -709,6 +716,24 @@ func Comment(ctx *middleware.Context, params martini.Params) {
}
}
+ attachments := strings.Split(params["attachments"], ",")
+
+ for _, a := range attachments {
+ aId, err := base.StrTo(a).Int64()
+
+ if err != nil {
+ ctx.Handle(400, "issue.Comment(base.StrTo.Int64)", err)
+ return
+ }
+
+ err = models.AssignAttachment(issue.Id, comment.Id, aId)
+
+ if err != nil {
+ ctx.Handle(400, "issue.Comment(models.AssignAttachment)", err)
+ return
+ }
+ }
+
// Notify watchers.
act := &models.Action{
ActUserId: ctx.User.Id,
@@ -985,3 +1010,118 @@ func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form au
ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones")
}
+
+func IssuePostAttachment(ctx *middleware.Context, params martini.Params) {
+ issueId, _ := base.StrTo(params["index"]).Int64()
+
+ if issueId == 0 {
+ ctx.Handle(400, "issue.IssuePostAttachment", nil)
+ return
+ }
+
+ commentId, err := base.StrTo(params["id"]).Int64()
+
+ if err != nil && len(params["id"]) > 0 {
+ ctx.JSON(400, map[string]interface{}{
+ "ok": false,
+ "error": "invalid comment id",
+ })
+
+ return
+ }
+
+ file, header, err := ctx.Req.FormFile("attachment")
+
+ if err != nil {
+ ctx.JSON(400, map[string]interface{}{
+ "ok": false,
+ "error": "upload error",
+ })
+
+ return
+ }
+
+ defer file.Close()
+
+ // check mime type, write to file, insert attachment to db
+ allowedTypes := strings.Split(setting.AttachmentAllowedTypes, "|")
+ 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.JSON(400, map[string]interface{}{
+ "ok": false,
+ "error": "mime type not allowed",
+ })
+
+ return
+ }
+
+ out, err := ioutil.TempFile(setting.AttachmentPath, "attachment_")
+
+ if err != nil {
+ ctx.JSON(500, map[string]interface{}{
+ "ok": false,
+ "error": "internal server error",
+ })
+
+ return
+ }
+
+ defer out.Close()
+
+ _, err = io.Copy(out, file)
+
+ if err != nil {
+ ctx.JSON(500, map[string]interface{}{
+ "ok": false,
+ "error": "internal server error",
+ })
+
+ return
+ }
+
+ a, err := models.CreateAttachment(issueId, commentId, header.Filename, out.Name())
+
+ if err != nil {
+ ctx.JSON(500, map[string]interface{}{
+ "ok": false,
+ "error": "internal server error",
+ })
+
+ return
+ }
+
+ ctx.JSON(500, map[string]interface{}{
+ "ok": true,
+ "id": a.Id,
+ })
+}
+
+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)
+}