mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-20 20:07:03 +01:00
Small improve on deleting attachements (#3145)
* Small improve on deleting attachements * improve the sequence of deletion
This commit is contained in:
parent
cc7b8e3379
commit
f5155b9913
1 changed files with 17 additions and 8 deletions
|
@ -135,19 +135,28 @@ func DeleteAttachment(a *Attachment, remove bool) error {
|
||||||
|
|
||||||
// DeleteAttachments deletes the given attachments and optionally the associated files.
|
// DeleteAttachments deletes the given attachments and optionally the associated files.
|
||||||
func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
|
func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
|
||||||
for i, a := range attachments {
|
if len(attachments) == 0 {
|
||||||
if remove {
|
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 {
|
if err := os.Remove(a.LocalPath()); err != nil {
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := x.Delete(a); err != nil {
|
|
||||||
return i, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return int(cnt), nil
|
||||||
return len(attachments), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
|
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
|
||||||
|
|
Loading…
Reference in a new issue