dir listing working
authorNils <forssennils@gmail.com>
Sun, 21 Jan 2024 23:05:40 +0000 (00:05 +0100)
committerNils <forssennils@gmail.com>
Sun, 21 Jan 2024 23:05:40 +0000 (00:05 +0100)
e-paper-project/include/web_HTML.h
e-paper-project/web_server.cpp

index af901dda6aced26800548ef4c8e61c92b94acedb..d2043d3e9ae1f5e78fcef593d81f8e809fe9258f 100644 (file)
 
 namespace HTML_data
 {
-    const char home_page_str[] PROGMEM = "<a href='/download'><button>Download</button></a><a href='/upload'><button>Upload</button></a><a href='/stream'><button>Stream</button></a><a href='/delete'><button>Delete</button></a><a href='/dir'><button>Directory</button></a>";
+    const char home_page_str[] PROGMEM = "<a href='/upload'><button>Upload</button></a><a href='/dir'><button>Directory</button></a>";
     
     const char file_upload_str[] PROGMEM = "<h3>Select File to Upload</h3><FORM action='/fupload' method='post' enctype='multipart/form-data'><input class='buttons' style='width:40%' type='file' name='fupload' id = 'fupload' value=''><br><br><button class='buttons' style='width:10%' type='submit'>Upload File</button><br><a href='/'>[Back]</a><br><br>";
     
     const char upload_finished_str[] PROGMEM = "<h3>File was successfully uploaded</h3>";
 
+    const char dir_page_str[] PROGMEM = "<h3 class='rcorners_m'>SD Card Contents</h3><br><table align='center'><tr><th>Name/Type</th><th style='width:20%'>Type File/Dir</th><th>File Size</th></tr>";
+
     const char append_page_header[] PROGMEM = "<!DOCTYPE html><html><head><title>File Server</title><meta name='viewport' content='user-scalable=yes,initial-scale=1.0,width=device-width'><style>body{max-width:65%;margin:0 auto;font-family:arial;font-size:105%;text-align:center;color:blue;background-color:#F7F2Fd;}ul{list-style-type:none;margin:0.1em;padding:0;border-radius:0.375em;overflow:hidden;background-color:#dcade6;font-size:1em;}li{float:left;border-radius:0.375em;border-right:0.06em solid #bbb;}last-child {border-right:none;font-size:85%}li a{display: block;border-radius:0.375em;padding:0.44em 0.44em;text-decoration:none;font-size:85%}li a:hover{background-color:#EAE3EA;border-radius:0.375em;font-size:85%}section {font-size:0.88em;}h1{color:white;border-radius:0.5em;font-size:1em;padding:0.2em 0.2em;background:#558ED5;}h2{color:orange;font-size:1.0em;}h3{font-size:0.8em;}table{font-family:arial,sans-serif;font-size:0.9em;border-collapse:collapse;width:85%;} th,td {border:0.06em solid #dddddd;text-align:left;padding:0.3em;border-bottom:0.06em solid #dddddd;} tr:nth-child(odd) {background-color:#eeeeee;}.rcorners_n {border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:20%;color:white;font-size:75%;}.rcorners_m {border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:50%;color:white;font-size:75%;}.rcorners_w {border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:70%;color:white;font-size:75%;}.column{float:left;width:50%;height:45%;}.row:after{content:'';display:table;clear:both;}*{box-sizing:border-box;}footer{background-color:#eedfff; text-align:center;padding:0.3em 0.3em;border-radius:0.375em;font-size:60%;}button{border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:20%;color:white;font-size:130%;}.buttons {border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:15%;color:white;font-size:80%;}.buttonsm{border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:9%; color:white;font-size:70%;}.buttonm {border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:15%;color:white;font-size:70%;}.buttonw {border-radius:0.5em;background:#558ED5;padding:0.3em 0.3em;width:40%;color:white;font-size:70%;}a{font-size:75%;}p{font-size:75%;}</style></head><body><h1>File Server</h1>";
 
-    const char append_page_footer[] PROGMEM = "<ul><li><a href='/'>Home</a></li><li><a href='/download'>Download</a></li> <li><a href='/upload'>Upload</a></li> <li><a href='/stream'>Stream</a></li> <li><a href='/delete'>Delete</a></li> <li><a href='/dir'>Directory</a></li></ul></body></html>";
+    const char append_page_footer[] PROGMEM = "<ul><li><a href='/'>Home</a></li><li><a href='/upload'>Upload</a></li><li><a href='/dir'>Directory</a></li></ul></body></html>";
 };
 
 
index fcb660c4dfc4fd34b74b94b114386bc6dea80c0f..cc442fb41a2d86487e93cb5410bf7eb869e32a48 100644 (file)
@@ -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<File> 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 += "<tr><td>" + String(file.name()) + "</td>";
+            Serial.print(String(file.isDirectory() ? "Dir " : "File ") + String(file.name()) + "\t");
+            web_content += "<td>" + String(file.isDirectory() ? "Dir" : "File") + "</td>";
+            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 += "<td>" + fsize + "</td></tr>";
+            Serial.println(String(fsize));
+
+            file = root->openNextFile();
+        }
+        web_content += "</table>";
+        send_HTML_page(web_content.c_str());
+        root->close();
+    }
+    else
+    {
+        send_HTML_page("<h3>No Files Found</h3>");
+    }
+}
+
 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
 };
 
 
+
+
+
+