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.

260 lines
7.2 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. // GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName.
  129. func GetAttachmentByReleaseIDFileName(releaseID int64, fileName string) (*Attachment, error) {
  130. return getAttachmentByReleaseIDFileName(x, releaseID, fileName)
  131. }
  132. func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
  133. attachments := make([]*Attachment, 0, 10)
  134. return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
  135. }
  136. // GetAttachmentsByIssueID returns all attachments of an issue.
  137. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  138. return getAttachmentsByIssueID(x, issueID)
  139. }
  140. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  141. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  142. return getAttachmentsByCommentID(x, commentID)
  143. }
  144. func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
  145. attachments := make([]*Attachment, 0, 10)
  146. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  147. }
  148. // getAttachmentByReleaseIDFileName return a file based on the the following infos:
  149. func getAttachmentByReleaseIDFileName(e Engine, releaseID int64, fileName string) (*Attachment, error) {
  150. attach := &Attachment{ReleaseID: releaseID, Name: fileName}
  151. has, err := e.Get(attach)
  152. if err != nil {
  153. return nil, err
  154. } else if !has {
  155. return nil, err
  156. }
  157. return attach, nil
  158. }
  159. // DeleteAttachment deletes the given attachment and optionally the associated file.
  160. func DeleteAttachment(a *Attachment, remove bool) error {
  161. _, err := DeleteAttachments([]*Attachment{a}, remove)
  162. return err
  163. }
  164. // DeleteAttachments deletes the given attachments and optionally the associated files.
  165. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  166. if len(attachments) == 0 {
  167. return 0, nil
  168. }
  169. var ids = make([]int64, 0, len(attachments))
  170. for _, a := range attachments {
  171. ids = append(ids, a.ID)
  172. }
  173. cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
  174. if err != nil {
  175. return 0, err
  176. }
  177. if remove {
  178. for i, a := range attachments {
  179. if err := os.Remove(a.LocalPath()); err != nil {
  180. return i, err
  181. }
  182. }
  183. }
  184. return int(cnt), nil
  185. }
  186. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  187. func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
  188. attachments, err := GetAttachmentsByIssueID(issueID)
  189. if err != nil {
  190. return 0, err
  191. }
  192. return DeleteAttachments(attachments, remove)
  193. }
  194. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  195. func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
  196. attachments, err := GetAttachmentsByCommentID(commentID)
  197. if err != nil {
  198. return 0, err
  199. }
  200. return DeleteAttachments(attachments, remove)
  201. }
  202. // UpdateAttachment updates the given attachment in database
  203. func UpdateAttachment(atta *Attachment) error {
  204. return updateAttachment(x, atta)
  205. }
  206. func updateAttachment(e Engine, atta *Attachment) error {
  207. var sess *xorm.Session
  208. if atta.ID != 0 && atta.UUID == "" {
  209. sess = e.ID(atta.ID)
  210. } else {
  211. // Use uuid only if id is not set and uuid is set
  212. sess = e.Where("uuid = ?", atta.UUID)
  213. }
  214. _, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta)
  215. return err
  216. }