attachment.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/setting"
  10. "io"
  11. "mime/multipart"
  12. "os"
  13. "path"
  14. "time"
  15. "github.com/go-xorm/xorm"
  16. gouuid "github.com/satori/go.uuid"
  17. )
  18. // Attachment represent a attachment of issue/comment/release.
  19. type Attachment struct {
  20. ID int64
  21. UUID string `xorm:"uuid UNIQUE"`
  22. IssueID int64 `xorm:"INDEX"`
  23. CommentID int64
  24. ReleaseID int64 `xorm:"INDEX"`
  25. Name string
  26. Created time.Time `xorm:"-" json:"-"`
  27. CreatedUnix int64
  28. }
  29. // BeforeInsert will be invoked by XORM before inserting a record
  30. func (a *Attachment) BeforeInsert() {
  31. a.CreatedUnix = time.Now().Unix()
  32. }
  33. // AfterSet is invoked from XORM after setting the values of all fields of this object.
  34. func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
  35. switch colName {
  36. case "created_unix":
  37. a.Created = time.Unix(a.CreatedUnix, 0).Local()
  38. }
  39. }
  40. // AttachmentLocalPath returns where attachment is stored in local file 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, 5)
  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. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  104. attachments := make([]*Attachment, 0, 5)
  105. return attachments, e.Where("comment_id=?", commentID).Find(&attachments)
  106. }
  107. // GetAttachmentsByCommentID returns all attachments of a comment.
  108. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  109. return getAttachmentsByCommentID(x, commentID)
  110. }
  111. func getAttachmentsByReleaseID(e Engine, releaseID int64) ([]*Attachment, error) {
  112. attachments := make([]*Attachment, 0, 10)
  113. return attachments, e.Where("release_id = ?", releaseID).Find(&attachments)
  114. }
  115. // GetAttachmentsByReleaseID returns all attachments of a release.
  116. func GetAttachmentsByReleaseID(releaseID int64) ([]*Attachment, error) {
  117. return getAttachmentsByReleaseID(x, releaseID)
  118. }
  119. // DeleteAttachment deletes the given attachment and optionally the associated file.
  120. func DeleteAttachment(a *Attachment, remove bool) error {
  121. _, err := DeleteAttachments([]*Attachment{a}, remove)
  122. return err
  123. }
  124. // DeleteAttachments deletes the given attachments and optionally the associated files.
  125. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  126. for i, a := range attachments {
  127. if remove {
  128. if err := os.Remove(a.LocalPath()); err != nil {
  129. return i, err
  130. }
  131. }
  132. if _, err := x.Delete(a); err != nil {
  133. return i, err
  134. }
  135. }
  136. return len(attachments), nil
  137. }
  138. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  139. func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
  140. attachments, err := GetAttachmentsByIssueID(issueID)
  141. if err != nil {
  142. return 0, err
  143. }
  144. return DeleteAttachments(attachments, remove)
  145. }
  146. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  147. func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
  148. attachments, err := GetAttachmentsByCommentID(commentID)
  149. if err != nil {
  150. return 0, err
  151. }
  152. return DeleteAttachments(attachments, remove)
  153. }