@@ -7,17 +7,12 @@ package models
7
7
import (
8
8
"errors"
9
9
"fmt"
10
- "io"
11
- "mime/multipart"
12
- "os"
13
- "path"
14
10
"strings"
15
11
"time"
16
12
17
13
api "code.gitea.io/sdk/gitea"
18
14
"github.com/Unknwon/com"
19
15
"github.com/go-xorm/xorm"
20
- gouuid "github.com/satori/go.uuid"
21
16
22
17
"code.gitea.io/gitea/modules/base"
23
18
"code.gitea.io/gitea/modules/log"
@@ -1740,158 +1735,3 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
1740
1735
return sess .Commit ()
1741
1736
}
1742
1737
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