Skip to content

Commit 1610b9f

Browse files
coulingbkcsoft
authored andcommitted
Spun attachments into seperate go file (#701)
Moved attachments into seperate go file
1 parent 74bbec3 commit 1610b9f

File tree

4 files changed

+249
-217
lines changed

4 files changed

+249
-217
lines changed

models/attachment.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright 2017 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 models
6+
7+
import (
8+
"fmt"
9+
"io"
10+
"mime/multipart"
11+
"os"
12+
"path"
13+
"time"
14+
15+
"github.com/go-xorm/xorm"
16+
gouuid "github.com/satori/go.uuid"
17+
18+
"code.gitea.io/gitea/modules/setting"
19+
)
20+
21+
// Attachment represent a attachment of issue/comment/release.
22+
type Attachment struct {
23+
ID int64 `xorm:"pk autoincr"`
24+
UUID string `xorm:"uuid UNIQUE"`
25+
IssueID int64 `xorm:"INDEX"`
26+
CommentID int64
27+
ReleaseID int64 `xorm:"INDEX"`
28+
Name string
29+
30+
Created time.Time `xorm:"-"`
31+
CreatedUnix int64
32+
}
33+
34+
35+
// BeforeInsert is invoked from XORM before inserting an object of this type.
36+
func (a *Attachment) BeforeInsert() {
37+
a.CreatedUnix = time.Now().Unix()
38+
}
39+
40+
// AfterSet is invoked from XORM after setting the value of a field of
41+
// this object.
42+
func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
43+
switch colName {
44+
case "created_unix":
45+
a.Created = time.Unix(a.CreatedUnix, 0).Local()
46+
}
47+
}
48+
49+
// AttachmentLocalPath returns where attachment is stored in local file
50+
// system based on given UUID.
51+
func AttachmentLocalPath(uuid string) string {
52+
return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
53+
}
54+
55+
// LocalPath returns where attachment is stored in local file system.
56+
func (a *Attachment) LocalPath() string {
57+
return AttachmentLocalPath(a.UUID)
58+
}
59+
60+
// NewAttachment creates a new attachment object.
61+
func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
62+
attach := &Attachment{
63+
UUID: gouuid.NewV4().String(),
64+
Name: name,
65+
}
66+
67+
localPath := attach.LocalPath()
68+
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
69+
return nil, fmt.Errorf("MkdirAll: %v", err)
70+
}
71+
72+
fw, err := os.Create(localPath)
73+
if err != nil {
74+
return nil, fmt.Errorf("Create: %v", err)
75+
}
76+
defer fw.Close()
77+
78+
if _, err = fw.Write(buf); err != nil {
79+
return nil, fmt.Errorf("Write: %v", err)
80+
} else if _, err = io.Copy(fw, file); err != nil {
81+
return nil, fmt.Errorf("Copy: %v", err)
82+
}
83+
84+
if _, err := x.Insert(attach); err != nil {
85+
return nil, err
86+
}
87+
88+
return attach, nil
89+
}
90+
91+
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
92+
attach := &Attachment{UUID: uuid}
93+
has, err := x.Get(attach)
94+
if err != nil {
95+
return nil, err
96+
} else if !has {
97+
return nil, ErrAttachmentNotExist{0, uuid}
98+
}
99+
return attach, nil
100+
}
101+
102+
func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
103+
if len(uuids) == 0 {
104+
return []*Attachment{}, nil
105+
}
106+
107+
// Silently drop invalid uuids.
108+
attachments := make([]*Attachment, 0, len(uuids))
109+
return attachments, e.In("uuid", uuids).Find(&attachments)
110+
}
111+
112+
// GetAttachmentByUUID returns attachment by given UUID.
113+
func GetAttachmentByUUID(uuid string) (*Attachment, error) {
114+
return getAttachmentByUUID(x, uuid)
115+
}
116+
117+
func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
118+
attachments := make([]*Attachment, 0, 10)
119+
return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
120+
}
121+
122+
// GetAttachmentsByIssueID returns all attachments of an issue.
123+
func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
124+
return getAttachmentsByIssueID(x, issueID)
125+
}
126+
127+
// GetAttachmentsByCommentID returns all attachments if comment by given ID.
128+
func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
129+
attachments := make([]*Attachment, 0, 10)
130+
return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
131+
}
132+
133+
// DeleteAttachment deletes the given attachment and optionally the associated file.
134+
func DeleteAttachment(a *Attachment, remove bool) error {
135+
_, err := DeleteAttachments([]*Attachment{a}, remove)
136+
return err
137+
}
138+
139+
// DeleteAttachments deletes the given attachments and optionally the associated files.
140+
func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
141+
for i, a := range attachments {
142+
if remove {
143+
if err := os.Remove(a.LocalPath()); err != nil {
144+
return i, err
145+
}
146+
}
147+
148+
if _, err := x.Delete(a); err != nil {
149+
return i, err
150+
}
151+
}
152+
153+
return len(attachments), nil
154+
}
155+
156+
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
157+
func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
158+
attachments, err := GetAttachmentsByIssueID(issueID)
159+
160+
if err != nil {
161+
return 0, err
162+
}
163+
164+
return DeleteAttachments(attachments, remove)
165+
}
166+
167+
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
168+
func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
169+
attachments, err := GetAttachmentsByCommentID(commentID)
170+
171+
if err != nil {
172+
return 0, err
173+
}
174+
175+
return DeleteAttachments(attachments, remove)
176+
}

models/issue.go

