attachment.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. func (a *Attachment) BeforeInsert() {
  30. a.CreatedUnix = time.Now().Unix()
  31. }
  32. func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
  33. switch colName {
  34. case "created_unix":
  35. a.Created = time.Unix(a.CreatedUnix, 0).Local()
  36. }
  37. }
  38. // AttachmentLocalPath returns where attachment is stored in local file system based on given UUID.
  39. func AttachmentLocalPath(uuid string) string {
  40. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  41. }
  42. // LocalPath returns where attachment is stored in local file system.
  43. func (attach *Attachment) LocalPath() string {
  44. return AttachmentLocalPath(attach.UUID)
  45. }
  46. // NewAttachment creates a new attachment object.
  47. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  48. attach := &Attachment{
  49. UUID: gouuid.NewV4().String(),
  50. Name: name,
  51. }
  52. localPath := attach.LocalPath()
  53. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  54. return nil, fmt.Errorf("MkdirAll: %v", err)
  55. }
  56. fw, err := os.Create(localPath)
  57. if err != nil {
  58. return nil, fmt.Errorf("Create: %v", err)
  59. }
  60. defer fw.Close()
  61. if _, err = fw.Write(buf); err != nil {
  62. return nil, fmt.Errorf("Write: %v", err)
  63. } else if _, err = io.Copy(fw, file); err != nil {
  64. return nil, fmt.Errorf("Copy: %v", err)
  65. }
  66. if _, err := x.Insert(attach); err != nil {
  67. return nil, err
  68. }
  69. return attach, nil
  70. }
  71. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  72. attach := &Attachment{UUID: uuid}
  73. has, err := x.Get(attach)
  74. if err != nil {
  75. return nil, err
  76. } else if !has {
  77. return nil, ErrAttachmentNotExist{0, uuid}
  78. }
  79. return attach, nil
  80. }
  81. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  82. if len(uuids) == 0 {
  83. return []*Attachment{}, nil
  84. }
  85. // Silently drop invalid uuids.
  86. attachments := make([]*Attachment, 0, len(uuids))
  87. return attachments, e.In("uuid", uuids).Find(&attachments)
  88. }
  89. // GetAttachmentByUUID returns attachment by given UUID.
  90. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  91. return getAttachmentByUUID(x, uuid)
  92. }
  93. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  94. attachments := make([]*Attachment, 0, 5)
  95. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  96. }
  97. // GetAttachmentsByIssueID returns all attachments of an issue.
  98. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  99. return getAttachmentsByIssueID(x, issueID)
  100. }
  101. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  102. attachments := make([]*Attachment, 0, 5)
  103. return attachments, e.Where("comment_id=?", commentID).Find(&attachments)
  104. }
  105. // GetAttachmentsByCommentID returns all attachments of a comment.
  106. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  107. return getAttachmentsByCommentID(x, commentID)
  108. }
  109. func getAttachmentsByReleaseID(e Engine, releaseID int64) ([]*Attachment, error) {
  110. attachments := make([]*Attachment, 0, 10)
  111. return attachments, e.Where("release_id = ?", releaseID).Find(&attachments)
  112. }
  113. // GetAttachmentsByReleaseID returns all attachments of a release.
  114. func GetAttachmentsByReleaseID(releaseID int64) ([]*Attachment, error) {
  115. return getAttachmentsByReleaseID(x, releaseID)
  116. }
  117. // DeleteAttachment deletes the given attachment and optionally the associated file.
  118. func DeleteAttachment(a *Attachment, remove bool) error {
  119. _, err := DeleteAttachments([]*Attachment{a}, remove)
  120. return err
  121. }
  122. // DeleteAttachments deletes the given attachments and optionally the associated files.
  123. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  124. for i, a := range attachments {
  125. if remove {
  126. if err := os.Remove(a.LocalPath()); err != nil {
  127. return i, err
  128. }
  129. }
  130. if _, err := x.Delete(a); err != nil {
  131. return i, err
  132. }
  133. }
  134. return len(attachments), nil
  135. }
  136. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  137. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  138. attachments, err := GetAttachmentsByIssueID(issueId)
  139. if err != nil {
  140. return 0, err
  141. }
  142. return DeleteAttachments(attachments, remove)
  143. }
  144. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  145. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  146. attachments, err := GetAttachmentsByCommentID(commentId)
  147. if err != nil {
  148. return 0, err
  149. }
  150. return DeleteAttachments(attachments, remove)
  151. }