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.

attachment.go 4.7KB

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. "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. CommentID int64
  22. ReleaseID int64 `xorm:"INDEX"`
  23. Name string
  24. Created time.Time `xorm:"-"`
  25. CreatedUnix int64
  26. }
  27. // BeforeInsert is invoked from XORM before inserting an object of this type.
  28. func (a *Attachment) BeforeInsert() {
  29. a.CreatedUnix = time.Now().Unix()
  30. }
  31. // AfterSet is invoked from XORM after setting the value of a field of
  32. // this object.
  33. func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
  34. switch colName {
  35. case "created_unix":
  36. a.Created = time.Unix(a.CreatedUnix, 0).Local()
  37. }
  38. }
  39. // AttachmentLocalPath returns where attachment is stored in local file
  40. // system based on given UUID.
  41. func AttachmentLocalPath(uuid string) string {
  42. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  43. }
  44. // LocalPath returns where attachment is stored in local file system.
  45. func (a *Attachment) LocalPath() string {
  46. return AttachmentLocalPath(a.UUID)
  47. }
  48. // NewAttachment creates a new attachment object.
  49. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  50. attach := &Attachment{
  51. UUID: gouuid.NewV4().String(),
  52. Name: name,
  53. }
  54. localPath := attach.LocalPath()
  55. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  56. return nil, fmt.Errorf("MkdirAll: %v", err)
  57. }
  58. fw, err := os.Create(localPath)
  59. if err != nil {
  60. return nil, fmt.Errorf("Create: %v", err)
  61. }
  62. defer fw.Close()
  63. if _, err = fw.Write(buf); err != nil {
  64. return nil, fmt.Errorf("Write: %v", err)
  65. } else if _, err = io.Copy(fw, file); err != nil {
  66. return nil, fmt.Errorf("Copy: %v", err)
  67. }
  68. if _, err := x.Insert(attach); err != nil {
  69. return nil, err
  70. }
  71. return attach, nil
  72. }
  73. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  74. attach := &Attachment{UUID: uuid}
  75. has, err := x.Get(attach)
  76. if err != nil {
  77. return nil, err
  78. } else if !has {
  79. return nil, ErrAttachmentNotExist{0, uuid}
  80. }
  81. return attach, nil
  82. }
  83. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  84. if len(uuids) == 0 {
  85. return []*Attachment{}, nil
  86. }
  87. // Silently drop invalid uuids.
  88. attachments := make([]*Attachment, 0, len(uuids))
  89. return attachments, e.In("uuid", uuids).Find(&attachments)
  90. }
  91. // GetAttachmentByUUID returns attachment by given UUID.
  92. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  93. return getAttachmentByUUID(x, uuid)
  94. }
  95. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  96. attachments := make([]*Attachment, 0, 10)
  97. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  98. }
  99. // GetAttachmentsByIssueID returns all attachments of an issue.
  100. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  101. return getAttachmentsByIssueID(x, issueID)
  102. }
  103. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  104. func GetAttachmentsByCommentID(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. }