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.1 kB

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