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

attachment.go 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. sess := x.NewSession()
  29. defer sess.Close()
  30. // Update download count.
  31. if _, err := sess.Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil {
  32. return fmt.Errorf("increase attachment count: %v", err)
  33. }
  34. return nil
  35. }
  36. // AttachmentLocalPath returns where attachment is stored in local file
  37. // system based on given UUID.
  38. func AttachmentLocalPath(uuid string) string {
  39. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  40. }
  41. // LocalPath returns where attachment is stored in local file system.
  42. func (a *Attachment) LocalPath() string {
  43. return AttachmentLocalPath(a.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. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  71. attach := &Attachment{UUID: uuid}
  72. has, err := e.Get(attach)
  73. if err != nil {
  74. return nil, err
  75. } else if !has {
  76. return nil, ErrAttachmentNotExist{0, uuid}
  77. }
  78. return attach, nil
  79. }
  80. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  81. if len(uuids) == 0 {
  82. return []*Attachment{}, nil
  83. }
  84. // Silently drop invalid uuids.
  85. attachments := make([]*Attachment, 0, len(uuids))
  86. return attachments, e.In("uuid", uuids).Find(&attachments)
  87. }
  88. // GetAttachmentByUUID returns attachment by given UUID.
  89. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  90. return getAttachmentByUUID(x, uuid)
  91. }
  92. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  93. attachments := make([]*Attachment, 0, 10)
  94. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  95. }
  96. // GetAttachmentsByIssueID returns all attachments of an issue.
  97. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  98. return getAttachmentsByIssueID(x, issueID)
  99. }
  100. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  101. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  102. return getAttachmentsByCommentID(x, commentID)
  103. }
  104. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  105. attachments := make([]*Attachment, 0, 10)
  106. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  107. }
  108. // DeleteAttachment deletes the given attachment and optionally the associated file.
  109. func DeleteAttachment(a *Attachment, remove bool) error {
  110. _, err := DeleteAttachments([]*Attachment{a}, remove)
  111. return err
  112. }
  113. // DeleteAttachments deletes the given attachments and optionally the associated files.
  114. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  115. for i, a := range attachments {
  116. if remove {
  117. if err := os.Remove(a.LocalPath()); err != nil {
  118. return i, err
  119. }
  120. }
  121. if _, err := x.Delete(a); err != nil {
  122. return i, err
  123. }
  124. }
  125. return len(attachments), nil
  126. }
  127. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  128. func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
  129. attachments, err := GetAttachmentsByIssueID(issueID)
  130. if err != nil {
  131. return 0, err
  132. }
  133. return DeleteAttachments(attachments, remove)
  134. }
  135. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  136. func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
  137. attachments, err := GetAttachmentsByCommentID(commentID)
  138. if err != nil {
  139. return 0, err
  140. }
  141. return DeleteAttachments(attachments, remove)
  142. }