Compare commits

...

2 commits
v0.0.7 ... main

Author SHA1 Message Date
70a70ca983 ughh 2024-12-14 22:46:17 +01:00
c5d9235b47 Fix directories and add support for 404 pages 2024-12-14 22:37:28 +01:00

View file

@ -67,6 +67,10 @@ func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) {
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"
@ -90,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)
@ -100,49 +137,70 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
isFluffyPagesRepo := strings.HasSuffix(repo, ".fluffy.pw")
_, 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
}
limited, allowall := c.allowsPages(owner, repo)
if !limited && !allowall && !isFluffyPagesRepo {
return nil, fs.ErrNotExist
return c.serveNotFound(owner, repo), nil
}
hasConfig := true
if err := c.readConfig(owner, repo); err != nil {
if !isFluffyPagesRepo && !allowall {
return nil, err
return c.serveNotFound(owner, repo), nil
}
hasConfig = false
}
if !hasConfig && !validRefs(ref, allowall || isFluffyPagesRepo) {
return nil, fs.ErrNotExist
}
entries, err := c.getDirectoryContents(owner, repo, filepath, ref)
if err == nil {
return &openDir{
entries: entries,
name: filepath,
}, nil
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) {
var (
giteaURL string