jomjol 5 лет назад
Родитель
Сommit
78b56ce9d4

+ 21 - 7
README.md

@@ -11,9 +11,29 @@ A 3d-printable housing can be found here: https://www.thingiverse.com/thing:4571
 <img src="https://raw.githubusercontent.com/jomjol/AI-on-the-edge-device/master/images/watermeter.jpg" width="600"> 
 <img src="https://raw.githubusercontent.com/jomjol/AI-on-the-edge-device/master/images/edit_reference.jpg" width="600"> 
 
-
 ## Change log
 
+
+
+------
+
+### Known Issues
+
+* Parts of the web page only works correctly in **Firefox**!
+  With **Chrome** or **Edge** not all parts (especially the configuration) is **not full functional**.
+* spontaneous reboot, especially in case of intensive web server access
+
+------
+
+
+
+##### 1.1.0 (2020-09-06)
+
+* Implementation of "delete complete directory"
+  **Attention: beside the `firmware.bin`, also the content of `/html` needs to be updated!**
+
+
+
 ##### 1.0.2 (2020-09-06)
 
 * Bug in configuration of analog ROIs corrected
@@ -40,12 +60,6 @@ A 3d-printable housing can be found here: https://www.thingiverse.com/thing:4571
 
 
 
-## Known Issues
-
-* spontaneous reboot, especially in case of intensive web server access
-
-
-
 ## Solved topics
 
 * n.a.

+ 97 - 53
code/lib/jomjol_fileserver_ota/server_file.cpp

@@ -119,32 +119,25 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath)
     /* Send HTML file header */
     httpd_resp_sendstr_chunk(req, "<!DOCTYPE html><html><body>");
 
-    /* Get handle to embedded file upload script */
-    extern const unsigned char upload_script_start[] asm("_binary_upload_script_html_start");
-    extern const unsigned char upload_script_end[]   asm("_binary_upload_script_html_end");
-    const size_t upload_script_size = (upload_script_end - upload_script_start);
-
-    /* Add file upload form and script which on execution sends a POST request to /upload */
-    httpd_resp_send_chunk(req, (const char *)upload_script_start, upload_script_size);
-
-
 /////////////////////////////////////////////////
-/*
-    FILE *fd = fopen("/sdcard/html/file_server_upload_script.html", "r");
+
+    FILE *fd = fopen("/sdcard/html/upload_script.html", "r");
     char *chunk = ((struct file_server_data *)req->user_ctx)->scratch;
     size_t chunksize;
     do {
         chunksize = fread(chunk, 1, SCRATCH_BUFSIZE, fd);
-        if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
+//        printf("Chunksize %d\n", chunksize);
+        if (chunksize > 0){
+            if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
             fclose(fd);
             ESP_LOGE(TAG, "File sending failed!");
             return ESP_FAIL;
+            }
         }
     } while (chunksize != 0);
     fclose(fd);
-    ESP_LOGI(TAG, "File sending complete");
-    httpd_resp_send_chunk(req, NULL, 0);
-*/
+//    ESP_LOGI(TAG, "File sending complete");
+
 ///////////////////////////////
 
 
@@ -152,7 +145,7 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath)
     httpd_resp_sendstr_chunk(req,
         "<table class=\"fixed\" border=\"1\">"
         "<col width=\"800px\" /><col width=\"300px\" /><col width=\"300px\" /><col width=\"100px\" />"
-        "<thead><tr><th>Name</th><th>Type</th><th>Size (Bytes)</th><th>Delete</th></tr></thead>"
+        "<thead><tr><th>Name</th><th>Type</th><th>Size (Bytes)</th><th>Delete<br><button id=\"deleteall\" type=\"button\" onclick=\"deleteall()\">DELETE ALL!</button></th></tr></thead>"
         "<tbody>");
 
     /* Iterate over all files / folders and fetch their names and sizes */
@@ -434,51 +427,100 @@ static esp_err_t delete_post_handler(httpd_req_t *req)
     struct stat file_stat;
 
 
