Skip to content

Commit aad91d4

Browse files
committed
Merge pull request #324 from nuss-justin/issue/318
Fix #318. Issues attachments: allow Select Attachments to append files
2 parents 204ef41 + f0da8a6 commit aad91d4

File tree

6 files changed

+179
-40
lines changed

6 files changed

+179
-40
lines changed

modules/middleware/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ func InitContext() martini.Handler {
369369
}
370370

371371
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
372-
if strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
373-
if err = ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil { // 32MB max size
372+
if r.Method == "POST" && strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
373+
if err = ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
374374
ctx.Handle(500, "issue.Comment(ctx.Req.ParseMultipartForm)", err)
375375
return
376376
}

public/css/gogs.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,4 +1836,11 @@ body {
18361836

18371837
#issue-create-form #attached {
18381838
margin-bottom: 0;
1839+
}
1840+
1841+
#submit-error {
1842+
display: none;
1843+
padding: 10px 15px 15px 15px;
1844+
font-weight: bold;
1845+
text-align: center;
18391846
}

public/js/app.js

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,26 +579,121 @@ function initIssue() {
579579
var $attachedList = $("#attached-list");
580580
var $addButton = $("#attachments-button");
581581

582+
var files = [];
583+
582584
var fileInput = document.getElementById("attachments-input");
583585

584586
if (fileInput === null) {
585587
return;
586588
}
587-
fileInput.addEventListener("change", function(event) {
588-
$attachedList.empty();
589-
$attachedList.append("<b>Attachments:</b> ");
590589

590+
$attachedList.on("click", "span.attachment-remove", function(event) {
591+
var $parent = $(this).parent();
592+
593+
files.splice($parent.data("index"), 1);
594+
$parent.remove();
595+
});
596+
597+
var clickedButton = undefined;
598+
599+
$("button,input[type=\"submit\"]", fileInput.form).on("click", function() {
600+
clickedButton = this;
601+
602+
var $button = $(this);
603+
604+
$button.removeClass("btn-success");
605+
$button.addClass("btn-warning");
606+
607+
$button.text("Submiting...");
608+
});
609+
610+
fileInput.form.addEventListener("submit", function(event) {
611+
event.stopImmediatePropagation();
612+
event.preventDefault();
613+
614+
//var data = new FormData(this);
615+
616+
// Internet Explorer ... -_-
617+
var data = new FormData();
618+
619+
$.each($("[name]", this), function(i, e) {
620+
if (e.name == "attachments" || e.type == "submit") {
621+
return;
622+
}
623+
624+
data.append(e.name, $(e).val());
625+
});
626+
627+
data.append(clickedButton.name, $(clickedButton).val());
628+
629+
files.forEach(function(file) {
630+
data.append("attachments", file);
631+
});
632+
633+
var xhr = new XMLHttpRequest();
634+
635+
xhr.addEventListener("error", function() {
636+
debugger;
637+
});
638+
639+
xhr.addEventListener("load", function() {
640+
var response = xhr.response;
641+
642+
if (typeof response == "string") {
643+
try {
644+
response = JSON.parse(response);
645+
} catch (err) {
646+
response = { ok: false, error: "Could not parse JSON" };
647+
}
648+
}
649+
650+
if (response.ok === false) {
651+
$("#submit-error").text(response.error);
652+
$("#submit-error").show();
653+
654+
var $button = $(clickedButton);
655+
656+
$button.removeClass("btn-warning");
657+
$button.addClass("btn-danger");
658+
659+
$button.text("An error encoured!")
660+
661+
return;
662+
}
663+
664+
window.location.href = response.data;
665+
});
666+
667+
xhr.open("POST", this.action, true);
668+
xhr.send(data);
669+
670+
return false;
671+
});
672+
673+
fileInput.addEventListener("change", function(event) {
591674
for (var index = 0; index < fileInput.files.length; index++) {
592675
var file = fileInput.files[index];
593676

677+
if (files.indexOf(file) > -1) {
678+
continue;
679+
}
680+
594681
var $span = $("<span></span>");
595682

596683
$span.addClass("label");
597684
$span.addClass("label-default");
598685

599-
$span.append(file.name.toLowerCase());
686+
$span.data("index", files.length);
687+
688+
$span.append(file.name);
689+
$span.append(" <span class=\"attachment-remove fa fa-times-circle\"></span>");
690+
600691
$attachedList.append($span);
692+
693+
files.push(file);
601694
}
695+
696+
this.value = "";
602697
});
603698

604699
$addButton.on("click", function() {

routers/repo/issue.go

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,45 @@ func CreateIssue(ctx *middleware.Context, params martini.Params) {
188188
}
189189

190190
func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
191-
ctx.Data["Title"] = "Create issue"
192-
ctx.Data["IsRepoToolbarIssues"] = true
193-
ctx.Data["IsRepoToolbarIssuesList"] = false
194-
ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled
191+
send := func(status int, data interface{}, err error) {
192+
if err != nil {
193+
log.Error("issue.CreateIssuePost(?): %s", err.Error())
194+
195+
ctx.JSON(status, map[string]interface{}{
196+
"ok": false,
197+
"status": status,
198+
"error": err.Error(),
199+
})
200+
} else {
201+
ctx.JSON(status, map[string]interface{}{
202+
"ok": true,
203+
"status": status,
204+
"data": data,
205+
})
206+
}
207+
}
195208

196209
var err error
197210
// Get all milestones.
198-
ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false)
211+
_, err = models.GetMilestones(ctx.Repo.Repository.Id, false)
199212
if err != nil {
200-
ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err)
213+
send(500, nil, err)
201214
return
202215
}
203-
ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true)
216+
_, err = models.GetMilestones(ctx.Repo.Repository.Id, true)
204217
if err != nil {
205-
ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err)
218+
send(500, nil, err)
206219
return
207220
}
208221

209-
us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
222+
_, err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
210223
if err != nil {
211-
ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err)
224+
send(500, nil, err)
212225
return
213226
}
214-
ctx.Data["Collaborators"] = us
215227

