You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.0 KiB

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