Fix directories and add support for 404 pages
This commit is contained in:
parent
084ef407c3
commit
c5d9235b47
1 changed files with 51 additions and 3 deletions
|
@ -67,6 +67,10 @@ func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) {
|
||||||
return entries, nil
|
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"
|
||||||
|
@ -90,6 +94,39 @@ 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)
|
||||||
|
|
||||||
|
@ -100,21 +137,29 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
|
||||||
|
|
||||||
isFluffyPagesRepo := strings.HasSuffix(repo, ".fluffy.pw")
|
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)
|
limited, allowall := c.allowsPages(owner, repo)
|
||||||
if !limited && !allowall && !isFluffyPagesRepo {
|
if !limited && !allowall && !isFluffyPagesRepo {
|
||||||
return nil, fs.ErrNotExist
|
return c.serveNotFound(owner, repo), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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 nil, err
|
return c.serveNotFound(owner, repo), nil
|
||||||
}
|
}
|
||||||
hasConfig = false
|
hasConfig = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hasConfig && !validRefs(ref, allowall || isFluffyPagesRepo) {
|
if !hasConfig && !validRefs(ref, allowall || isFluffyPagesRepo) {
|
||||||
return nil, fs.ErrNotExist
|
return c.serveNotFound(owner, repo), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
entries, err := c.getDirectoryContents(owner, repo, filepath, ref)
|
entries, err := c.getDirectoryContents(owner, repo, filepath, ref)
|
||||||
|
@ -127,6 +172,9 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
|
||||||
|
|
||||||
res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
|
res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == fs.ErrNotExist {
|
||||||
|
return c.serveNotFound(owner, repo), nil
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue