Skip to content

Commit 63ff616

Browse files
adelowolafriks
authored andcommitted
Delete releases attachments if release is deleted (#6068)
* delete attachments from the database and file system * add migration * fix import statements * fix package name * remove conditional should in case the confi has been changed and the server restarted * simplify deletion of attachments in DB * fix CI build * fix review * add copyright in the proper place * fix review
1 parent 6322d25 commit 63ff616

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ var migrations = []Migration{
246246
NewMigration("add enable_status_check, status_check_contexts to protected_branch", addStatusCheckColumnsForProtectedBranches),
247247
// v95 -> v96
248248
NewMigration("add table columns for cross referencing issues", addCrossReferenceColumns),
249+
// v96 -> v97
250+
NewMigration("delete orphaned attachments", deleteOrphanedAttachments),
249251
}
250252

251253
// Migrate database to current version

models/migrations/v80.go

-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ func addIsLockedToIssues(x *xorm.Engine) error {
1414
}
1515

1616
return x.Sync2(new(Issue))
17-
1817
}

models/migrations/v96.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"os"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/setting"
12+
13+
"github.com/go-xorm/xorm"
14+
)
15+
16+
func deleteOrphanedAttachments(x *xorm.Engine) error {
17+
18+
type Attachment struct {
19+
ID int64 `xorm:"pk autoincr"`
20+
UUID string `xorm:"uuid UNIQUE"`
21+
IssueID int64 `xorm:"INDEX"`
22+
ReleaseID int64 `xorm:"INDEX"`
23+
CommentID int64
24+
}
25+
26+
sess := x.NewSession()
27+
defer sess.Close()
28+
29+
err := sess.BufferSize(setting.Database.IterateBufferSize).
30+
Where("`comment_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))").Cols("uuid").
31+
Iterate(new(Attachment),
32+
func(idx int, bean interface{}) error {
33+
attachment := bean.(*Attachment)
34+
35+
if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
36+
return err
37+
}
38+
39+
_, err := sess.ID(attachment.ID).NoAutoCondition().Delete(attachment)
40+
return err
41+
})
42+
43+
if err != nil {
44+
return err
45+
}
46+
47+
return sess.Commit()
48+
}

models/release.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package models
67

78
import (
89
"fmt"
10+
"os"
911
"sort"
1012
"strings"
1113

@@ -357,6 +359,17 @@ func DeleteReleaseByID(id int64, doer *User, delTag bool) error {
357359
return fmt.Errorf("LoadAttributes: %v", err)
358360
}
359361

362+
if _, err := x.Delete(&Attachment{ReleaseID: id}); err != nil {
363+
return err
364+
}
365+
366+
for i := range rel.Attachments {
367+
attachment := rel.Attachments[i]
368+
if err := os.RemoveAll(attachment.LocalPath()); err != nil {
369+
return err
370+
}
371+
}
372+
360373
mode, _ := AccessLevel(doer, rel.Repo)
361374
if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
362375
Action: api.HookReleaseDeleted,

0 commit comments

Comments
 (0)