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.

182 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. gouuid "github.com/satori/go.uuid"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/util"
  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. CreatedUnix util.TimeStamp `xorm:"created"`
  25. }
  26. // IncreaseDownloadCount is update download count + 1
  27. func (a *Attachment) IncreaseDownloadCount() error {
  28. // Update download count.
  29. if _, err := x.Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil {
  30. return fmt.Errorf("increase attachment count: %v", err)
  31. }
  32. return nil
  33. }
  34. // AttachmentLocalPath returns where attachment is stored in local file
  35. // system based on given UUID.
  36. func AttachmentLocalPath(uuid string) string {
  37. return path.Join(setting.AttachmentPath, 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, fmt.Errorf("MkdirAll: %v", err)
  52. }
  53. fw, err := os.Create(localPath)
  54. if err != nil {
  55. return nil, fmt.Errorf("Create: %v", err)
  56. }
  57. defer fw.Close()
  58. if _, err = fw.Write(buf); err != nil {
  59. return nil, fmt.Errorf("Write: %v", err)
  60. } else if _, err = io.Copy(fw, file); err != nil {
  61. return nil, fmt.Errorf("Copy: %v", err)
  62. }
  63. if _, err := x.Insert(attach); err != nil {
  64. return nil, err
  65. }
  66. return attach, nil
  67. }
  68. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  69. attach := &Attachment{UUID: uuid}
  70. has, err := e.Get(attach)
  71. if err != nil {
  72. return nil, err
  73. } else if !has {
  74. return nil, ErrAttachmentNotExist{0, uuid}
  75. }
  76. return attach, nil
  77. }
  78. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  79. if len(uuids) == 0 {
  80. return []*Attachment{}, nil
  81. }
  82. // Silently drop invalid uuids.
  83. attachments := make([]*Attachment, 0, len(uuids))
  84. return attachments, e.In("uuid", uuids).Find(&attachments)
  85. }
  86. // GetAttachmentByUUID returns attachment by given UUID.
  87. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  88. return getAttachmentByUUID(x, uuid)
  89. }
  90. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  91. attachments := make([]*Attachment, 0, 10)
  92. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  93. }
  94. // GetAttachmentsByIssueID returns all attachments of an issue.
  95. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  96. return getAttachmentsByIssueID(x, issueID)
  97. }
  98. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  99. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  100. return getAttachmentsByCommentID(x, commentID)
  101. }
  102. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  103. attachments := make([]*Attachment, 0, 10)
  104. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  105. }
  106. // DeleteAttachment deletes the given attachment and optionally the associated file.
  107. func DeleteAttachment(a *Attachment, remove bool) error {
  108. _, err := DeleteAttachments([]*Attachment{a}, remove)
  109. return err
  110. }
  111. // DeleteAttachments deletes the given attachments and optionally the associated files.
  112. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  113. if len(attachments) == 0 {
  114. return 0, nil
  115. }
  116. var ids = make([]int64, 0, len(attachments))
  117. for _, a := range attachments {
  118. ids = append(ids, a.ID)
  119. }
  120. cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
  121. if err != nil {
  122. return 0, err
  123. }
  124. if remove {
  125. for i, a := range attachments {
  126. if err := os.Remove(a.LocalPath()); err != nil {
  127. return i, err
  128. }
  129. }
  130. }
  131. return int(cnt), 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. }