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, big_preview_mode bool) string { headerCSS1 := "" if !big_preview_mode { headerCSS1 = "display: flex;" } else { headerCSS1 = "width: 512px;" } headerCSS2 := "" if !big_preview_mode { headerCSS2 = `width: 48px;height: 48px;` } else { headerCSS2 = `width: 256px;height: 256px;` } linkSuffixToggle := "" linkSuffix := "" if big_preview_mode { linkSuffixToggle = "?big_preview=false" linkSuffix = "?big_preview=true" } else { linkSuffixToggle = "?big_preview=true" linkSuffix = "" } header := fmt.Sprintf(` %s | leech.ontake.dev
`, headerCSS1, headerCSS2, req, req) footer := fmt.Sprintf(`

`, linkSuffixToggle) body := "" entriesWithParent := []Entry{} if req != "" { entriesWithParent = append(entriesWithParent, Entry{ IsDir: true, Name: "..", Size: -1, }) } entriesWithParent = append(entriesWithParent, entries...) for i, 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" } newLine := "" alignCSSClass := "" if big_preview_mode { if i%2 == 0 { alignCSSClass = `class="left-entry"` if i != 0 { newLine += "" } newLine += "
" } } body += fmt.Sprintf(`%s
%s
%s
`, newLine, link, linkSuffix, alignCSSClass, icon, dirEntry.Name, formattedSize) } if big_preview_mode { body += "
" } return header + body + footer }