aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-12-24 15:04:22 -0600
committerLauris BH <lauris@nix.lv>2017-12-24 23:04:22 +0200
commitf5155b99136b6e19ab494a97adfcee1810a3d5e7 (patch)
tree2fb34ac49c22134884b48699bc6d14b4c4500fba /models
parentcc7b8e3379f46469d3ec72b044fb0f993fec4d1b (diff)
downloadgitea-f5155b99136b6e19ab494a97adfcee1810a3d5e7.tar.gz
gitea-f5155b99136b6e19ab494a97adfcee1810a3d5e7.zip
Small improve on deleting attachements (#3145)
* Small improve on deleting attachements * improve the sequence of deletion
Diffstat (limited to 'models')
-rw-r--r--models/attachment.go25
1 files changed, 17 insertions, 8 deletions
diff --git a/models/attachment.go b/models/attachment.go
index e7e0dbf5c2..acb1f0716c 100644
--- a/models/attachment.go
+++ b/models/attachment.go
@@ -135,19 +135,28 @@ func DeleteAttachment(a *Attachment, remove bool) error {
// DeleteAttachments deletes the given attachments and optionally the associated files.
func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
- for i, a := range attachments {
- if remove {
+ if len(attachments) == 0 {
+ return 0, nil
+ }
+
+ var ids = make([]int64, 0, len(attachments))
+ for _, a := range attachments {
+ ids = append(ids, a.ID)
+ }
+
+ cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
+ if err != nil {
+ return 0, err
+ }
+
+ if remove {
+ for i, a := range attachments {
if err := os.Remove(a.LocalPath()); err != nil {
return i, err
}
}
-
- if _, err := x.Delete(a); err != nil {
- return i, err
- }
}
-
- return len(attachments), nil
+ return int(cnt), nil
}
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.