216228
if ctx.HasError() {
217-
ctx.HTML(200, ISSUE_CREATE)
229+
send(400, nil, errors.New(ctx.Flash.ErrorMsg))
218230
return
219231
}
220232

@@ -233,11 +245,11 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C
233245
Content: form.Content,
234246
}
235247
if err := models.NewIssue(issue); err != nil {
236-
ctx.Handle(500, "issue.CreateIssue(NewIssue)", err)
248+
send(500, nil, err)
237249
return
238250
} else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id, ctx.Repo.Owner.Id,
239251
ctx.User.Id, form.AssigneeId, ctx.Repo.Repository.Name); err != nil {
240-
ctx.Handle(500, "issue.CreateIssue(NewIssueUserPairs)", err)
252+
send(500, nil, err)
241253
return
242254
}
243255

@@ -253,7 +265,7 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C
253265
}
254266

255267
if err := models.UpdateMentions(ms, issue.Id); err != nil {
256-
ctx.Handle(500, "issue.CreateIssue(UpdateMentions)", err)
268+
send(500, nil, err)
257269
return
258270
}
259271
}
@@ -272,15 +284,15 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C
272284
}
273285
// Notify watchers.
274286
if err := models.NotifyWatchers(act); err != nil {
275-
ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err)
287+
send(500, nil, err)
276288
return
277289
}
278290

279291
// Mail watchers and mentions.
280292
if setting.Service.EnableNotifyMail {
281293
tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
282294
if err != nil {
283-
ctx.Handle(500, "issue.CreateIssue(SendIssueNotifyMail)", err)
295+
send(500, nil, err)
284296
return
285297
}
286298

@@ -295,13 +307,13 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C
295307
}
296308
if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
297309
ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
298-
ctx.Handle(500, "issue.CreateIssue(SendIssueMentionMail)", err)
310+
send(500, nil, err)
299311
return
300312
}
301313
}
302314
log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
303315

304-
ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index))
316+
send(200, fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index), nil)
305317
}
306318

