From: Nils Date: Sun, 21 Jan 2024 23:05:40 +0000 (+0100) Subject: dir listing working X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=da6cac3476384c96ca2381b3e41691650153089c;p=e-paper-project.git dir listing working --- diff --git a/e-paper-project/include/web_HTML.h b/e-paper-project/include/web_HTML.h index af901dd..d2043d3 100644 --- a/e-paper-project/include/web_HTML.h +++ b/e-paper-project/include/web_HTML.h @@ -14,15 +14,17 @@ namespace HTML_data { - const char home_page_str[] PROGMEM = ""; + const char home_page_str[] PROGMEM = ""; const char file_upload_str[] PROGMEM = "

Select File to Upload




[Back]

"; const char upload_finished_str[] PROGMEM = "

File was successfully uploaded

"; + const char dir_page_str[] PROGMEM = "

SD Card Contents


"; + const char append_page_header[] PROGMEM = "File Server

File Server

"; - const char append_page_footer[] PROGMEM = ""; + const char append_page_footer[] PROGMEM = ""; }; diff --git a/e-paper-project/web_server.cpp b/e-paper-project/web_server.cpp index fcb660c..cc442fb 100644 --- a/e-paper-project/web_server.cpp +++ b/e-paper-project/web_server.cpp @@ -18,7 +18,7 @@ static void send_HTML_page(const char* webpage) server.sendContent(HTML_data::append_page_header); server.sendContent(webpage); - server.sendContent(""); + server.sendContent(HTML_data::append_page_footer); server.client().stop(); } @@ -27,6 +27,47 @@ static void home_page() send_HTML_page(HTML_data::home_page_str); } +static void dir_page() +{ + uint8_t levels = 0; + std::unique_ptr root = SD_card_fs::get_file("/"); + if (*root) + { + root->rewindDirectory(); + String web_content {HTML_data::dir_page_str}; + + File file = root->openNextFile(); + while (file) + { + // Serial.print(String(file.name())+"\t"); + web_content += ""; + Serial.print(String(file.isDirectory() ? "Dir " : "File ") + String(file.name()) + "\t"); + web_content += ""; + int bytes = file.size(); + String fsize = ""; + if (bytes < 1024) + fsize = String(bytes) + " B"; + else if (bytes < (1024 * 1024)) + fsize = String(bytes / 1024.0, 3) + " KB"; + else if (bytes < (1024 * 1024 * 1024)) + fsize = String(bytes / 1024.0 / 1024.0, 3) + " MB"; + else + fsize = String(bytes / 1024.0 / 1024.0 / 1024.0, 3) + " GB"; + web_content += ""; + Serial.println(String(fsize)); + + file = root->openNextFile(); + } + web_content += "
Name/TypeType File/DirFile Size
" + String(file.name()) + "" + String(file.isDirectory() ? "Dir" : "File") + "" + fsize + "
"; + send_HTML_page(web_content.c_str()); + root->close(); + } + else + { + send_HTML_page("

No Files Found

"); + } +} + static void file_upload() { send_HTML_page(HTML_data::file_upload_str); @@ -50,7 +91,7 @@ static void handle_file_upload() } else if (uploadfile.status == UPLOAD_FILE_WRITE) { - Serial.println("Writing file somewhere"); + Serial.println("Writing file..."); SD_card_fs::append_to_file(uploadfile.buf, uploadfile.currentSize); } else if (uploadfile.status == UPLOAD_FILE_END) @@ -59,7 +100,7 @@ static void handle_file_upload() Serial.println(uploadfile.totalSize); SD_card_fs::close_file(); - home_page(); + send_HTML_page(HTML_data::upload_finished_str); } else { @@ -75,7 +116,7 @@ namespace Web_server server.on("/upload", file_upload); server.on("/fupload", HTTP_POST,[](){server.send(200);}, handle_file_upload); //server.on("/delete", file_delete); - //server.on("/dir", SD_dir); + server.on("/dir", dir_page); Serial.println("Web-server starting!"); server.begin(); } @@ -103,3 +144,7 @@ namespace Web_server }; + + + +