Skip to content

Commit

Permalink
Small improve on deleting attachements (go-gitea#3145)
Browse files Browse the repository at this point in the history
* Small improve on deleting attachements

* improve the sequence of deletion
  • Loading branch information
lunny authored and lafriks committed Dec 24, 2017
1 parent cc7b8e3 commit f5155b9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f5155b9

Please sign in to comment.