Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
70a70ca983 | |||
c5d9235b47 | |||
084ef407c3 | |||
114e83e5b2 |
3 changed files with 146 additions and 24 deletions
2
gitea.go
2
gitea.go
|
@ -5,7 +5,7 @@ import (
|
|||
"net/http"
|
||||
"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/caddyconfig/caddyfile"
|
||||
"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
|
||||
|
||||
|
|
|
@ -22,6 +22,55 @@ type Client struct {
|
|||
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) {
|
||||
if giteapages == "" {
|
||||
giteapages = "gitea-pages"
|
||||
|
@ -45,6 +94,39 @@ func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client
|
|||
}, 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) {
|
||||
owner, repo, filepath := splitName(name)
|
||||
|
||||
|
@ -53,47 +135,70 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
|
|||
filepath = "index.html"
|
||||
}
|
||||
|
||||
if filepath == "" {
|
||||
filepath = "index.html"
|
||||
}
|
||||
|
||||
isFluffyPagesRepo := strings.HasSuffix(repo, ".fluffy.pw")
|
||||
|
||||
limited, allowall := c.allowsPages(owner, repo)
|
||||
if !limited && !allowall && !isFluffyPagesRepo {
|
||||
_, 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
|
||||
}
|
||||
|
||||
hasConfig := true
|
||||
|
||||
if err := c.readConfig(owner, repo); err != nil {
|
||||
if !isFluffyPagesRepo && !allowall {
|
||||
return nil, err
|
||||
limited, allowall := c.allowsPages(owner, repo)
|
||||
if !limited && !allowall && !isFluffyPagesRepo {
|
||||
return c.serveNotFound(owner, repo), nil
|
||||
}
|
||||
|
||||
hasConfig := true
|
||||
if err := c.readConfig(owner, repo); err != nil {
|
||||
if !isFluffyPagesRepo && !allowall {
|
||||
return c.serveNotFound(owner, repo), nil
|
||||
}
|
||||
hasConfig = false
|
||||
}
|
||||
|
||||
if !hasConfig && !validRefs(ref, allowall || isFluffyPagesRepo) {
|
||||
return nil, fs.ErrNotExist
|
||||
return c.serveNotFound(owner, repo), nil
|
||||
}
|
||||
|
||||
res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
if strings.HasSuffix(filepath, ".md") {
|
||||
res, err = handleMD(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &openFile{
|
||||
content: res,
|
||||
name: filepath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// If file not found, try as directory
|
||||
entries, err := c.getDirectoryContents(owner, repo, filepath, ref)
|
||||
if err == nil {
|
||||
// Check if this is a directory and the request doesn't end with /
|
||||
// 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 c.serveNotFound(owner, repo), nil
|
||||
}
|
||||
|
||||
func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, error) {
|
||||
|
@ -250,3 +355,20 @@ func validRefs(ref string, allowall bool) bool {
|
|||
|
||||
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