From 2cc9bc89698ab693a8ececa7f839483675bf88d6 Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Fri, 14 Aug 2015 03:07:20 +0800
Subject: [PATCH] delete attachments when delete repo

---
 models/repo.go | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/models/repo.go b/models/repo.go
index 73ae6b54a..bad6f3868 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -923,8 +923,9 @@ func DeleteRepository(uid, repoID int64, userName string) error {
 		return err
 	}
 
-	// Delete comments.
+	// Delete comments and attachments.
 	issues := make([]*Issue, 0, 25)
+	attachmentPaths := make([]string, 0, len(issues))
 	if err = sess.Where("repo_id=?", repoID).Find(&issues); err != nil {
 		return err
 	}
@@ -932,6 +933,18 @@ func DeleteRepository(uid, repoID int64, userName string) error {
 		if _, err = sess.Delete(&Comment{IssueID: issues[i].ID}); err != nil {
 			return err
 		}
+
+		attachments := make([]*Attachment, 0, 5)
+		if err = sess.Where("issue_id=?", issues[i].ID).Find(&attachments); err != nil {
+			return err
+		}
+		for j := range attachments {
+			attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
+		}
+
+		if _, err = sess.Delete(&Attachment{IssueID: issues[i].ID}); err != nil {
+			return err
+		}
 	}
 
 	if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
@@ -957,6 +970,13 @@ func DeleteRepository(uid, repoID int64, userName string) error {
 		}
 	}
 
+	// Remove attachment files.
+	for i := range attachmentPaths {
+		if err = os.Remove(attachmentPaths[i]); err != nil {
+			log.Warn("delete attachment: %v", err)
+		}
+	}
+
 	return sess.Commit()
 }