Files

39 lines
1.1 KiB
Go
Raw Permalink Normal View History

package ocapi
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
)
// RawFile reads a file the server can see, for previews. content is the
// server's Content-Type; the body is bounded to 32 MiB. Paths outside the
// session's workspace are allowed: the tool call already read the file, and
// the preview shows the same one.
func (c *Client) RawFile(ctx context.Context, path, directory string) ([]byte, string, error) {
q := url.Values{}
q.Set("path", path)
q.Set("allowOutsideWorkspace", "true")
req, err := c.newRequest(ctx, http.MethodGet, "/api/fs/raw", requestOptions{directory: directory, query: q}, nil)
if err != nil {
return nil, "", err
}
hc := *c.http
hc.Timeout = 0
res, err := hc.Do(req)
if err != nil {
return nil, "", err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
raw, _ := io.ReadAll(io.LimitReader(res.Body, 1<<14))
return nil, "", &APIError{Status: res.StatusCode, Raw: string(raw), Message: fmt.Sprintf("HTTP %d", res.StatusCode)}
}
data, err := io.ReadAll(io.LimitReader(res.Body, 32<<20))
if err != nil {
return nil, "", err
}
return data, res.Header.Get("Content-Type"), nil
}