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
|
return commit, res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) get(path string) (body []byte, res *http.Response, err error) {
|
func (c *client) GetFileReader(
|
||||||
res, err = c.http.Get(c.endpoint + path)
|
owner, repo, ref, filepath string,
|
||||||
if err != nil {
|
resolveLFS ...bool,
|
||||||
return nil, nil, errors.Join(errors.New("failed to request"), err)
|
) (io.ReadCloser, *http.Response, error) {
|
||||||
}
|
if len(resolveLFS) != 0 && resolveLFS[0] {
|
||||||
defer res.Body.Close()
|
return c.getResponseReader(
|
||||||
|
fmt.Sprintf(
|
||||||
data, err := statusCodeToErr(res)
|
"/repos/%s/%s/media/%s?ref=%s",
|
||||||
if err != nil {
|
owner,
|
||||||
return data, res, err
|
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 {
|
if err != nil {
|
||||||
return nil, res, err
|
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
|
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) {
|
func statusCodeToErr(resp *http.Response) (body []byte, err error) {
|
||||||
if resp.StatusCode/100 == 2 {
|
if resp.StatusCode/100 == 2 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user