Something went wrong. Try again.
Supercharged auto-indexing of a filesystem for web hosting
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302package main
import ( "strings" "strconv" "path/filepath" "net/url" "os"
log "github.com/sirupsen/logrus")
const ( PAGE_SIZE = 100 TITLE_LENGTH_TAXONOMY = 50)
// Write a table of entries.func writeEntries(w *ConfiguredWriter, entries []entry) { w.AppendString(`<table id="entries"><tr><th id="name">Name</th><th id="modtime">Last Modified</th><th id="size">Size</th></tr><tr><td><a href="../">../</a></td><td></td><td></td></tr>`) for _, e := range entries { w.AppendString(`<tr><td><a href="`) w.AppendString(e.generateLink(w.Roots.Main, w.Uris)) w.AppendString(`">`) w.AppendString(e.generateName()) w.AppendString(`</a></td><td data-sort="`) w.AppendString(e.generateSortableDate()) w.AppendString(`">`) w.AppendString(e.generateReadableDate()) w.AppendString(`</td><td data-sort1="`) w.AppendString(e.generateIsFolder()) w.AppendString(`" data-sort2="`) w.AppendString(e.generateSortableSize()) w.AppendString(`">`) w.AppendString(e.generateReadableSize()) w.AppendString(`</td></tr>`) } w.AppendString(`</table>`)}
// Write a table of taxonomy entries.func writeTaxonomyEntries(w *ConfiguredWriter, root string, entries []taxonomyEntry) { w.AppendString(`<table id="entries"><tr><th id="name">Name</th><th id="modtime">Last Modified</th><th id="count">Count</th></tr><tr><td><a href="../">../</a></td><td></td><td></td></tr>`) for _, e := range entries { s := e.generateCount()
w.AppendString(`<tr><td><a href="`) w.AppendString(e.generateLink(root)) w.AppendString(`">`) w.AppendString(e.generateName()) w.AppendString(`</a></td><td data-sort="`) w.AppendString(e.generateSortableDate()) w.AppendString(`">`) w.AppendString(e.generateReadableDate()) w.AppendString(`</td><td data-sort="`) w.AppendInt(s) w.AppendString(`">`) w.AppendInt(s) w.AppendString(`</td></tr>`) } w.AppendString(`</table>`)}
// Write pagination links.func writePagination(w *ConfiguredWriter, max int) { w.AppendString(`<ul id="pagination">`)
for i := 1; i <= max; i++ { b := []byte(strconv.Itoa(i))
w.AppendString(`<li id="pagination`) w.Append(b) w.AppendString(`"><a href="?page=`) w.Append(b) w.AppendString(`">`) w.Append(b) w.AppendString(`</a></li>`) }
w.AppendString(`</ul>`)}
// Write a sequence of entries with previews.func writeEntriesWithPreview(w *ConfiguredWriter, entries []entry) { w.AppendString(`<a href="../">Up one level</a><div class="page" id="page1">`)
for i, e := range entries { if (i != 0) && ((i % PAGE_SIZE) == 0) { w.AppendString(`</div><div class="page" id="page`) w.AppendInt((i / PAGE_SIZE) + 1) w.AppendString(`">`) }
w.AppendString(`<a class="image" href="`) w.AppendString(e.generateLink(w.Roots.Main, w.Uris)) w.AppendString(`"><img loading="lazy" src="`) w.AppendString(e.generatePreview("200x200", w.Uris)) w.AppendString(`" onerror="this.src='/fallback.svg';this.onerror='';"><p class="title">`) w.AppendString(e.generateTitle())
if 0 < e.Children { w.AppendString(`</p><p class="count">`) w.AppendString(e.generateChildCount(50)) }
w.AppendString(`</p></a>`) }
w.AppendString(`</div>`)
writePagination(w, (len(entries) / PAGE_SIZE) + 1)}
// Write a sequence of book entries with previews.func writeBookEntries(w *ConfiguredWriter, entries []entry) { w.AppendString(`<a href="../">Up one level</a><div class="page" id="page1">`)
for i, e := range entries { if (i != 0) && ((i % PAGE_SIZE) == 0) { w.AppendString(`</div><div class="page" id="page`) w.AppendInt((i / PAGE_SIZE) + 1) w.AppendString(`">`) }
w.AppendString(`<a class="image" href="`) w.AppendString(e.generateLink(w.Roots.Main, w.Uris)) w.AppendString(`"><img loading="lazy" src="`) w.AppendString(e.generatePreview("200x300", w.Uris)) w.AppendString(`" onerror="this.src='/fallback.svg';this.onerror='';"></a>`) }
w.AppendString(`</div>`) writePagination(w, (len(entries) / PAGE_SIZE) + 1)}
// Write a sequence of web comic entries with previews.func writeWebComicEntries(w *ConfiguredWriter, entries []entry) { //TODO: implement}
// Write a taxonomic directory.func writeTaxonomy(w *ConfiguredWriter, root, name string, entries []entry) { if len(entries) == 0 { return }
// Write to `<virtual root>/<taxonomy name>/index.html`. dest := filepath.Join("." + root, safeLocation(name), "index.html")
// Truncate page title as needed. title := name if TITLE_LENGTH_TAXONOMY < len(title) { title = title[:TITLE_LENGTH_TAXONOMY] + "..." }
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil { log.WithError(err).Errorf("cannot create directories for %s", dest) return }
w.Reset()
w.AppendString(`<!doctype html><html lang="en"><head><meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"><title>`) w.AppendString(name) w.AppendString(`</title><link rel="stylesheet" type="text/css" href="/css/common.css" /><script src="/js/jquery-3.6.0.min.js"></script><script src="/js/common.js"></script><script src="/js/pagination.js"></script></head><body><nav>`) w.AppendNavigation() w.AppendString(`</nav><main><h1>`) w.AppendString(title) w.AppendString(`</h1><aside><ul id="tags-and-creators"></ul></aside><article>`) writeEntriesWithPreview(w, entries) w.AppendString(`</article></main></body></html>`)
w.Write(dest)}
// Write the virtual root directory.func writeRoot(w *ConfiguredWriter, entries []entry, preview bool) { // Write to `<virtual root>/index.html`. dest := filepath.Join("." + w.Roots.Main, "index.html")
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil { log.WithError(err).Errorf("cannot create directories for %s", dest) return }
w.Reset()
w.AppendString(`<!doctype html><html lang="en"><head><meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"><title>Latest Entries</title><link rel="stylesheet" type="text/css" href="/css/common.css" /><script src="/js/jquery-3.6.0.min.js"></script><script src="/js/common.js"></script>`) if preview { w.AppendString(`<script src="/js/pagination.js"></script>`) } w.AppendString(`</head><body><nav>`) w.AppendNavigation() w.AppendString(`</nav><main><h1>Latest Entries</h1><article>`)
if preview { writeEntriesWithPreview(w, entries) } else { writeEntries(w, entries) }
w.AppendString(`</article></main></body></html>`)
w.Write(dest)}
// Helper to construct a URI as `<imagor host>/unsafe/<dims>/<filters>/<path>`.func imagorUri(dims, path string, u uris) string { safePath := url.PathEscape( strings.ReplaceAll(path, " ", "+")) return u.Imagor + "/unsafe/" + dims + u.ImagorFilters + safePath}
// Helper to construct a URI as `<imagorvideo host>/unsafe/<dims>/<filters>/<path>`.func imagorVideoUri(dims, path string, u uris) string { safePath := url.PathEscape( strings.ReplaceAll(path, " ", "+")) return u.ImagorVideo + "/unsafe/" + dims + u.ImagorVideoFilters + safePath}
var ( safeReplacer = strings.NewReplacer(" - ","+", "!","", "?","", ",","", "'","", " ","+"))
// Construct a safe path. Since files are read from the filesystem, paths are// certain to be filesystem-safe. Just need to make them URL safe now.// Preference is to strip rare but problematic characters, rather than percent-// encode them. Introduces a risk of collision.func safeLocation(location string) string { return safeReplacer.Replace(strings.ToLower(location))}