attachment.go 5.4 KB

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