Compare commits
No commits in common. "main" and "v0.0.7" have entirely different histories.
1 changed files with 19 additions and 77 deletions
|
@ -67,10 +67,6 @@ 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"
|
||||||
|
@ -94,39 +90,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)
|
||||||
|
|
||||||
|
@ -137,68 +100,47 @@ 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 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)
|
|
||||||
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)
|
entries, err := c.getDirectoryContents(owner, repo, filepath, ref)
|
||||||
if err == nil {
|
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{
|
return &openDir{
|
||||||
entries: entries,
|
entries: entries,
|
||||||
name: filepath,
|
name: filepath,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neither file nor directory found
|
res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
|
||||||
return c.serveNotFound(owner, repo), nil
|
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) {
|
func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, error) {
|
||||||
|
|
Reference in a new issue