package html
import (
"fmt"
"leech/thumbnail"
"strings"
"github.com/dustin/go-humanize"
)
type Entry struct {
IsDir bool
Name string
Size int64
}
func FileListPage(req string, entries []Entry) string {
header := fmt.Sprintf(`
%s | leech.ontake.dev
`, req, req)
footer := `
`
body := ""
entriesWithParent := []Entry{}
if req != "" {
entriesWithParent = append(entriesWithParent, Entry{
IsDir: true,
Name: "..",
Size: -1,
})
}
entriesWithParent = append(entriesWithParent, entries...)
for _, dirEntry := range entriesWithParent {
formattedSize := ""
if dirEntry.Size != -1 {
formattedSize = humanize.Bytes(uint64(dirEntry.Size))
}
link := "/" + req + "/" + dirEntry.Name
if dirEntry.Name == ".." {
splitReq := strings.Split(req, "/")
link = "/" + strings.Join(splitReq[:len(splitReq)-1], "/")
}
if req == "" {
link = "/" + req + dirEntry.Name
}
if !dirEntry.IsDir {
link = "/serve/" + req + "/" + dirEntry.Name
}
icon := "/thumb/" + req + "/" + dirEntry.Name
if !thumbnail.IsSupportedFileType(dirEntry.Name) {
icon = "/assets/images/fileicon.png"
}
if dirEntry.IsDir {
icon = "/assets/images/diricon.png"
}
body += fmt.Sprintf(`%s
%s
`, link, icon, dirEntry.Name, formattedSize)
}
return header + body + footer
}