diff options
author | Unknwon <u@gogs.io> | 2017-03-13 04:32:20 -0400 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2017-03-13 04:32:20 -0400 |
commit | 1df54ea0cde038e2057d083078f952c072d0dc67 (patch) | |
tree | c5466e010f5ed8e236f5b021e669f9aa37ea0047 /routers/repo | |
parent | e6df2259abdeb75545f53a5ca4c247491b5c3d7f (diff) |
release: able to add attchments to release (#1614)
Added new config section '[release.attachment]’.
Diffstat (limited to 'routers/repo')
-rw-r--r-- | routers/repo/download.go | 4 | ||||
-rw-r--r-- | routers/repo/issue.go | 41 | ||||
-rw-r--r-- | routers/repo/pull.go | 2 | ||||
-rw-r--r-- | routers/repo/release.go | 41 |
4 files changed, 61 insertions, 27 deletions
diff --git a/routers/repo/download.go b/routers/repo/download.go index faa30ac9..83d67a88 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -23,7 +23,7 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { if !base.IsTextFile(buf) { if !base.IsImageFile(buf) { - ctx.Resp.Header().Set("Content-Disposition", "attachment; filename=\""+path.Base(ctx.Repo.TreePath)+"\"") + ctx.Resp.Header().Set("Content-Disposition", "attachment; filename=\""+name+"\"") ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary") } } else if !ctx.QueryBool("render") { @@ -40,7 +40,7 @@ func ServeBlob(ctx *context.Context, blob *git.Blob) error { return err } - return ServeData(ctx, ctx.Repo.TreePath, dataRc) + return ServeData(ctx, path.Base(ctx.Repo.TreePath), dataRc) } func SingleDownload(ctx *context.Context) { diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 3b1d9305..3305b8ed 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -348,7 +348,7 @@ func NewIssue(ctx *context.Context) { ctx.HTML(200, ISSUE_NEW) } -func ValidateRepoMetas(ctx *context.Context, f form.CreateIssue) ([]int64, int64, int64) { +func ValidateRepoMetas(ctx *context.Context, f form.NewIssue) ([]int64, int64, int64) { var ( repo = ctx.Repo.Repository err error @@ -402,34 +402,30 @@ func ValidateRepoMetas(ctx *context.Context, f form.CreateIssue) ([]int64, int64 return labelIDs, milestoneID, assigneeID } -func NewIssuePost(ctx *context.Context, f form.CreateIssue) { +func NewIssuePost(ctx *context.Context, f form.NewIssue) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true ctx.Data["RequireHighlightJS"] = true ctx.Data["RequireSimpleMDE"] = true renderAttachmentSettings(ctx) - var ( - repo = ctx.Repo.Repository - attachments []string - ) - labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, f) if ctx.Written() { return } - if setting.AttachmentEnabled { - attachments = f.Files - } - if ctx.HasError() { ctx.HTML(200, ISSUE_NEW) return } + var attachments []string + if setting.AttachmentEnabled { + attachments = f.Files + } + issue := &models.Issue{ - RepoID: repo.ID, + RepoID: ctx.Repo.Repository.ID, Title: f.Title, PosterID: ctx.User.ID, Poster: ctx.User, @@ -437,21 +433,16 @@ func NewIssuePost(ctx *context.Context, f form.CreateIssue) { AssigneeID: assigneeID, Content: f.Content, } - if err := models.NewIssue(repo, issue, labelIDs, attachments); err != nil { + if err := models.NewIssue(ctx.Repo.Repository, issue, labelIDs, attachments); err != nil { ctx.Handle(500, "NewIssue", err) return } - log.Trace("Issue created: %d/%d", repo.ID, issue.ID) + log.Trace("Issue created: %d/%d", ctx.Repo.Repository.ID, issue.ID) ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index)) } -func UploadIssueAttachment(ctx *context.Context) { - if !setting.AttachmentEnabled { - ctx.Error(404, "attachment is not enabled") - return - } - +func uploadAttachment(ctx *context.Context, allowedTypes []string) { file, header, err := ctx.Req.FormFile("file") if err != nil { ctx.Error(500, fmt.Sprintf("FormFile: %v", err)) @@ -466,7 +457,6 @@ func UploadIssueAttachment(ctx *context.Context) { } fileType := http.DetectContentType(buf) - allowedTypes := strings.Split(setting.AttachmentAllowedTypes, ",") allowed := false for _, t := range allowedTypes { t := strings.Trim(t, " ") @@ -493,6 +483,15 @@ func UploadIssueAttachment(ctx *context.Context) { }) } +func UploadIssueAttachment(ctx *context.Context) { + if !setting.AttachmentEnabled { + ctx.NotFound() + return + } + + uploadAttachment(ctx, strings.Split(setting.AttachmentAllowedTypes, ",")) +} + func ViewIssue(ctx *context.Context) { ctx.Data["RequireHighlightJS"] = true ctx.Data["RequireDropzone"] = true diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 736f26e4..c4c5187c 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -634,7 +634,7 @@ func CompareAndPullRequest(ctx *context.Context) { ctx.HTML(200, COMPARE_PULL) } -func CompareAndPullRequestPost(ctx *context.Context, f form.CreateIssue) { +func CompareAndPullRequestPost(ctx *context.Context, f form.NewIssue) { ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes") ctx.Data["PageIsComparePull"] = true ctx.Data["IsDiffCompare"] = true diff --git a/routers/repo/release.go b/routers/repo/release.go index 316598c6..6329de9d 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -6,6 +6,7 @@ package repo import ( "fmt" + "strings" log "gopkg.in/clog.v1" @@ -14,6 +15,7 @@ import ( "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" "github.com/gogits/gogs/modules/markdown" + "github.com/gogits/gogs/modules/setting" ) const ( @@ -148,16 +150,26 @@ func Releases(ctx *context.Context) { ctx.HTML(200, RELEASES) } +func renderReleaseAttachmentSettings(ctx *context.Context) { + ctx.Data["RequireDropzone"] = true + ctx.Data["IsAttachmentEnabled"] = setting.Release.Attachment.Enabled + ctx.Data["AttachmentAllowedTypes"] = strings.Join(setting.Release.Attachment.AllowedTypes, ",") + ctx.Data["AttachmentMaxSize"] = setting.Release.Attachment.MaxSize + ctx.Data["AttachmentMaxFiles"] = setting.Release.Attachment.MaxFiles +} + func NewRelease(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.release.new_release") ctx.Data["PageIsReleaseList"] = true ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch + renderReleaseAttachmentSettings(ctx) ctx.HTML(200, RELEASE_NEW) } func NewReleasePost(ctx *context.Context, f form.NewRelease) { ctx.Data["Title"] = ctx.Tr("repo.release.new_release") ctx.Data["PageIsReleaseList"] = true + renderReleaseAttachmentSettings(ctx) if ctx.HasError() { ctx.HTML(200, RELEASE_NEW) @@ -169,6 +181,7 @@ func NewReleasePost(ctx *context.Context, f form.NewRelease) { return } + // Use current time if tag not yet exist, otherwise get time from Git var tagCreatedUnix int64 tag, err := ctx.Repo.GitRepo.GetTag(f.TagName) if err == nil { @@ -190,6 +203,11 @@ func NewReleasePost(ctx *context.Context, f form.NewRelease) { return } + var attachments []string + if setting.Release.Attachment.Enabled { + attachments = f.Files + } + rel := &models.Release{ RepoID: ctx.Repo.Repository.ID, PublisherID: ctx.User.ID, @@ -203,7 +221,7 @@ func NewReleasePost(ctx *context.Context, f form.NewRelease) { IsPrerelease: f.Prerelease, CreatedUnix: tagCreatedUnix, } - if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil { + if err = models.NewRelease(ctx.Repo.GitRepo, rel, attachments); err != nil { ctx.Data["Err_TagName"] = true switch { case models.IsErrReleaseAlreadyExist(err): @@ -211,7 +229,7 @@ func NewReleasePost(ctx *context.Context, f form.NewRelease) { case models.IsErrInvalidTagName(err): ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &f) default: - ctx.Handle(500, "CreateRelease", err) + ctx.Handle(500, "NewRelease", err) } return } @@ -224,6 +242,7 @@ func EditRelease(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.release.edit_release") ctx.Data["PageIsReleaseList"] = true ctx.Data["PageIsEditRelease"] = true + renderReleaseAttachmentSettings(ctx) tagName := ctx.Params("*") rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName) @@ -240,6 +259,7 @@ func EditRelease(ctx *context.Context) { ctx.Data["tag_target"] = rel.Target ctx.Data["title"] = rel.Title ctx.Data["content"] = rel.Note + ctx.Data["attachments"] = rel.Attachments ctx.Data["prerelease"] = rel.IsPrerelease ctx.Data["IsDraft"] = rel.IsDraft @@ -250,6 +270,7 @@ func EditReleasePost(ctx *context.Context, f form.EditRelease) { ctx.Data["Title"] = ctx.Tr("repo.release.edit_release") ctx.Data["PageIsReleaseList"] = true ctx.Data["PageIsEditRelease"] = true + renderReleaseAttachmentSettings(ctx) tagName := ctx.Params("*") rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName) @@ -265,6 +286,7 @@ func EditReleasePost(ctx *context.Context, f form.EditRelease) { ctx.Data["tag_target"] = rel.Target ctx.Data["title"] = rel.Title ctx.Data["content"] = rel.Note + ctx.Data["attachments"] = rel.Attachments ctx.Data["prerelease"] = rel.IsPrerelease ctx.Data["IsDraft"] = rel.IsDraft @@ -273,18 +295,31 @@ func EditReleasePost(ctx *context.Context, f form.EditRelease) { return } + var attachments []string + if setting.Release.Attachment.Enabled { + attachments = f.Files + } + isPublish := rel.IsDraft && len(f.Draft) == 0 rel.Title = f.Title rel.Note = f.Content rel.IsDraft = len(f.Draft) > 0 rel.IsPrerelease = f.Prerelease - if err = models.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, isPublish); err != nil { + if err = models.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, isPublish, attachments); err != nil { ctx.Handle(500, "UpdateRelease", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/releases") } +func UploadReleaseAttachment(ctx *context.Context) { + if !setting.Release.Attachment.Enabled { + ctx.NotFound() + return + } + uploadAttachment(ctx, setting.Release.Attachment.AllowedTypes) +} + func DeleteRelease(ctx *context.Context) { if err := models.DeleteReleaseOfRepoByID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil { ctx.Flash.Error("DeleteReleaseByID: " + err.Error()) |