Lines changed: 0 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ package models
77
import (
88
"errors"
99
"fmt"
10-
"io"
11-
"mime/multipart"
12-
"os"
13-
"path"
1410
"strings"
1511
"time"
1612

1713
api "code.gitea.io/sdk/gitea"
1814
"github.com/Unknwon/com"
1915
"github.com/go-xorm/xorm"
20-
gouuid "github.com/satori/go.uuid"
2116

2217
"code.gitea.io/gitea/modules/base"
2318
"code.gitea.io/gitea/modules/log"
@@ -1740,158 +1735,3 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
17401735
return sess.Commit()
17411736
}
17421737

1743-
// Attachment represent a attachment of issue/comment/release.
1744-
type Attachment struct {
1745-
ID int64 `xorm:"pk autoincr"`
1746-
UUID string `xorm:"uuid UNIQUE"`
1747-
IssueID int64 `xorm:"INDEX"`
1748-
CommentID int64
1749-
ReleaseID int64 `xorm:"INDEX"`
1750-
Name string
1751-
1752-
Created time.Time `xorm:"-"`
1753-
CreatedUnix int64
1754-
}
1755-
1756-
// BeforeInsert is invoked from XORM before inserting an object of this type.
1757-
func (a *Attachment) BeforeInsert() {
1758-
a.CreatedUnix = time.Now().Unix()
1759-
}
1760-
1761-
// AfterSet is invoked from XORM after setting the value of a field of
1762-
// this object.
1763-
func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
1764-
switch colName {
1765-
case "created_unix":
1766-
a.Created = time.Unix(a.CreatedUnix, 0).Local()
1767-
}
1768-
}
1769-
1770-
// AttachmentLocalPath returns where attachment is stored in local file
1771-
// system based on given UUID.
1772-
func AttachmentLocalPath(uuid string) string {
1773-
return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
1774-
}
1775-
1776-
// LocalPath returns where attachment is stored in local file system.
1777-
func (a *Attachment) LocalPath() string {
1778-
return AttachmentLocalPath(a.UUID)
1779-
}
1780-
1781-
// NewAttachment creates a new attachment object.
1782-
func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
1783-
attach := &Attachment{
1784-
UUID: gouuid.NewV4().String(),
1785-
Name: name,
1786-
}
1787-
1788-
localPath := attach.LocalPath()
1789-
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
1790-
return nil, fmt.Errorf("MkdirAll: %v", err)
1791-
}
1792-
1793-
fw, err := os.Create(localPath)
1794-
if err != nil {
1795-
return nil, fmt.Errorf("Create: %v", err)
1796-
}
1797-
defer fw.Close()
1798-
1799-
if _, err = fw.Write(buf); err != nil {
1800-
return nil, fmt.Errorf("Write: %v", err)
1801-
} else if _, err = io.Copy(fw, file); err != nil {
1802-
return nil, fmt.Errorf("Copy: %v", err)
1803-
}
1804-
1805-
if _, err := x.Insert(attach); err != nil {
1806-
return nil, err
1807-
}
1808-
1809-
return attach, nil
1810-
}
1811-
1812-
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
1813-
attach := &Attachment{UUID: uuid}
1814-
has, err := x.Get(attach)
1815-
if err != nil {
1816-
return nil, err
1817-
} else if !has {
1818-
return nil, ErrAttachmentNotExist{0, uuid}
1819-
}
1820-
return attach, nil
1821-
}
1822-
1823-
func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
1824-
if len(uuids) == 0 {
1825-
return []*Attachment{}, nil
1826-
}
1827-
1828-
// Silently drop invalid uuids.
1829-
attachments := make([]*Attachment, 0, len(uuids))
1830-
return attachments, e.In("uuid", uuids).Find(&attachments)
1831-
}
1832-
1833-
// GetAttachmentByUUID returns attachment by given UUID.
1834-
func GetAttachmentByUUID(uuid string) (*Attachment, error) {
1835-
return getAttachmentByUUID(x, uuid)
1836-
}
1837-
1838-
func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
1839-
attachments := make([]*Attachment, 0, 10)
1840-
return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
1841-
}
1842-
1843-
// GetAttachmentsByIssueID returns all attachments of an issue.
1844-
func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
1845-
return getAttachmentsByIssueID(x, issueID)
1846-
}
1847-
1848-
// GetAttachmentsByCommentID returns all attachments if comment by given ID.
1849-
func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
1850-
attachments := make([]*Attachment, 0, 10)
1851-
return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
1852-
}
1853-
1854-
// DeleteAttachment deletes the given attachment and optionally the associated file.
1855-
func DeleteAttachment(a *Attachment, remove bool) error {
1856-
_, err := DeleteAttachments([]*Attachment{a}, remove)
1857-
return err
1858-
}
1859-
1860-
// DeleteAttachments deletes the given attachments and optionally the associated files.
1861-
func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
1862-
for i, a := range attachments {
1863-
if remove {
1864-
if err := os.Remove(a.LocalPath()); err != nil {
1865-
return i, err
1866-
}
1867-
}
1868-
1869-
if _, err := x.Delete(a); err != nil {
1870-
return i, err
1871-
}
1872-
}
1873-
1874-
return len(attachments), nil
1875-
}
1876-
1877-
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
1878-
func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
1879-
attachments, err := GetAttachmentsByIssueID(issueID)
1880-
1881-
if err != nil {
1882-
return 0, err
1883-
}
1884-
1885-
return DeleteAttachments(attachments, remove)
1886-
}
1887-
1888-
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
1889-
func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
1890-
attachments, err := GetAttachmentsByCommentID(commentID)
1891-
1892-
if err != nil {
1893-
return 0, err
1894-
}
1895-
1896-
return DeleteAttachments(attachments, remove)
1897-
}

0 commit comments

Comments
 (0)