import std/algorithm import std/os import std/streams import std/times import std/envvars import loader/connecterror import loader/dirlist import utils/twtstr proc loadDir(path: string) = var path = path if path[^1] != '/': #TODO dos/windows path &= '/' var base = getEnv("QUERY_STRING") if base[^1] != '/': #TODO dos/windows base &= '/' stdout.write("Content-Type: text/html\n\n") stdout.write("""
""") var fs: seq[(PathComponent, string)] for pc, file in walkDir(path, relative = true): fs.add((pc, file)) fs.sort(cmp = proc(a, b: (PathComponent, string)): int = cmp(a[1], b[1])) var items: seq[DirlistItem] for (pc, file) in fs: let fullpath = path / file var info: FileInfo try: info = getFileInfo(fullpath, followSymlink = false) except OSError: continue let modified = $info.lastWriteTime.local().format("MMM/dd/yyyy HH:MM") case pc of pcDir: items.add(DirlistItem( t: ITEM_DIR, name: file, modified: modified )) of pcFile: items.add(DirlistItem( t: ITEM_FILE, name: file, modified: modified, nsize: int(info.size) )) of pcLinkToDir, pcLinkToFile: var target = expandSymlink(fullpath) if pc == pcLinkToDir: target &= '/' items.add(DirlistItem( t: ITEM_LINK, name: file, modified: modified, linkto: target )) stdout.write(makeDirlist(items)) stdout.write("\n\n\n\n") proc loadSymlink(path: string) = stdout.write("Content-Type: text/html\n\n") let sl = expandSymlink(path) stdout.write("""