From ee4832323d5bf074253fbdc593ea1020d730228a Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Sun, 8 Jan 2023 12:36:13 +0100 Subject: [PATCH] Improve file server (#1785) * . * . * . * . * . * . * . Co-authored-by: CaCO3 --- .../jomjol_fileserver_ota/server_file.cpp | 22 ++++-- .../{upload_script.html => file_server.html} | 74 ++++++++++++++++--- sd-card/html/index.html | 4 +- sd-card/html/ota_page.html | 6 +- sd-card/html/wlan_config.html | 2 +- 5 files changed, 88 insertions(+), 20 deletions(-) rename sd-card/html/{upload_script.html => file_server.html} (64%) diff --git a/code/components/jomjol_fileserver_ota/server_file.cpp b/code/components/jomjol_fileserver_ota/server_file.cpp index 5807072f..3f3b6601 100644 --- a/code/components/jomjol_fileserver_ota/server_file.cpp +++ b/code/components/jomjol_fileserver_ota/server_file.cpp @@ -219,7 +219,7 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const ///////////////////////////////////////////////// if (!readonly) { - FILE *fd = fopen("/sdcard/html/upload_script.html", "r"); + FILE *fd = fopen("/sdcard/html/file_server.html", "r"); char *chunk = ((struct file_server_data *)req->user_ctx)->scratch; size_t chunksize; do { @@ -245,11 +245,11 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const /* Send file-list table definition and column labels */ httpd_resp_sendstr_chunk(req, - "" + "
" "" - ""); + ""); if (!readonly) { - httpd_resp_sendstr_chunk(req, "
NameTypeSize (Bytes)
NameTypeSizeDelete
" + httpd_resp_sendstr_chunk(req, "
" "
d_name); continue; } - sprintf(entrysize, "%ld", entry_stat.st_size); + + if (entry->d_type == DT_DIR) { + strcpy(entrysize, "-\0"); + } + else { + if (entry_stat.st_size >= 1024) { + sprintf(entrysize, "%ld KiB", entry_stat.st_size / 1024); // kBytes + } + else { + sprintf(entrysize, "%ld B", entry_stat.st_size); // Bytes + } + } + ESP_LOGI(TAG, "Found %s: %s (%s bytes)", entrytype, entry->d_name, entrysize); /* Send chunk of HTML file containing table entries with file name and size */ diff --git a/sd-card/html/upload_script.html b/sd-card/html/file_server.html similarity index 64% rename from sd-card/html/upload_script.html rename to sd-card/html/file_server.html index 72ef30c5..51d5009f 100644 --- a/sd-card/html/upload_script.html +++ b/sd-card/html/file_server.html @@ -1,18 +1,43 @@ - - - + + + + - - +
- + + +
-

ESP32 File Server

-
- +

Content on SD-Card

+
@@ -35,6 +60,11 @@
+      +
diff --git a/sd-card/html/index.html b/sd-card/html/index.html index 09cbc0ce..5ce92c81 100644 --- a/sd-card/html/index.html +++ b/sd-card/html/index.html @@ -18,7 +18,7 @@