-    /* Skip leading "/delete" from URI to get filename */
-    /* Note sizeof() counts NULL termination hence the -1 */
-    const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path,
-                                             req->uri  + sizeof("/delete") - 1, sizeof(filepath));
-    if (!filename) {
-        /* Respond with 500 Internal Server Error */
-        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
-        return ESP_FAIL;
+//////////////////////////////////////////////////////////////
+    char _query[200];
+    char _filename[30];
+    char _valuechar[30];    
+    std::string fn = "/sdcard/firmware/";
+    std::string _task;
+    std::string directory;
+    std::string zw; 
+
+    if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
+    {
+        printf("Query: "); printf(_query); printf("\n");
+        
+        if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
+        {
+            printf("task is found: "); printf(_valuechar); printf("\n"); 
+            _task = std::string(_valuechar);
+        }
     }
 
-    /* Filename cannot have a trailing '/' */
-    if (filename[strlen(filename) - 1] == '/') {
-        ESP_LOGE(TAG, "Invalid filename : %s", filename);
-        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Invalid filename");
-        return ESP_FAIL;
+    if (_task.compare("deldircontent") == 0)
+    {
+        /* Skip leading "/delete" from URI to get filename */
+        /* Note sizeof() counts NULL termination hence the -1 */
+        const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path,
+                                                req->uri  + sizeof("/delete") - 1, sizeof(filepath));
+        if (!filename) {
+            /* Respond with 500 Internal Server Error */
+            httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
+            return ESP_FAIL;
+        }
+        zw = std::string(filename);
+        zw = zw.substr(0, zw.length()-1);
+        zw = "/sdcard" + zw;
+        printf("Directory to delete: %s\n", zw.c_str());
+
+        delete_all_in_directory(zw);
+        directory = std::string(filepath);
+        printf("Location after delete directory content: %s\n", directory.c_str());
     }
+    else
+    {
+        /* Skip leading "/delete" from URI to get filename */
+        /* Note sizeof() counts NULL termination hence the -1 */
+        const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path,
+                                                req->uri  + sizeof("/delete") - 1, sizeof(filepath));
+        if (!filename) {
+            /* Respond with 500 Internal Server Error */
+            httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
+            return ESP_FAIL;
+        }
 
-    if (stat(filepath, &file_stat) == -1) {
-        ESP_LOGE(TAG, "File does not exist : %s", filename);
-        /* Respond with 400 Bad Request */
-        httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "File does not exist");
-        return ESP_FAIL;
+        /* Filename cannot have a trailing '/' */
+        if (filename[strlen(filename) - 1] == '/') {
+            ESP_LOGE(TAG, "Invalid filename : %s", filename);
+            httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Invalid filename");
+            return ESP_FAIL;
+        }
+
+        if (stat(filepath, &file_stat) == -1) {
+            ESP_LOGE(TAG, "File does not exist : %s", filename);
+            /* Respond with 400 Bad Request */
+            httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "File does not exist");
+            return ESP_FAIL;
+        }
+
+        ESP_LOGI(TAG, "Deleting file : %s", filename);
+        /* Delete file */
+        unlink(filepath);
+
+        directory = std::string(filepath);
+        size_t zw = directory.find("/");
+        size_t found = zw;
+        while (zw != std::string::npos)
+        {
+            zw = directory.find("/", found+1);  
+            if (zw != std::string::npos)
+                found = zw;
+        }
+
+        int start_fn = strlen(((struct file_server_data *)req->user_ctx)->base_path);
+        printf("Directory: %s, start_fn: %d, found: %d\n", directory.c_str(), start_fn, found);
+        directory = directory.substr(start_fn, found - start_fn + 1);
+        printf("Directory danach: %s\n", directory.c_str());    
+
+        directory = "/fileserver" + directory;
+        printf("Directory danach: %s\n", directory.c_str());   
     }
+    
 
-    ESP_LOGI(TAG, "Deleting file : %s", filename);
-    /* Delete file */
-    unlink(filepath);
 
-    std::string directory = std::string(filepath);
-	size_t zw = directory.find("/");
-	size_t found = zw;
-	while (zw != std::string::npos)
-	{
-		zw = directory.find("/", found+1);  
-		if (zw != std::string::npos)
-			found = zw;
-	}
 
-    int start_fn = strlen(((struct file_server_data *)req->user_ctx)->base_path);
-    printf("Directory: %s, start_fn: %d, found: %d\n", directory.c_str(), start_fn, found);
-	directory = directory.substr(start_fn, found - start_fn + 1);
-    printf("Directory danach: %s\n", directory.c_str());    
 
-    directory = "/fileserver" + directory;
-    printf("Directory danach: %s\n", directory.c_str());   
+//////////////////////////////////////////////////////////////
 
     /* Redirect onto root to see the updated file list */
     httpd_resp_set_status(req, "303 See Other");
