From 4d702eb345d36888b9cace32ba51ac79e9a260f8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Justin=20Nu=C3=9F?= <justin.nuss@hmmh.de>
Date: Thu, 24 Jul 2014 15:51:40 +0200
Subject: [PATCH] Allow disabling uploads

---
 conf/app.ini                     |  2 ++
 modules/setting/setting.go       |  2 ++
 routers/repo/issue.go            | 12 +++++++++++-
 templates/repo/issue/create.tmpl |  4 ++++
 templates/repo/issue/view.tmpl   |  4 ++++
 5 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/conf/app.ini b/conf/app.ini
index 20ff349441..96e320375b 100644
--- a/conf/app.ini
+++ b/conf/app.ini
@@ -181,6 +181,8 @@ SERVICE = server
 DISABLE_GRAVATAR = false
 
 [attachment]
+; Whether attachments are enabled. Defaults to `true`
+ENABLE =
 ; Path for attachments. Defaults to files/attachments
 PATH = 
 ; One or more allowed types, e.g. image/jpeg|image/png
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 349ef11595..569d1bd1c5 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -76,6 +76,7 @@ var (
 	AttachmentAllowedTypes string
 	AttachmentMaxSize      int64
 	AttachmentMaxFiles     int
+	AttachmentEnabled      bool
 
 	// Cache settings.
 	Cache        cache.Cache
@@ -176,6 +177,7 @@ func NewConfigContext() {
 	AttachmentAllowedTypes = Cfg.MustValue("attachment", "ALLOWED_TYPES", "*/*")
 	AttachmentMaxSize = Cfg.MustInt64("attachment", "MAX_SIZE", 32)
 	AttachmentMaxFiles = Cfg.MustInt("attachment", "MAX_FILES", 10)
+	AttachmentEnabled = Cfg.MustBool("attachment", "ENABLE", true)
 
 	if err = os.MkdirAll(AttachmentPath, os.ModePerm); err != nil {
 		log.Fatal("Could not create directory %s: %s", AttachmentPath, err)
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 81261e6cd5..c033e0f31c 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -160,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.
@@ -190,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.
@@ -239,7 +241,9 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C
 		return
 	}
 
-	uploadFiles(ctx, issue.Id, 0)
+	if setting.AttachmentEnabled {
+		uploadFiles(ctx, issue.Id, 0)
+	}
 
 	// Update mentions.
 	ms := base.MentionPattern.FindAllString(issue.Content, -1)
@@ -313,6 +317,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)
@@ -628,6 +634,10 @@ 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"]
 
diff --git a/templates/repo/issue/create.tmpl b/templates/repo/issue/create.tmpl
index 0d72123b81..7705841708 100644
--- a/templates/repo/issue/create.tmpl
+++ b/templates/repo/issue/create.tmpl
@@ -101,13 +101,17 @@
                         <div class="tab-pane issue-preview-content" id="issue-preview">loading...</div>
                     </div>
                 </div>
+                {{if .AttachmentsEnabled}}
                 <div id="attached">
                     <div id="attached-list"></div>
                 </div>
+                {{end}}
                 <div class="text-right panel-body">
                     <div class="form-group">
+                        {{if .AttachmentsEnabled}}
                         <input type="file" accept="{{.AllowedTypes}}" style="display: none;" id="attachments-input" name="attachments" multiple />
                         <button class="btn-default btn attachment-add" id="attachments-button">Select Attachments...</button>
+                        {{end}}
                         <input type="hidden" value="id" name="repo-id"/>
                         <button class="btn-success btn">Create new issue</button>
                     </div>
diff --git a/templates/repo/issue/view.tmpl b/templates/repo/issue/view.tmpl
index dd200e8016..570698975b 100644
--- a/templates/repo/issue/view.tmpl
+++ b/templates/repo/issue/view.tmpl
@@ -137,13 +137,17 @@
                                     <div class="tab-pane issue-preview-content" id="issue-preview">Loading...</div>
                                 </div>
                             </div>
+                            {{if .AttachmentsEnabled}}
                             <div id="attached">
                                 <div id="attached-list"></div>
                             </div>
+                            {{end}}
                             <div class="text-right">
                                 <div class="form-group">
+                                    {{if .AttachmentsEnabled}}
                                     <input type="file" accept="{{.AllowedTypes}}" style="display: none;" id="attachments-input" name="attachments" multiple />
                                     <button class="btn-default btn attachment-add" id="attachments-button">Select Attachments...</button>
+                                    {{end}}
                                     {{if .IsIssueOwner}}{{if .Issue.IsClosed}}
                                     <input type="submit" class="btn-default btn issue-open" id="issue-open-btn" data-origin="Reopen" data-text="Reopen & Comment" name="change_status" value="Reopen"/>{{else}}
                                     <input type="submit" class="btn-default btn issue-close" id="issue-close-btn" data-origin="Close" data-text="Close & Comment" name="change_status" value="Close"/>{{end}}{{end}}&nbsp;&nbsp;