This repository has been archived on 2024-12-14. You can view files and clone it, but cannot push or open issues or pull requests.
caddy-gitea/pkg/gitea/gitea.go

317 lines
6.2 KiB
Go
Raw Permalink Normal View History

2022-10-30 22:46:54 +01:00
package gitea
import (
"bytes"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"strings"
"sync"
2022-11-01 21:50:49 +01:00
gclient "code.gitea.io/sdk/gitea"
2022-10-30 22:46:54 +01:00
"github.com/spf13/viper"
)
type Client struct {
serverURL string
token string
giteapages string
giteapagesAllowAll string
gc *gclient.Client
2022-10-30 22:46:54 +01:00
}
2024-12-14 22:10:56 +01:00
type dirEntry struct {
name string
isDir bool
content []byte
}
func (d *dirEntry) Name() string { return d.name }
func (d *dirEntry) IsDir() bool { return d.isDir }
func (d *dirEntry) Type() fs.FileMode { return fs.ModePerm }
func (d *dirEntry) Info() (fs.FileInfo, error) { return nil, fs.ErrNotExist }
type openDir struct {
entries []fs.DirEntry
name string
pos int
}
func (d *openDir) Read([]byte) (int, error) {
return 0, fmt.Errorf("cannot Read() a directory")
}
func (d *openDir) Close() error {
return nil
}
func (d *openDir) Stat() (fs.FileInfo, error) {
return nil, fs.ErrNotExist
}
func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) {
n := len(d.entries) - d.pos
if count > 0 && n > count {
n = count
}
if n == 0 {
if count <= 0 {
return nil, nil
}
return nil, io.EOF
}
entries := d.entries[d.pos : d.pos+n]
d.pos += n
return entries, nil
}
func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client, error) {
2022-11-01 21:22:12 +01:00
if giteapages == "" {
giteapages = "gitea-pages"
}
if giteapagesAllowAll == "" {
giteapagesAllowAll = "gitea-pages-allowall"
}
2022-11-01 21:50:49 +01:00
gc, err := gclient.NewClient(serverURL, gclient.SetToken(token), gclient.SetGiteaVersion(""))
if err != nil {
return nil, err
}
2022-10-30 22:46:54 +01:00
return &Client{
serverURL: serverURL,
token: token,
gc: gc,
giteapages: giteapages,
giteapagesAllowAll: giteapagesAllowAll,
2022-11-01 21:50:49 +01:00
}, nil
2022-10-30 22:46:54 +01:00
}
func (c *Client) Open(name, ref string) (fs.File, error) {
owner, repo, filepath := splitName(name)
if repo == "" {
2024-12-14 22:00:50 +01:00
repo = owner + ".fluffy.pw"
filepath = "index.html"
}
2024-12-14 22:00:50 +01:00
isFluffyPagesRepo := strings.HasSuffix(repo, ".fluffy.pw")
2024-12-14 22:00:50 +01:00
limited, allowall := c.allowsPages(owner, repo)
if !limited && !allowall && !isFluffyPagesRepo {
return nil, fs.ErrNotExist
2022-10-30 22:46:54 +01:00
}
hasConfig := true
if err := c.readConfig(owner, repo); err != nil {
2024-12-14 22:00:50 +01:00
if !isFluffyPagesRepo && !allowall {
return nil, err
}
hasConfig = false
2022-10-30 22:46:54 +01:00
}
2024-12-14 22:00:50 +01:00
if !hasConfig && !validRefs(ref, allowall || isFluffyPagesRepo) {
2022-10-30 22:46:54 +01:00
return nil, fs.ErrNotExist
}
2024-12-14 22:10:56 +01:00
entries, err := c.getDirectoryContents(owner, repo, filepath, ref)
if err == nil {
return &openDir{
entries: entries,
name: filepath,
}, nil
}
2022-10-30 22:46:54 +01:00
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
)
2022-11-01 21:50:49 +01:00
// TODO: make pr for go-sdk
// gitea sdk doesn't support "media" type for lfs/non-lfs
2022-10-30 22:46:54 +01:00
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
}
title, _ := meta["title"].(string)
res = append([]byte("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(title)...)
2022-10-30 22:46:54 +01:00
res = append(res, []byte("</h1>")...)
res = append(res, resmd...)
res = append(res, []byte("</body></html>")...)
return res, nil
}
func (c *Client) repoTopics(owner, repo string) ([]string, error) {
2022-11-01 21:50:49 +01:00
repos, _, err := c.gc.ListRepoTopics(owner, repo, gclient.ListRepoTopicsOptions{})
return repos, err
2022-10-30 22:46:54 +01:00
}
func (c *Client) hasRepoBranch(owner, repo, branch string) bool {
b, _, err := c.gc.GetRepoBranch(owner, repo, branch)
if err != nil {
return false
}
return b.Name == branch
}
func (c *Client) allowsPages(owner, repo string) (bool, bool) {
2024-12-14 22:00:50 +01:00
if strings.HasSuffix(repo, ".fluffy.pw") {
return true, true
}
2022-10-30 22:46:54 +01:00
topics, err := c.repoTopics(owner, repo)
if err != nil {
return false, false
}
for _, topic := range topics {
if topic == c.giteapagesAllowAll {
return true, true
}
2022-10-30 22:46:54 +01:00
}
for _, topic := range topics {
2022-11-01 21:22:12 +01:00
if topic == c.giteapages {
return true, false
2022-10-30 22:46:54 +01:00
}
}
return false, false
2022-10-30 22:46:54 +01:00
}
func (c *Client) readConfig(owner, repo string) error {
2022-11-01 21:22:12 +01:00
cfg, err := c.getRawFileOrLFS(owner, repo, c.giteapages+".toml", c.giteapages)
if err != nil {
2022-10-30 22:46:54 +01:00
return err
}
viper.SetConfigType("toml")
return viper.ReadConfig(bytes.NewBuffer(cfg))
2022-10-30 22:46:54 +01:00
}
func splitName(name string) (string, string, string) {
2022-10-30 22:46:54 +01:00
parts := strings.Split(name, "/")
// parts contains: ["owner", "repo", "filepath"]
switch len(parts) {
case 1:
return parts[0], "", ""
case 2:
return parts[0], parts[1], ""
default:
return parts[0], parts[1], strings.Join(parts[2:], "/")
2022-10-30 22:46:54 +01:00
}
}
func validRefs(ref string, allowall bool) bool {
if allowall {
return true
}
2022-10-30 22:46:54 +01:00
validrefs := viper.GetStringSlice("allowedrefs")
for _, r := range validrefs {
if r == ref {
return true
}
if r == "*" {
return true
}
}
return false
}
2024-12-14 22:10:56 +01:00
func (c *Client) getDirectoryContents(owner, repo, dirPath, ref string) ([]fs.DirEntry, error) {
entries, _, err := c.gc.ListContents(owner, repo, ref, dirPath)
if err != nil {
return nil, fs.ErrNotExist
}
var result []fs.DirEntry
for _, entry := range entries {
result = append(result, &dirEntry{
name: entry.Name,
isDir: entry.Type == "dir",
})
}
return result, nil
}