From 8b4663156c04bbe1185343865af556724f554725 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 23 May 2016 15:09:57 -0400 Subject: [PATCH] nitcorn/file_server: extract answer_directory_listing method Signed-off-by: Alexandre Terrasa --- lib/nitcorn/file_server.nit | 103 +++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/lib/nitcorn/file_server.nit b/lib/nitcorn/file_server.nit index f9feb0f..b701aa9 100644 --- a/lib/nitcorn/file_server.nit +++ b/lib/nitcorn/file_server.nit @@ -110,54 +110,7 @@ class FileServer var is_dir = local_file.file_stat.is_dir if show_directory_listing and is_dir then - # Show the directory listing - var title = turi - var files = local_file.files - - alpha_comparator.sort files - - var links = new Array[String] - if turi.length > 1 then - var path = (request.uri + "/..").simplify_path - links.add ".." - end - for file in files do - var local_path = local_file.join_path(file).simplify_path - var web_path = file.simplify_path - if local_path.file_stat.is_dir then web_path = web_path + "/" - links.add "{file}" - end - - var header = self.header - var header_code - if header != null then - header_code = header.write_to_string - else header_code = "" - - response = new HttpResponse(200) - response.body = """ - - - - - - - {{{title}}} - - - {{{header_code}}} -
-

{{{title}}}

-
    -
  • {{{links.join("
  • \n\t\t\t
  • ")}}}
  • -
-
- -""" - - response.header["Content-Type"] = media_types["html"].as(not null) + response = answer_directory_listing(request, turi, local_file) else if not is_dir then # It's a single file response = answer_file(local_file) else response = new HttpResponse(404) @@ -195,4 +148,58 @@ class FileServer response.header["cache-control"] = cache_control return response end + + # Answer with a directory listing for files within `local_files`. + fun answer_directory_listing(request: HttpRequest, turi, local_file: String): HttpResponse do + # Show the directory listing + var title = turi + var files = local_file.files + + alpha_comparator.sort files + + var links = new Array[String] + if turi.length > 1 then + var path = (request.uri + "/..").simplify_path + links.add ".." + end + for file in files do + var local_path = local_file.join_path(file).simplify_path + var web_path = file.simplify_path + var file_stat = local_path.file_stat + if file_stat != null and file_stat.is_dir then web_path = web_path + "/" + links.add "{file}" + end + + var header = self.header + var header_code + if header != null then + header_code = header.write_to_string + else header_code = "" + + var response = new HttpResponse(200) + response.body = """ + + + + + + + {{{title}}} + + + {{{header_code}}} +
+

{{{title}}}

+
    +
  • {{{links.join("
  • \n\t\t\t
  • ")}}}
  • +
+
+ +""" + + response.header["Content-Type"] = media_types["html"].as(not null) + return response + end end -- 1.7.9.5