in lib/src/directory_listing.dart [53:99]
Response listDirectory(String fileSystemPath, String dirPath) {
final controller = StreamController<List<int>>();
const encoding = Utf8Codec();
const sanitizer = HtmlEscape();
void add(String string) {
controller.add(encoding.encode(string));
}
var heading = path.relative(dirPath, from: fileSystemPath);
if (heading == '.') {
heading = '/';
} else {
heading = '/$heading/';
}
add(_getHeader(sanitizer.convert(heading)));
// Return a sorted listing of the directory contents asynchronously.
Directory(dirPath).list().toList().then((entities) {
entities.sort((e1, e2) {
if (e1 is Directory && e2 is! Directory) {
return -1;
}
if (e1 is! Directory && e2 is Directory) {
return 1;
}
return e1.path.compareTo(e2.path);
});
for (var entity in entities) {
var name = path.relative(entity.path, from: dirPath);
if (entity is Directory) name += '/';
final sanitizedName = sanitizer.convert(name);
add(' <li><a href="$sanitizedName">$sanitizedName</a></li>\n');
}
add(_trailer);
controller.close();
});
return Response.ok(
controller.stream,
encoding: encoding,
headers: {HttpHeaders.contentTypeHeader: 'text/html'},
);
}