@@ -502,7 +544,7 @@ void delete_all_in_directory(std::string _directory)
     /* Iterate over all files / folders and fetch their names and sizes */
     while ((entry = readdir(dir)) != NULL) {
         if (!(entry->d_type == DT_DIR)){
-            filename = _directory + std::string(entry->d_name);
+            filename = _directory + "/" + std::string(entry->d_name);
             ESP_LOGI(TAG, "Deleting file : %s", filename.c_str());
             /* Delete file */
             unlink(filename.c_str());    
@@ -524,6 +566,8 @@ void unzip(std::string _in_zip_file, std::string _target_directory){
 //    static const char* s_Test_archive_filename = "testhtml.zip";
 
     printf("miniz.c version: %s\n", MZ_VERSION);
+    printf("Zipfile: %s\n", _in_zip_file.c_str());
+    printf("Target Dir: %s\n", _target_directory.c_str());
 
     // Now try to open the archive.
     memset(&zip_archive, 0, sizeof(zip_archive));

+ 5 - 3
code/lib/jomjol_fileserver_ota/server_ota.cpp

@@ -338,11 +338,13 @@ esp_err_t handler_ota_update(httpd_req_t *req)
         std::string in, out, zw;
 
         in = "/sdcard/firmware/html.zip";
-        out = "/sdcard/html/";
+        out = "/sdcard/html2/";
 
         unzip(in, out);
-        zw = "Unzip html Done";
-        httpd_resp_sendstr_chunk(req, zw.c_str()); 
+        zw = "HTML Update Successfull!<br><br>No reboot necessary";
+        httpd_resp_sendstr_chunk(req, zw.c_str());
+        httpd_resp_sendstr_chunk(req, NULL);  
+        return ESP_OK;        
     }
 
 

+ 0 - 1
code/platformio.ini

@@ -18,7 +18,6 @@ framework = espidf
 
 board_build.embed_files  =
   src/favicon.ico
-  src/upload_script.html
 
 
 ;board_build.partitions = partitions_singleapp.csv

+ 1 - 1
code/src/CMakeLists.txt

@@ -7,4 +7,4 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
 
 idf_component_register(SRCS ${app_sources}
                     INCLUDE_DIRS "."
-                    EMBED_FILES "favicon.ico" "upload_script.html")
+                    EMBED_FILES "favicon.ico")

+ 0 - 101
code/src/upload_script.html

@@ -1,101 +0,0 @@
-<table class="fixed" border="0">
-    <col width="300px" /><col width="200px" />
-    <tr><td>
-        <h2>ESP32 File Server</h2>
-    </td><td>
-        <button id="dirup" type="button" onclick="dirup()">Directory up</button>
-    </td>
-    <td>
-        <table border="0">
-            <tr>
-                <td>
-                    <label for="newfile">Upload a file</label>
-                </td>
-                <td colspan="2">
-                    <input id="newfile" type="file" onchange="setpath()" style="width:100%;">
-                </td>
-            </tr>
-            <tr>
-                <td>
-                    <label for="filepath">Set path on server</label>
-                </td>
-                <td>
-                    <input id="filepath" type="text" style="width:100%;">
-                </td>
-                <td>
-                    <button id="upload" type="button" onclick="upload()">Upload</button>
-                </td>
-            </tr>
-        </table>
-    </td></tr>
-</table>
-<script>
-function setpath() {
-    var fileserverpraefix = "/fileserver";
-    var anz_zeichen_fileserver = fileserverpraefix.length;
-    var default_path = window.location.pathname.substring(anz_zeichen_fileserver) + document.getElementById("newfile").files[0].name;
-    document.getElementById("filepath").value = default_path;
-}
-
-function dirup() {
-    var str = window.location.href;
-    str = str.substring(0, str.length-1);
-	var zw = str.indexOf("/");
-	var found = zw;
-	while (zw >= 0)
-	{
-		zw = str.indexOf("/", found+1);  
-		if (zw >= 0)
-			found = zw;
-	}
-	var res = str.substring(0, found+1);
-	window.location.href = res;	
-}
-
-function upload() {
-    var filePath = document.getElementById("filepath").value;
-    var upload_path = "/upload/" + filePath;
-    var fileInput = document.getElementById("newfile").files;
-
-    /* Max size of an individual file. Make sure this
-     * value is same as that set in file_server.c */
-    var MAX_FILE_SIZE = 2000*1024;
-    var MAX_FILE_SIZE_STR = "2000KB";
-
-    if (fileInput.length == 0) {
-        alert("No file selected!");
-    } else if (filePath.length == 0) {
-        alert("File path on server is not set!");
-    } else if (filePath.indexOf(' ') >= 0) {
-        alert("File path on server cannot have spaces!");
-    } else if (filePath[filePath.length-1] == '/') {
-        alert("File name not specified after path!");
-    } else if (fileInput[0].size > 2000*1024) {
-        alert("File size must be less than 2000KB!");
-    } else {
-        document.getElementById("newfile").disabled = true;
-        document.getElementById("filepath").disabled = true;
-        document.getElementById("upload").disabled = true;
-
-        var file = fileInput[0];
-        var xhttp = new XMLHttpRequest();
-        xhttp.onreadystatechange = function() {
-            if (xhttp.readyState == 4) {
-                if (xhttp.status == 200) {
-                    document.open();
-                    document.write(xhttp.responseText);
-                    document.close();
-                } else if (xhttp.status == 0) {
-                    alert("Server closed the connection abruptly!");
-                    location.reload()
-                } else {
-                    alert(xhttp.status + " Error!\n" + xhttp.responseText);
-                    location.reload()
-                }
-            }
-        };
-        xhttp.open("POST", upload_path, true);
-        xhttp.send(file);
-    }
-}
-</script>

+ 0 - 82
sd-card/html/file_server_upload_script.html

@@ -1,82 +0,0 @@
-<table class="fixed" border="0">
-    <col width="1000px" /><col width="500px" />
-    <tr><td>
-        <h2>ESP32 File Server</h2>
-    </td><td>
-        <table border="0">
-            <tr>
-                <td>
-                    <label for="newfile">Upload a file</label>
-                </td>
-                <td colspan="2">
-                    <input id="newfile" type="file" onchange="setpath()" style="width:100%;">
-                </td>
-            </tr>
-            <tr>
-                <td>
-                    <label for="filepath">Set path on server</label>
-                </td>
-                <td>
-                    <input id="filepath" type="text" style="width:100%;">
-                </td>
-                <td>
-                    <button id="upload" type="button" onclick="upload()">Upload</button>
-                </td>
-            </tr>
-        </table>
-    </td></tr>
-</table>
-<script>
-function setpath() {
-    var fileserverpraefix = "/fileserver";
-    var anz_zeichen_fileserver = fileserverpraefix.length;
-    var default_path = window.location.pathname.substring(anz_zeichen_fileserver) + document.getElementById("newfile").files[0].name;
-    document.getElementById("filepath").value = default_path;
-}
-function upload() {
-    var filePath = document.getElementById("filepath").value;
-    var upload_path = "/upload/" + filePath;
-    var fileInput = document.getElementById("newfile").files;
-
-    /* Max size of an individual file. Make sure this
-     * value is same as that set in file_server.c */
-    var MAX_FILE_SIZE = 200*1024;
-    var MAX_FILE_SIZE_STR = "200KB";
-
-    if (fileInput.length == 0) {
-        alert("No file selected!");
-    } else if (filePath.length == 0) {
-        alert("File path on server is not set!");
-    } else if (filePath.indexOf(' ') >= 0) {
-        alert("File path on server cannot have spaces!");
-    } else if (filePath[filePath.length-1] == '/') {
-        alert("File name not specified after path!");
-    } else if (fileInput[0].size > 200*1024) {
-        alert("File size must be less than 200KB!");
-    } else {
-        document.getElementById("newfile").disabled = true;
-        document.getElementById("filepath").disabled = true;
-        document.getElementById("upload").disabled = true;
-
-        var file = fileInput[0];
-        var xhttp = new XMLHttpRequest();
-        xhttp.onreadystatechange = function() {
-            if (xhttp.readyState == 4) {
-                if (xhttp.status == 200) {
-                    document.open();
-                    document.write(xhttp.responseText);
-                    document.close();
-                } else if (xhttp.status == 0) {
-                    alert("Server closed the connection abruptly!");
-                    location.reload()
-                } else {
-                    alert(xhttp.status + " Error!\n" + xhttp.responseText);
-                    location.reload()
-                }
-            }
-        };
-        xhttp.open("POST", upload_path, true);
-        xhttp.send(file);
-    }
-}
-</script>

+ 32 - 0
sd-card/html/upload_script.html

@@ -52,6 +52,38 @@ function dirup() {
 	window.location.href = res;	
 }
 
+function deleteall(){
+    var str = window.location.href;
+//    str = str.substring(0, str.length-1);
+//	str = str.substring(10, str.length);
+	str = str.replace("/fileserver/", "/delete/");
+	str = str + "?task=deldircontent";
+	if (confirm("This will delete ALL files in this directory!!!\n\nAre you sure?")) {
+		alert(str);
+	
+        var xhttp = new XMLHttpRequest();
+        xhttp.onreadystatechange = function() {
+            if (xhttp.readyState == 4) {
+                if (xhttp.status == 200) {
+//                    document.open();
+//                    document.write(xhttp.responseText);
+//                    document.close();
+                    location.reload();
+                } else if (xhttp.status == 0) {
+                    alert("Server closed the connection abruptly!");
+                    location.reload();
+                } else {
+//                    alert(xhttp.status + " Error!\n" + xhttp.responseText);
+                    location.reload();
+                }
+            }
+        };
+        xhttp.open("POST", str, true);
+        xhttp.send();
+
+	}
+}
+
 function upload() {
     var filePath = document.getElementById("filepath").value;
     var upload_path = "/upload/" + filePath;