1
0

attachment.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package database
  2. import (
  3. "fmt"
  4. "io"
  5. "mime/multipart"
  6. "os"
  7. "path"
  8. "time"
  9. "github.com/cockroachdb/errors"
  10. gouuid "github.com/satori/go.uuid"
  11. "xorm.io/xorm"
  12. "gogs.io/gogs/internal/conf"
  13. "gogs.io/gogs/internal/errutil"
  14. )
  15. // Attachment represent a attachment of issue/comment/release.
  16. type Attachment struct {
  17. ID int64
  18. UUID string `xorm:"uuid UNIQUE"`
  19. IssueID int64 `xorm:"INDEX"`
  20. CommentID int64
  21. ReleaseID int64 `xorm:"INDEX"`
  22. Name string
  23. Created time.Time `xorm:"-" json:"-" gorm:"-"`
  24. CreatedUnix int64
  25. }
  26. func (a *Attachment) BeforeInsert() {
  27. a.CreatedUnix = time.Now().Unix()
  28. }
  29. func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
  30. switch colName {
  31. case "created_unix":
  32. a.Created = time.Unix(a.CreatedUnix, 0).Local()
  33. }
  34. }
  35. // AttachmentLocalPath returns where attachment is stored in local file system based on given UUID.
  36. func AttachmentLocalPath(uuid string) string {
  37. return path.Join(conf.Attachment.Path, uuid[0:1], uuid[1:2], uuid)
  38. }
  39. // LocalPath returns where attachment is stored in local file system.
  40. func (a *Attachment) LocalPath() string {
  41. return AttachmentLocalPath(a.UUID)
  42. }
  43. // NewAttachment creates a new attachment object.
  44. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  45. attach := &Attachment{
  46. UUID: gouuid.NewV4().String(),
  47. Name: name,
  48. }
  49. localPath := attach.LocalPath()
  50. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  51. return nil, errors.Newf("MkdirAll: %v", err)
  52. }
  53. fw, err := os.Create(localPath)
  54. if err != nil {
  55. return nil, errors.Newf("Create: %v", err)
  56. }
  57. defer fw.Close()
  58. if _, err = fw.Write(buf); err != nil {
  59. return nil, errors.Newf("write: %v", err)
  60. } else if _, err = io.Copy(fw, file); err != nil {
  61. return nil, errors.Newf("copy: %v", err)
  62. }
  63. if _, err := x.Insert(attach); err != nil {
  64. return nil, err
  65. }
  66. return attach, nil
  67. }
  68. var _ errutil.NotFound = (*ErrAttachmentNotExist)(nil)
  69. type ErrAttachmentNotExist struct {
  70. args map[string]any
  71. }
  72. func IsErrAttachmentNotExist(err error) bool {
  73. _, ok := err.(ErrAttachmentNotExist)
  74. return ok
  75. }
  76. func (err ErrAttachmentNotExist) Error() string {
  77. return fmt.Sprintf("attachment does not exist: %v", err.args)
  78. }
  79. func (ErrAttachmentNotExist) NotFound() bool {
  80. return true
  81. }
  82. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  83. attach := &Attachment{UUID: uuid}
  84. has, err := e.Get(attach)
  85. if err != nil {
  86. return nil, err
  87. } else if !has {
  88. return nil, ErrAttachmentNotExist{args: map[string]any{"uuid": uuid}}
  89. }
  90. return attach, nil
  91. }
  92. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  93. if len(uuids) == 0 {
  94. return []*Attachment{}, nil
  95. }
  96. // Silently drop invalid uuids.
  97. attachments := make([]*Attachment, 0, len(uuids))
  98. return attachments, e.In("uuid", uuids).Find(&attachments)
  99. }
  100. // GetAttachmentByUUID returns attachment by given UUID.
  101. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  102. return getAttachmentByUUID(x, uuid)
  103. }
  104. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  105. attachments := make([]*Attachment, 0, 5)
  106. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  107. }
  108. // GetAttachmentsByIssueID returns all attachments of an issue.
  109. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  110. return getAttachmentsByIssueID(x, issueID)
  111. }
  112. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  113. attachments := make([]*Attachment, 0, 5)
  114. return attachments, e.Where("comment_id=?", commentID).Find(&attachments)
  115. }
  116. // GetAttachmentsByCommentID returns all attachments of a comment.
  117. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  118. return getAttachmentsByCommentID(x, commentID)
  119. }
  120. func getAttachmentsByReleaseID(e Engine, releaseID int64) ([]*Attachment, error) {
  121. attachments := make([]*Attachment, 0, 10)
  122. return attachments, e.Where("release_id = ?", releaseID).Find(&attachments)
  123. }
  124. // GetAttachmentsByReleaseID returns all attachments of a release.
  125. func GetAttachmentsByReleaseID(releaseID int64) ([]*Attachment, error) {
  126. return getAttachmentsByReleaseID(x, releaseID)
  127. }
  128. // DeleteAttachment deletes the given attachment and optionally the associated file.
  129. func DeleteAttachment(a *Attachment, remove bool) error {
  130. _, err := DeleteAttachments([]*Attachment{a}, remove)
  131. return err
  132. }
  133. // DeleteAttachments deletes the given attachments and optionally the associated files.
  134. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  135. for i, a := range attachments {
  136. if remove {
  137. if err := os.Remove(a.LocalPath()); err != nil {
  138. return i, err
  139. }
  140. }
  141. if _, err := x.Delete(a); err != nil {
  142. return i, err
  143. }
  144. }
  145. return len(attachments), nil
  146. }
  147. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  148. func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
  149. attachments, err := GetAttachmentsByIssueID(issueID)
  150. if err != nil {
  151. return 0, err
  152. }
  153. return DeleteAttachments(attachments, remove)
  154. }
  155. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  156. func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
  157. attachments, err := GetAttachmentsByCommentID(commentID)
  158. if err != nil {
  159. return 0, err
  160. }
  161. return DeleteAttachments(attachments, remove)
  162. }