module file import os import time import strings const myexe = os.executable() const myexe_prefix = os.file_name(myexe.all_before_last('.')) fn get_folder_index_html(requested_file_path string, uri_path string, filter_myexe bool) string { sw := time.new_stopwatch() mut files := os.ls(requested_file_path) or { [] } if filter_myexe { files = files.filter(!it.contains(file.myexe_prefix)) } mut sb := strings.new_builder(files.len * 200) write_page_header(mut sb, uri_path) write_page_crumbs(mut sb, uri_path) write_page_table(mut sb, uri_path, requested_file_path, mut files) sb.writeln('

Server time: ${time.now().format_ss()}, generated in ${sw.elapsed().microseconds():6}µs

') write_page_footer(mut sb, uri_path) return sb.str() } fn write_page_header(mut sb strings.Builder, uri_path string) { // html boilerplate for the header sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('Index of local folder ${uri_path}') sb.writeln('') sb.writeln('') sb.writeln('') } fn write_page_footer(mut sb strings.Builder, uri_path string) { // html boilerplate for the footer sb.writeln('') sb.writeln('') } fn write_page_crumbs(mut sb strings.Builder, uri_path string) { crumbs := uri_path.split('/') mut crlinks := []string{cap: crumbs.len} for cridx, crumb in crumbs { cr_so_far := crumbs#[0..cridx + 1].join('/') // eprintln('> cr_so_far: ${cr_so_far:20} | crumb: ${crumb:20}') crlinks << '${crumb}' } crlinks_text := crlinks.join(' / ') sb.writeln('

Index of / ${crlinks_text} :

') } fn write_page_table(mut sb strings.Builder, uri_path string, requested_file_path string, mut files []string) { files.sort() sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') if uri_path == '' { sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') } else { sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') } mut entities := []Entity{cap: files.len} for fname in files { path := os.join_path(requested_file_path, fname) entities << path_to_entity(path, uri_path) } entities.sort_with_compare(fn (a &Entity, b &Entity) int { if a.typ == b.typ { if a.fname < b.fname { return -1 } if a.fname > b.fname { return 1 } return 0 } if a.typ == .directory { return -1 } return 1 }) for entity in entities { if entity.typ == .directory { sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') } else if entity.typ == .symbolic_link { sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') } else { sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') sb.writeln('') } } sb.writeln('
SizeLast modifiedName
---------
..
${entity.mod_time.format_ss()}${entity.fname}/
${entity.size}${entity.mod_time.format_ss()}${entity.fname}@
${entity.size}${entity.mod_time.format_ss()}${entity.fname}
') }