Compare commits
No commits in common. "main" and "v0.0.5" have entirely different histories.
3 changed files with 21 additions and 143 deletions
2
gitea.go
2
gitea.go
|
@ -5,7 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.yiffing.dev/leafus/caddy-gitea/pkg/gitea"
|
"git.yiffing.dev/Leafus/caddy-gitea/pkg/gitea"
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
||||||
module git.yiffing.dev/leafus/caddy-gitea
|
module git.yiffing.dev/Leafus/caddy-gitea
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
|
|
@ -22,55 +22,6 @@ type Client struct {
|
||||||
gc *gclient.Client
|
gc *gclient.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (d *openDir) ReadDirFile(count int) ([]fs.DirEntry, error) {
|
|
||||||
return d.ReadDir(count)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client, error) {
|
func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client, error) {
|
||||||
if giteapages == "" {
|
if giteapages == "" {
|
||||||
giteapages = "gitea-pages"
|
giteapages = "gitea-pages"
|
||||||
|
@ -94,39 +45,6 @@ func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultNotFoundPage = `<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>404 - Not Found</title>
|
|
||||||
<style>
|
|
||||||
body { font-family: sans-serif; text-align: center; padding: 50px; }
|
|
||||||
h1 { font-size: 48px; margin-bottom: 20px; }
|
|
||||||
p { color: #666; }
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>404</h1>
|
|
||||||
<p>The page you're looking for could not be found.</p>
|
|
||||||
</body>
|
|
||||||
</html>`
|
|
||||||
|
|
||||||
func (c *Client) serveNotFound(owner, repo string) fs.File {
|
|
||||||
if repo != "" {
|
|
||||||
custom404, err := c.getRawFileOrLFS(owner, repo, "404.html", "main")
|
|
||||||
if err == nil {
|
|
||||||
return &openFile{
|
|
||||||
content: custom404,
|
|
||||||
name: "404.html",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &openFile{
|
|
||||||
content: []byte(defaultNotFoundPage),
|
|
||||||
name: "404.html",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Open(name, ref string) (fs.File, error) {
|
func (c *Client) Open(name, ref string) (fs.File, error) {
|
||||||
owner, repo, filepath := splitName(name)
|
owner, repo, filepath := splitName(name)
|
||||||
|
|
||||||
|
@ -135,70 +53,47 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
|
||||||
filepath = "index.html"
|
filepath = "index.html"
|
||||||
}
|
}
|
||||||
|
|
||||||
isFluffyPagesRepo := strings.HasSuffix(repo, ".fluffy.pw")
|
if filepath == "" {
|
||||||
|
filepath = "index.html"
|
||||||
_, resp, err := c.gc.GetRepo(owner, repo)
|
|
||||||
if err != nil {
|
|
||||||
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
|
||||||
return c.serveNotFound(owner, repo), nil
|
|
||||||
}
|
|
||||||
return nil, fs.ErrNotExist
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isFluffyPagesRepo := strings.HasSuffix(repo, ".fluffy.pw")
|
||||||
|
|
||||||
limited, allowall := c.allowsPages(owner, repo)
|
limited, allowall := c.allowsPages(owner, repo)
|
||||||
if !limited && !allowall && !isFluffyPagesRepo {
|
if !limited && !allowall && !isFluffyPagesRepo {
|
||||||
return c.serveNotFound(owner, repo), nil
|
return nil, fs.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
hasConfig := true
|
hasConfig := true
|
||||||
|
|
||||||
if err := c.readConfig(owner, repo); err != nil {
|
if err := c.readConfig(owner, repo); err != nil {
|
||||||
if !isFluffyPagesRepo && !allowall {
|
if !isFluffyPagesRepo && !allowall {
|
||||||
return c.serveNotFound(owner, repo), nil
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hasConfig = false
|
hasConfig = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hasConfig && !validRefs(ref, allowall || isFluffyPagesRepo) {
|
if !hasConfig && !validRefs(ref, allowall || isFluffyPagesRepo) {
|
||||||
return c.serveNotFound(owner, repo), nil
|
return nil, fs.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
|
res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
if strings.HasSuffix(filepath, ".md") {
|
return nil, err
|
||||||
res, err = handleMD(res)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &openFile{
|
|
||||||
content: res,
|
|
||||||
name: filepath,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If file not found, try as directory
|
if strings.HasSuffix(filepath, ".md") {
|
||||||
entries, err := c.getDirectoryContents(owner, repo, filepath, ref)
|
res, err = handleMD(res)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
// Check if this is a directory and the request doesn't end with /
|
return nil, err
|
||||||
// If so, look for index.html in this directory
|
|
||||||
if !strings.HasSuffix(filepath, "/") {
|
|
||||||
indexContent, err := c.getRawFileOrLFS(owner, repo, filepath+"/index.html", ref)
|
|
||||||
if err == nil {
|
|
||||||
return &openFile{
|
|
||||||
content: indexContent,
|
|
||||||
name: "index.html",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &openDir{
|
|
||||||
entries: entries,
|
|
||||||
name: filepath,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neither file nor directory found
|
return &openFile{
|
||||||
return c.serveNotFound(owner, repo), nil
|
content: res,
|
||||||
|
name: filepath,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, error) {
|
func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, error) {
|
||||||
|
@ -355,20 +250,3 @@ func validRefs(ref string, allowall bool) bool {
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
Reference in a new issue