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.

243 lines
6.6 KiB

Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
  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. Size int64 `xorm:"DEFAULT 0"`
  27. CreatedUnix util.TimeStamp `xorm:"created"`
  28. }
  29. // IncreaseDownloadCount is update download count + 1
  30. func (a *Attachment) IncreaseDownloadCount() error {
  31. // Update download count.
  32. if _, err := x.Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil {
  33. return fmt.Errorf("increase attachment count: %v", err)
  34. }
  35. return nil
  36. }
  37. // APIFormat converts models.Attachment to api.Attachment
  38. func (a *Attachment) APIFormat() *api.Attachment {
  39. return &api.Attachment{
  40. ID: a.ID,
  41. Name: a.Name,
  42. Created: a.CreatedUnix.AsTime(),
  43. DownloadCount: a.DownloadCount,
  44. Size: a.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. // DownloadURL returns the download url of the attached file
  59. func (a *Attachment) DownloadURL() string {
  60. return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
  61. }
  62. // NewAttachment creates a new attachment object.
  63. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  64. attach := &Attachment{
  65. UUID: gouuid.NewV4().String(),
  66. Name: name,
  67. }
  68. localPath := attach.LocalPath()
  69. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  70. return nil, fmt.Errorf("MkdirAll: %v", err)
  71. }
  72. fw, err := os.Create(localPath)
  73. if err != nil {
  74. return nil, fmt.Errorf("Create: %v", err)
  75. }
  76. defer fw.Close()
  77. if _, err = fw.Write(buf); err != nil {
  78. return nil, fmt.Errorf("Write: %v", err)
  79. } else if _, err = io.Copy(fw, file); err != nil {
  80. return nil, fmt.Errorf("Copy: %v", err)
  81. }
  82. // Update file size
  83. var fi os.FileInfo
  84. if fi, err = fw.Stat(); err != nil {
  85. return nil, fmt.Errorf("file size: %v", err)
  86. }
  87. attach.Size = fi.Size()
  88. if _, err := x.Insert(attach); err != nil {
  89. return nil, err
  90. }
  91. return attach, nil
  92. }
  93. // GetAttachmentByID returns attachment by given id
  94. func GetAttachmentByID(id int64) (*Attachment, error) {
  95. return getAttachmentByID(x, id)
  96. }
  97. func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
  98. attach := &Attachment{ID: id}
  99. if has, err := e.Get(attach); err != nil {
  100. return nil, err
  101. } else if !has {
  102. return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
  103. }
  104. return attach, nil
  105. }
  106. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  107. attach := &Attachment{UUID: uuid}
  108. has, err := e.Get(attach)
  109. if err != nil {
  110. return nil, err
  111. } else if !has {
  112. return nil, ErrAttachmentNotExist{0, uuid}
  113. }
  114. return attach, nil
  115. }
  116. func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
  117. if len(uuids) == 0 {
  118. return []*Attachment{}, nil
  119. }
  120. // Silently drop invalid uuids.
  121. attachments := make([]*Attachment, 0, len(uuids))
  122. return attachments, e.In("uuid", uuids).Find(&attachments)
  123. }
  124. // GetAttachmentByUUID returns attachment by given UUID.
  125. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  126. return getAttachmentByUUID(x, uuid)
  127. }
  128. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  129. attachments := make([]*Attachment, 0, 10)
  130. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  131. }
  132. // GetAttachmentsByIssueID returns all attachments of an issue.
  133. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  134. return getAttachmentsByIssueID(x, issueID)
  135. }
  136. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  137. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  138. return getAttachmentsByCommentID(x, commentID)
  139. }
  140. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  141. attachments := make([]*Attachment, 0, 10)
  142. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  143. }
  144. // DeleteAttachment deletes the given attachment and optionally the associated file.
  145. func DeleteAttachment(a *Attachment, remove bool) error {
  146. _, err := DeleteAttachments([]*Attachment{a}, remove)
  147. return err
  148. }
  149. // DeleteAttachments deletes the given attachments and optionally the associated files.
  150. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  151. if len(attachments) == 0 {
  152. return 0, nil
  153. }
  154. var ids = make([]int64, 0, len(attachments))
  155. for _, a := range attachments {
  156. ids = append(ids, a.ID)
  157. }
  158. cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
  159. if err != nil {
  160. return 0, err
  161. }
  162. if remove {
  163. for i, a := range attachments {
  164. if err := os.Remove(a.LocalPath()); err != nil {
  165. return i, err
  166. }
  167. }
  168. }
  169. return int(cnt), nil
  170. }
  171. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  172. func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
  173. attachments, err := GetAttachmentsByIssueID(issueID)
  174. if err != nil {
  175. return 0, err
  176. }
  177. return DeleteAttachments(attachments, remove)
  178. }
  179. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  180. func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
  181. attachments, err := GetAttachmentsByCommentID(commentID)
  182. if err != nil {
  183. return 0, err
  184. }
  185. return DeleteAttachments(attachments, remove)
  186. }
  187. // UpdateAttachment updates the given attachment in database
  188. func UpdateAttachment(atta *Attachment) error {
  189. return updateAttachment(x, atta)
  190. }
  191. func updateAttachment(e Engine, atta *Attachment) error {
  192. var sess *xorm.Session
  193. if atta.ID != 0 && atta.UUID == "" {
  194. sess = e.ID(atta.ID)
  195. } else {
  196. // Use uuid only if id is not set and uuid is set
  197. sess = e.Where("uuid = ?", atta.UUID)
  198. }
  199. _, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta)
  200. return err
  201. }