1
0

attachment.go 5.4 KB

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