Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

attachment.go 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 gitea // import "code.gitea.io/sdk/gitea"
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "io"
  10. "mime/multipart"
  11. "net/http"
  12. "time"
  13. )
  14. // Attachment a generic attachment
  15. // swagger:model
  16. type Attachment struct {
  17. ID int64 `json:"id"`
  18. Name string `json:"name"`
  19. Size int64 `json:"size"`
  20. DownloadCount int64 `json:"download_count"`
  21. // swagger:strfmt date-time
  22. Created time.Time `json:"created_at"`
  23. UUID string `json:"uuid"`
  24. DownloadURL string `json:"browser_download_url"`
  25. }
  26. // ListReleaseAttachments list release's attachments
  27. func (c *Client) ListReleaseAttachments(user, repo string, release int64) ([]*Attachment, error) {
  28. attachments := make([]*Attachment, 0, 10)
  29. err := c.getParsedResponse("GET",
  30. fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release),
  31. nil, nil, &attachments)
  32. return attachments, err
  33. }
  34. // ListReleaseAttachments list release's attachments
  35. func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, error) {
  36. a := new(Attachment)
  37. err := c.getParsedResponse("GET",
  38. fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id),
  39. nil, nil, &a)
  40. return a, err
  41. }
  42. // CreateReleaseAttachment creates an attachment for the given release
  43. func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file io.Reader, filename string) (*Attachment, error) {
  44. // Write file to body
  45. body := new(bytes.Buffer)
  46. writer := multipart.NewWriter(body)
  47. part, err := writer.CreateFormFile("attachment", filename)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if _, err = io.Copy(part, file); err != nil {
  52. return nil, err
  53. }
  54. if err = writer.Close(); err != nil {
  55. return nil, err
  56. }
  57. // Send request
  58. attachment := new(Attachment)
  59. err = c.getParsedResponse("POST",
  60. fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release),
  61. http.Header{"Content-Type": {writer.FormDataContentType()}}, body, &attachment)
  62. return attachment, err
  63. }
  64. // EditReleaseAttachment updates the given attachment with the given options
  65. func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachment int64, form EditAttachmentOptions) (*Attachment, error) {
  66. body, err := json.Marshal(&form)
  67. if err != nil {
  68. return nil, err
  69. }
  70. attach := new(Attachment)
  71. return attach, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, attachment), jsonHeader, bytes.NewReader(body), attach)
  72. }
  73. // DeleteReleaseAttachment deletes the given attachment including the uploaded file
  74. func (c *Client) DeleteReleaseAttachment(user, repo string, release int64, id int64) error {
  75. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil)
  76. return err
  77. }
  78. // EditAttachmentOptions options for editing attachments
  79. // swagger:model
  80. type EditAttachmentOptions struct {
  81. Name string `json:"name"`
  82. }