2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea // import "code.gitea.io/sdk/gitea"
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Attachment a generic attachment
|
|
|
|
type Attachment struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
DownloadCount int64 `json:"download_count"`
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
DownloadURL string `json:"browser_download_url"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// ListReleaseAttachmentsOptions options for listing release's attachments
|
|
|
|
type ListReleaseAttachmentsOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// ListReleaseAttachments list release's attachments
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListReleaseAttachments(user, repo string, release int64, opt ListReleaseAttachmentsOptions) ([]*Attachment, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
attachments := make([]*Attachment, 0, opt.PageSize)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET",
|
2020-05-27 09:48:09 -04:00
|
|
|
fmt.Sprintf("/repos/%s/%s/releases/%d/assets?%s", user, repo, release, opt.getURLQuery().Encode()),
|
2019-04-25 13:06:53 -04:00
|
|
|
nil, nil, &attachments)
|
2020-09-15 22:01:41 -04:00
|
|
|
return attachments, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReleaseAttachment returns the requested attachment
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
a := new(Attachment)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET",
|
2019-04-25 13:06:53 -04:00
|
|
|
fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id),
|
|
|
|
nil, nil, &a)
|
2020-09-15 22:01:41 -04:00
|
|
|
return a, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateReleaseAttachment creates an attachment for the given release
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file io.Reader, filename string) (*Attachment, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
// Write file to body
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
part, err := writer.CreateFormFile("attachment", filename)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = io.Copy(part, file); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
if err = writer.Close(); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send request
|
|
|
|
attachment := new(Attachment)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
2019-04-25 13:06:53 -04:00
|
|
|
fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release),
|
|
|
|
http.Header{"Content-Type": {writer.FormDataContentType()}}, body, &attachment)
|
2020-09-15 22:01:41 -04:00
|
|
|
return attachment, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2019-10-13 11:13:33 -04:00
|
|
|
// EditAttachmentOptions options for editing attachments
|
|
|
|
type EditAttachmentOptions struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// EditReleaseAttachment updates the given attachment with the given options
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachment int64, form EditAttachmentOptions) (*Attachment, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&form)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
attach := new(Attachment)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, attachment), jsonHeader, bytes.NewReader(body), attach)
|
|
|
|
return attach, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteReleaseAttachment deletes the given attachment including the uploaded file
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteReleaseAttachment(user, repo string, release int64, id int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|