Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

attachment.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/util"
  13. api "code.gitea.io/sdk/gitea"
  14. "github.com/go-xorm/xorm"
  15. gouuid "github.com/satori/go.uuid"
  16. )
  17. // Attachment represent a attachment of issue/comment/release.
  18. type Attachment struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. UUID string `xorm:"uuid UNIQUE"`
  21. IssueID int64 `xorm:"INDEX"`
  22. ReleaseID int64 `xorm:"INDEX"`
  23. CommentID int64
  24. Name string
  25. DownloadCount int64 `xorm:"DEFAULT 0"`
  26. CreatedUnix util.TimeStamp `xorm:"created"`
  27. }
  28. // IncreaseDownloadCount is update download count + 1
  29. func (a *Attachment) IncreaseDownloadCount() error {
  30. // Update download count.
  31. if _, err := x.Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil {
  32. return fmt.Errorf("increase attachment count: %v", err)
  33. }
  34. return nil
  35. }
  36. // APIFormat converts models.Attachment to api.Attachment
  37. func (a *Attachment) APIFormat() *api.Attachment {
  38. size, _ := a.Size()
  39. return &api.Attachment{
  40. ID: a.ID,
  41. Name: a.Name,
  42. Created: a.CreatedUnix.AsTime(),
  43. DownloadCount: a.DownloadCount,
  44. Size: size,
  45. UUID: a.UUID,
  46. DownloadURL: a.DownloadURL(),
  47. }
  48. }
  49. // AttachmentLocalPath returns where attachment is stored in local file
  50. // system based on given UUID.
  51. func AttachmentLocalPath(uuid string) string {
  52. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  53. }
  54. // LocalPath returns where attachment is stored in local file system.
  55. func (a *Attachment) LocalPath() string {
  56. return AttachmentLocalPath(a.UUID)
  57. }
  58. // Size returns the file's size of the attachment
  59. func (a *Attachment) Size() (int64, error) {
  60. fi, err := os.Stat(a.LocalPath())
  61. if err != nil {
  62. return 0, err
  63. }
  64. return fi.Size(), nil
  65. }
  66. // DownloadURL returns the download url of the attached file
  67. func (a *Attachment) DownloadURL() string {
  68. return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
  69. }
  70. // NewAttachment creates a new attachment object.
  71. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  72. attach := &Attachment{
  73. UUID: gouuid.NewV4().String(),
  74. Name: name,
  75. }
  76. localPath := attach.LocalPath()
  77. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  78. return nil, fmt.Errorf("MkdirAll: %v", err)
  79. }
  80. fw, err := os.Create(localPath)
  81. if err != nil {
  82. return nil, fmt.Errorf("Create: %v", err)
  83. }
  84. defer fw.Close()
  85. if _, err = fw.Write(buf); err != nil {
  86. return nil, fmt.Errorf("Write: %v", err)
  87. } else if _, err = io.Copy(fw, file); err != nil {
  88. return nil, fmt.Errorf("Copy: %v", err)
  89. }
  90. if _, err := x.Insert(attach); err != nil {
  91. return nil, err
  92. }
  93. return attach, nil
  94. }
  95. // GetAttachmentByID returns attachment by given id
  96. func GetAttachmentByID(id int64) (*Attachment, error) {
  97. return getAttachmentByID(x, id)
  98. }
  99. func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
  100. attach := &Attachment{ID: id}
  101. if has, err := e.Get(attach); err != nil {
  102. return nil, err
  103. } else if !has {
  104. return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
  105. }
  106. return attach, nil
  107. }
  108. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  109. attach := &Attachment{UUID: uuid}
  110. has, err := e.Get(attach)
  111. if err != nil {
  112. return nil, err
  113. } else if !has {
  114. return nil, ErrAttachmentNotExist{0, uuid}
  115. }
  116. return attach, nil
  117. }
  118. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  119. if len(uuids) == 0 {
  120. return []*Attachment{}, nil
  121. }
  122. // Silently drop invalid uuids.
  123. attachments := make([]*Attachment, 0, len(uuids))
  124. return attachments, e.In("uuid", uuids).Find(&attachments)
  125. }
  126. // GetAttachmentByUUID returns attachment by given UUID.
  127. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  128. return getAttachmentByUUID(x, uuid)
  129. }
  130. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  131. attachments := make([]*Attachment, 0, 10)
  132. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  133. }
  134. // GetAttachmentsByIssueID returns all attachments of an issue.
  135. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  136. return getAttachmentsByIssueID(x, issueID)
  137. }
  138. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  139. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  140. return getAttachmentsByCommentID(x, commentID)
  141. }
  142. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  143. attachments := make([]*Attachment, 0, 10)
  144. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  145. }
  146. // DeleteAttachment deletes the given attachment and optionally the associated file.
  147. func DeleteAttachment(a *Attachment, remove bool) error {
  148. _, err := DeleteAttachments([]*Attachment{a}, remove)
  149. return err
  150. }
  151. // DeleteAttachments deletes the given attachments and optionally the associated files.
  152. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  153. if len(attachments) == 0 {
  154. return 0, nil
  155. }
  156. var ids = make([]int64, 0, len(attachments))
  157. for _, a := range attachments {
  158. ids = append(ids, a.ID)
  159. }
  160. cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
  161. if err != nil {
  162. return 0, err
  163. }
  164. if remove {
  165. for i, a := range attachments {
  166. if err := os.Remove(a.LocalPath()); err != nil {
  167. return i, err
  168. }
  169. }
  170. }
  171. return int(cnt), nil
  172. }
  173. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  174. func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
  175. attachments, err := GetAttachmentsByIssueID(issueID)
  176. if err != nil {
  177. return 0, err
  178. }
  179. return DeleteAttachments(attachments, remove)
  180. }
  181. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  182. func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
  183. attachments, err := GetAttachmentsByCommentID(commentID)
  184. if err != nil {
  185. return 0, err
  186. }
  187. return DeleteAttachments(attachments, remove)
  188. }
  189. // UpdateAttachment updates the given attachment in database
  190. func UpdateAttachment(atta *Attachment) error {
  191. return updateAttachment(x, atta)
  192. }
  193. func updateAttachment(e Engine, atta *Attachment) error {
  194. var sess *xorm.Session
  195. if atta.ID != 0 && atta.UUID == "" {
  196. sess = e.ID(atta.ID)
  197. } else {
  198. // Use uuid only if id is not set and uuid is set
  199. sess = e.Where("uuid = ?", atta.UUID)
  200. }
  201. _, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta)
  202. return err
  203. }