package gitea import ( "bytes" "errors" "fmt" "io" "io/fs" "net/http" "net/url" "strings" "sync" gclient "code.gitea.io/sdk/gitea" "github.com/spf13/viper" ) type Client struct { serverURL string token string giteapages string gc *gclient.Client } func NewClient(serverURL, token, giteapages string) (*Client, error) { if giteapages == "" { giteapages = "gitea-pages" } gc, err := gclient.NewClient(serverURL, gclient.SetToken(token), gclient.SetGiteaVersion("")) if err != nil { return nil, err } return &Client{ serverURL: serverURL, token: token, gc: gc, giteapages: giteapages, }, nil } func (c *Client) Open(name, ref string) (fs.File, error) { if ref == "" { ref = c.giteapages } owner, repo, filepath, err := splitName(name) if err != nil { return nil, err } if !c.allowsPages(owner, repo) { return nil, fs.ErrNotExist } if err := c.readConfig(owner, repo); err != nil { return nil, err } if !validRefs(ref) { return nil, fs.ErrNotExist } res, err := c.getRawFileOrLFS(owner, repo, filepath, ref) if err != nil { return nil, err } if strings.HasSuffix(filepath, ".md") { res, err = handleMD(res) if err != nil { return nil, err } } return &openFile{ content: res, name: filepath, }, nil } func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, error) { var ( giteaURL string err error ) // TODO: make pr for go-sdk // gitea sdk doesn't support "media" type for lfs/non-lfs giteaURL, err = url.JoinPath(c.serverURL+"/api/v1/repos/", owner, repo, "media", filepath) if err != nil { return nil, err } giteaURL += "?ref=" + url.QueryEscape(ref) req, err := http.NewRequest(http.MethodGet, giteaURL, nil) if err != nil { return nil, err } req.Header.Add("Authorization", "token "+c.token) resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } switch resp.StatusCode { case http.StatusNotFound: return nil, fs.ErrNotExist case http.StatusOK: default: return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) } res, err := io.ReadAll(resp.Body) if err != nil { return nil, err } defer resp.Body.Close() return res, nil } var bufPool = sync.Pool{ New: func() any { return new(bytes.Buffer) }, } func handleMD(res []byte) ([]byte, error) { meta, resbody, err := extractFrontMatter(string(res)) if err != nil { return nil, err } resmd, err := markdown([]byte(resbody)) if err != nil { return nil, err } res = append([]byte("\n\n
\n