diff --git a/pkg/gitea/gitea.go b/pkg/gitea/gitea.go index 090332a..2bb87e7 100644 --- a/pkg/gitea/gitea.go +++ b/pkg/gitea/gitea.go @@ -22,6 +22,51 @@ 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 NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client, error) { if giteapages == "" { giteapages = "gitea-pages" @@ -53,10 +98,6 @@ 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) @@ -65,12 +106,10 @@ func (c *Client) Open(name, ref string) (fs.File, error) { } hasConfig := true - if err := c.readConfig(owner, repo); err != nil { if !isFluffyPagesRepo && !allowall { return nil, err } - hasConfig = false } @@ -78,6 +117,14 @@ func (c *Client) Open(name, ref string) (fs.File, error) { return nil, fs.ErrNotExist } + entries, err := c.getDirectoryContents(owner, repo, filepath, ref) + if err == nil { + return &openDir{ + entries: entries, + name: filepath, + }, nil + } + res, err := c.getRawFileOrLFS(owner, repo, filepath, ref) if err != nil { return nil, err @@ -250,3 +297,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 +}