307319
func checkLabels(labels, allLabels []*models.Label) {
@@ -698,19 +710,38 @@ func uploadFiles(ctx *middleware.Context, issueId, commentId int64) {
698710
}
699711

700712
func Comment(ctx *middleware.Context, params martini.Params) {
713+
send := func(status int, data interface{}, err error) {
714+
if err != nil {
715+
log.Error("issue.Comment(?): %s", err.Error())
716+
717+
ctx.JSON(status, map[string]interface{}{
718+
"ok": false,
719+
"status": status,
720+
"error": err.Error(),
721+
})
722+
} else {
723+
ctx.JSON(status, map[string]interface{}{
724+
"ok": true,
725+
"status": status,
726+
"data": data,
727+
})
728+
}
729+
}
730+
701731
index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
702732
if err != nil {
703-
ctx.Handle(404, "issue.Comment(get index)", err)
733+
send(404, nil, err)
704734
return
705735
}
706736

707737
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index)
708738
if err != nil {
709739
if err == models.ErrIssueNotExist {
710-
ctx.Handle(404, "issue.Comment", err)
740+
send(404, nil, err)
711741
} else {
712-
ctx.Handle(200, "issue.Comment(get issue)", err)
742+
send(200, nil, err)
713743
}
744+
714745
return
715746
}
716747

@@ -724,17 +755,17 @@ func Comment(ctx *middleware.Context, params martini.Params) {
724755
(strings.Contains(newStatus, "Close") && !issue.IsClosed) {
725756
issue.IsClosed = !issue.IsClosed
726757
if err = models.UpdateIssue(issue); err != nil {
727-
ctx.Handle(500, "issue.Comment(UpdateIssue)", err)
758+
send(500, nil, err)
728759
return
729760
} else if err = models.UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil {
730-
ctx.Handle(500, "issue.Comment(UpdateIssueUserPairsByStatus)", err)
761+
send(500, nil, err)
731762
return
732763
}
733764

734765
// Change open/closed issue counter for the associated milestone
735766
if issue.MilestoneId > 0 {
736767
if err = models.ChangeMilestoneIssueStats(issue); err != nil {
737-
ctx.Handle(500, "issue.Comment(ChangeMilestoneIssueStats)", err)
768+
send(500, nil, err)
738769
}
739770
}
740771

@@ -744,7 +775,7 @@ func Comment(ctx *middleware.Context, params martini.Params) {
744775
}
745776

746777
if _, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, "", nil); err != nil {
747-
ctx.Handle(200, "issue.Comment(create status change comment)", err)
778+
send(200, nil, err)
748779
return
749780
}
750781
log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed)
@@ -760,7 +791,7 @@ func Comment(ctx *middleware.Context, params martini.Params) {
760791
switch params["action"] {
761792
case "new":
762793
if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.COMMENT, content, nil); err != nil {
763-
ctx.Handle(500, "issue.Comment(create comment)", err)
794+
send(500, nil, err)
764795
return
765796
}
766797

@@ -772,7 +803,7 @@ func Comment(ctx *middleware.Context, params martini.Params) {
772803
}
773804

774805
if err := models.UpdateMentions(ms, issue.Id); err != nil {
775-
ctx.Handle(500, "issue.CreateIssue(UpdateMentions)", err)
806+
send(500, nil, err)
776807
return
777808
}
778809
}
@@ -800,7 +831,7 @@ func Comment(ctx *middleware.Context, params martini.Params) {
800831
RepoName: ctx.Repo.Repository.LowerName,
801832
}
802833
if err = models.NotifyWatchers(act); err != nil {
803-
ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err)
834+
send(500, nil, err)
804835
return
805836
}
806837

@@ -809,7 +840,7 @@ func Comment(ctx *middleware.Context, params martini.Params) {
809840
issue.Content = content
810841
tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
811842
if err != nil {
812-
ctx.Handle(500, "issue.Comment(SendIssueNotifyMail)", err)
843+
send(500, nil, err)
813844
return
814845
}
815846

@@ -824,12 +855,12 @@ func Comment(ctx *middleware.Context, params martini.Params) {
824855
}
825856
if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
826857
ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
827-
ctx.Handle(500, "issue.Comment(SendIssueMentionMail)", err)
858+
send(500, nil, err)
828859
return
829860
}
830861
}
831862

832-
ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index))
863+
send(200, fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index), nil)
833864
}
834865

835866
func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) {

0 commit comments

Comments
 (0)