Skip to content

Commit f5155b9

Browse files
lunnylafriks
authored andcommitted
Small improve on deleting attachements (#3145)
* Small improve on deleting attachements * improve the sequence of deletion
1 parent cc7b8e3 commit f5155b9

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

models/attachment.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,28 @@ func DeleteAttachment(a *Attachment, remove bool) error {
135135

136136
// DeleteAttachments deletes the given attachments and optionally the associated files.
137137
func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
138-
for i, a := range attachments {
139-
if remove {
138+
if len(attachments) == 0 {
139+
return 0, nil
140+
}
141+
142+
var ids = make([]int64, 0, len(attachments))
143+
for _, a := range attachments {
144+
ids = append(ids, a.ID)
145+
}
146+
147+
cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
148+
if err != nil {
149+
return 0, err
150+
}
151+
152+
if remove {
153+
for i, a := range attachments {
140154
if err := os.Remove(a.LocalPath()); err != nil {
141155
return i, err
142156
}
143157
}
144-
145-
if _, err := x.Delete(a); err != nil {
146-
return i, err
147-
}
148158
}
149-
150-
return len(attachments), nil
159+
return int(cnt), nil
151160
}
152161

153162
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.

0 commit comments

Comments
 (0)