attachment.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2017 The Gogs 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. "github.com/gogs/gogs/pkg/setting"
  15. )
  16. // Attachment represent a attachment of issue/comment/release.
  17. type Attachment struct {
  18. ID int64
  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:"-" json:"-"`
  25. CreatedUnix int64
  26. }
  27. func (a *Attachment) BeforeInsert() {
  28. a.CreatedUnix = time.Now().Unix()
  29. }
  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. // AttachmentLocalPath returns where attachment is stored in local file system based on given UUID.
  37. func AttachmentLocalPath(uuid string) string {
  38. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  39. }
  40. // LocalPath returns where attachment is stored in local file system.
  41. func (attach *Attachment) LocalPath() string {
  42. return AttachmentLocalPath(attach.UUID)
  43. }
  44. // NewAttachment creates a new attachment object.
  45. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  46. attach := &Attachment{
  47. UUID: gouuid.NewV4().String(),
  48. Name: name,
  49. }
  50. localPath := attach.LocalPath()
  51. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  52. return nil, fmt.Errorf("MkdirAll: %v", err)
  53. }
  54. fw, err := os.Create(localPath)
  55. if err != nil {
  56. return nil, fmt.Errorf("Create: %v", err)
  57. }
  58. defer fw.Close()
  59. if _, err = fw.Write(buf); err != nil {
  60. return nil, fmt.Errorf("Write: %v", err)
  61. } else if _, err = io.Copy(fw, file); err != nil {
  62. return nil, fmt.Errorf("Copy: %v", err)
  63. }
  64. if _, err := x.Insert(attach); err != nil {
  65. return nil, err
  66. }
  67. return attach, nil
  68. }
  69. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  70. attach := &Attachment{UUID: uuid}
  71. has, err := x.Get(attach)
  72. if err != nil {
  73. return nil, err
  74. } else if !has {
  75. return nil, ErrAttachmentNotExist{0, uuid}
  76. }
  77. return attach, nil
  78. }
  79. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  80. if len(uuids) == 0 {
  81. return []*Attachment{}, nil
  82. }
  83. // Silently drop invalid uuids.
  84. attachments := make([]*Attachment, 0, len(uuids))
  85. return attachments, e.In("uuid", uuids).Find(&attachments)
  86. }
  87. // GetAttachmentByUUID returns attachment by given UUID.
  88. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  89. return getAttachmentByUUID(x, uuid)
  90. }
  91. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  92. attachments := make([]*Attachment, 0, 5)
  93. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  94. }
  95. // GetAttachmentsByIssueID returns all attachments of an issue.
  96. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  97. return getAttachmentsByIssueID(x, issueID)
  98. }
  99. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  100. attachments := make([]*Attachment, 0, 5)
  101. return attachments, e.Where("comment_id=?", commentID).Find(&attachments)
  102. }
  103. // GetAttachmentsByCommentID returns all attachments of a comment.
  104. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  105. return getAttachmentsByCommentID(x, commentID)
  106. }
  107. func getAttachmentsByReleaseID(e Engine, releaseID int64) ([]*Attachment, error) {
  108. attachments := make([]*Attachment, 0, 10)
  109. return attachments, e.Where("release_id = ?", releaseID).Find(&attachments)
  110. }
  111. // GetAttachmentsByReleaseID returns all attachments of a release.
  112. func GetAttachmentsByReleaseID(releaseID int64) ([]*Attachment, error) {
  113. return getAttachmentsByReleaseID(x, releaseID)
  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. }