@@ -1860,6 +1860,17 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
1860
1860
}
1861
1861
}
1862
1862
1863
+ attachments := make ([]* Attachment , 0 , 20 )
1864
+ if err = sess .Join ("INNER" , "`release`" , "`release`.id = `attachment`.release_id" ).
1865
+ Where ("`release`.repo_id = ?" , repoID ).
1866
+ Find (& attachments ); err != nil {
1867
+ return err
1868
+ }
1869
+ releaseAttachments := make ([]string , 0 , len (attachments ))
1870
+ for i := 0 ; i < len (attachments ); i ++ {
1871
+ releaseAttachments = append (releaseAttachments , attachments [i ].LocalPath ())
1872
+ }
1873
+
1863
1874
if err = deleteBeans (sess ,
1864
1875
& Access {RepoID : repo .ID },
1865
1876
& Action {RepoID : repo .ID },
@@ -1910,13 +1921,13 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
1910
1921
return err
1911
1922
}
1912
1923
1913
- attachmentPaths := make ([]string , 0 , 20 )
1914
- attachments := make ([]* Attachment , 0 , len (attachmentPaths ))
1924
+ attachments = attachments [:0 ]
1915
1925
if err = sess .Join ("INNER" , "issue" , "issue.id = attachment.issue_id" ).
1916
1926
Where ("issue.repo_id = ?" , repoID ).
1917
1927
Find (& attachments ); err != nil {
1918
1928
return err
1919
1929
}
1930
+ attachmentPaths := make ([]string , 0 , len (attachments ))
1920
1931
for j := range attachments {
1921
1932
attachmentPaths = append (attachmentPaths , attachments [j ].LocalPath ())
1922
1933
}
@@ -1953,11 +1964,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
1953
1964
return err
1954
1965
}
1955
1966
1956
- // Remove attachment files.
1957
- for i := range attachmentPaths {
1958
- removeAllWithNotice (sess , "Delete attachment" , attachmentPaths [i ])
1959
- }
1960
-
1961
1967
// Remove LFS objects
1962
1968
var lfsObjects []* LFSMetaObject
1963
1969
if err = sess .Where ("repository_id=?" , repoID ).Find (& lfsObjects ); err != nil {
@@ -1997,6 +2003,8 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
1997
2003
return fmt .Errorf ("Commit: %v" , err )
1998
2004
}
1999
2005
2006
+ sess .Close ()
2007
+
2000
2008
if org .IsOrganization () {
2001
2009
if err = PrepareWebhooks (repo , HookEventRepository , & api.RepositoryPayload {
2002
2010
Action : api .HookRepoDeleted ,
@@ -2009,6 +2017,19 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
2009
2017
go HookQueue .Add (repo .ID )
2010
2018
}
2011
2019
2020
+ // We should always delete the files after the database transaction succeed. If
2021
+ // we delete the file but the database rollback, the repository will be borken.
2022
+
2023
+ // Remove issue attachment files.
2024
+ for i := range attachmentPaths {
2025
+ removeAllWithNotice (x , "Delete issue attachment" , attachmentPaths [i ])
2026
+ }
2027
+
2028
+ // Remove release attachment files.
2029
+ for i := range releaseAttachments {
2030
+ removeAllWithNotice (x , "Delete release attachment" , releaseAttachments [i ])
2031
+ }
2032
+
2012
2033
if len (repo .Avatar ) > 0 {
2013
2034
avatarPath := repo .CustomAvatarPath ()
2014
2035
if com .IsExist (avatarPath ) {
0 commit comments