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