feat(blogo,gitea): GetFileReader method
This commit is contained in:
@@ -110,19 +110,41 @@ func (c *client) GetSingleCommit(user, repo, commitID string) (*commit, *http.Re
|
||||
return commit, res, err
|
||||
}
|
||||
|
||||
func (c *client) get(path string) (body []byte, res *http.Response, err error) {
|
||||
res, err = c.http.Get(c.endpoint + path)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Join(errors.New("failed to request"), err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
data, err := statusCodeToErr(res)
|
||||
if err != nil {
|
||||
return data, res, err
|
||||
func (c *client) GetFileReader(
|
||||
owner, repo, ref, filepath string,
|
||||
resolveLFS ...bool,
|
||||
) (io.ReadCloser, *http.Response, error) {
|
||||
if len(resolveLFS) != 0 && resolveLFS[0] {
|
||||
return c.getResponseReader(
|
||||
fmt.Sprintf(
|
||||
"/repos/%s/%s/media/%s?ref=%s",
|
||||
owner,
|
||||
repo,
|
||||
filepath,
|
||||
url.QueryEscape(ref),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
data, err = io.ReadAll(res.Body)
|
||||
return c.getResponseReader(
|
||||
fmt.Sprintf(
|
||||
"/repos/%s/%s/raw/%s?ref=%s",
|
||||
owner,
|
||||
repo,
|
||||
filepath,
|
||||
url.QueryEscape(ref),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *client) get(path string) ([]byte, *http.Response, error) {
|
||||
body, res, err := c.getResponseReader(path)
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
defer body.Close()
|
||||
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
@@ -130,6 +152,20 @@ func (c *client) get(path string) (body []byte, res *http.Response, err error) {
|
||||
return data, res, err
|
||||
}
|
||||
|
||||
func (c *client) getResponseReader(path string) (io.ReadCloser, *http.Response, error) {
|
||||
res, err := c.http.Get(c.endpoint + path)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Join(errors.New("failed to request"), err)
|
||||
}
|
||||
|
||||
data, err := statusCodeToErr(res)
|
||||
if err != nil {
|
||||
return io.NopCloser(bytes.NewReader(data)), res, err
|
||||
}
|
||||
|
||||
return res.Body, res, err
|
||||
}
|
||||
|
||||
func statusCodeToErr(resp *http.Response) (body []byte, err error) {
|
||||
if resp.StatusCode/100 == 2 {
|
||||
return nil, nil
|
||||
|
||||
Reference in New Issue
Block a user