Gz html files fix missing file server.html (#3582)

* Add files via upload

* Delete sd-card/html/file_server.html

* Update server_file.cpp

* Update server_file.cpp

---------

Co-authored-by: SybexX <Heinrich-Tuning@web.de>
This commit is contained in:
CaCO3
2025-02-24 21:55:16 +01:00
committed by GitHub
parent ec639d4236
commit 8f5bf209d9
4 changed files with 203 additions and 289 deletions

View File

@@ -6,11 +6,8 @@
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "server_file.h"
#include <stdio.h>
#include <string.h>
#include <string>
@@ -58,7 +55,6 @@ struct file_server_data {
char scratch[SERVER_FILER_SCRATCH_BUFSIZE];
};
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
@@ -67,11 +63,9 @@ using namespace std;
string SUFFIX_ZW = "_0xge";
static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file);
static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file);
esp_err_t get_numbers_file_handler(httpd_req_t *req)
{
std::string ret = flowctrl.getNumbersName();
@@ -87,7 +81,6 @@ esp_err_t get_numbers_file_handler(httpd_req_t *req)
return ESP_OK;
}
esp_err_t get_data_file_handler(httpd_req_t *req)
{
struct dirent *entry;
@@ -131,7 +124,6 @@ esp_err_t get_data_file_handler(httpd_req_t *req)
return ESP_OK;
}
esp_err_t get_tflite_file_handler(httpd_req_t *req)
{
struct dirent *entry;
@@ -175,12 +167,11 @@ esp_err_t get_tflite_file_handler(httpd_req_t *req)
return ESP_OK;
}
/* Send HTTP response with a run-time generated html consisting of
* a list of all files and folders under the requested path.
* In case of SPIFFS this returns empty list when path is any
* string other than '/', since SPIFFS doesn't support directories */
static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const char* uripath, bool readonly)
static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const char *uripath, bool readonly)
{
char entrypath[FILE_PATH_MAX];
char entrysize[16];
@@ -192,82 +183,85 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const
char dirpath_corrected[FILE_PATH_MAX];
strcpy(dirpath_corrected, dirpath);
file_server_data * server_data = (file_server_data *) req->user_ctx;
if ((strlen(dirpath_corrected)-1) > strlen(server_data->base_path)) // if dirpath is not mountpoint, the last "\" needs to be removed
dirpath_corrected[strlen(dirpath_corrected)-1] = '\0';
file_server_data *server_data = (file_server_data *)req->user_ctx;
DIR *dir = opendir(dirpath_corrected);
if ((strlen(dirpath_corrected) - 1) > strlen(server_data->base_path)) {
// if dirpath is not mountpoint, the last "\" needs to be removed
dirpath_corrected[strlen(dirpath_corrected) - 1] = '\0';
}
DIR *pdir = opendir(dirpath_corrected);
const size_t dirpath_len = strlen(dirpath);
ESP_LOGD(TAG, "Dirpath: <%s>, Pathlength: %d", dirpath, dirpath_len);
/* Retrieve the base path of file storage to construct the full path */
// Retrieve the base path of file storage to construct the full path
strlcpy(entrypath, dirpath, sizeof(entrypath));
ESP_LOGD(TAG, "entrypath: <%s>", entrypath);
if (!dir) {
if (!pdir) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to stat dir: " + std::string(dirpath) + "!");
/* Respond with 404 Not Found */
// Respond with 404 Not Found
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL;
}
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
/* Send HTML file header */
httpd_resp_sendstr_chunk(req, "<!DOCTYPE html><html><body>");
// Send HTML file header
httpd_resp_sendstr_chunk(req, "<!DOCTYPE html><html lang=\"en\" xml:lang=\"en\"><head>");
httpd_resp_sendstr_chunk(req, "<link href=\"/file_server.css\" rel=\"stylesheet\">");
httpd_resp_sendstr_chunk(req, "<link href=\"/firework.css\" rel=\"stylesheet\">");
httpd_resp_sendstr_chunk(req, "<script type=\"text/javascript\" src=\"/jquery-3.6.0.min.js\"></script>");
httpd_resp_sendstr_chunk(req, "<script type=\"text/javascript\" src=\"/firework.js\"></script></head>");
/////////////////////////////////////////////////
if (!readonly) {
FILE *fd = fopen("/sdcard/html/file_server.html", "r");
char *chunk = ((struct file_server_data *)req->user_ctx)->scratch;
size_t chunksize;
do {
chunksize = fread(chunk, 1, SERVER_FILER_SCRATCH_BUFSIZE, fd);
// ESP_LOGD(TAG, "Chunksize %d", chunksize);
if (chunksize > 0){
if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
fclose(fd);
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File sending failed!");
return ESP_FAIL;
}
}
} while (chunksize != 0);
fclose(fd);
// ESP_LOGI(TAG, "File sending complete");
}
///////////////////////////////
httpd_resp_sendstr_chunk(req, "<body>");
httpd_resp_sendstr_chunk(req, "<table class=\"fixed\" border=\"0\" width=100% style=\"font-family: arial\">");
httpd_resp_sendstr_chunk(req, "<tr><td style=\"vertical-align: top;width: 300px;\"><h2>Fileserver</h2></td>"
"<td rowspan=\"2\"><table border=\"0\" style=\"width:100%\"><tr><td style=\"width:80px\">"
"<label for=\"newfile\">Source</label></td><td colspan=\"2\">"
"<input id=\"newfile\" type=\"file\" onchange=\"setpath()\" style=\"width:100%;\"></td></tr>"
"<tr><td><label for=\"filepath\">Destination</label></td><td>"
"<input id=\"filepath\" type=\"text\" style=\"width:94%;\"></td><td>"
"<button id=\"upload\" type=\"button\" class=\"button\" onclick=\"upload()\">Upload</button></td></tr>"
"</table></td></tr><tr></tr><tr><td colspan=\"2\">"
"<button style=\"font-size:16px; padding: 5px 10px\" id=\"dirup\" type=\"button\" onclick=\"dirup()\""
"disabled>&#129145; Directory up</button><span style=\"padding-left:15px\" id=\"currentpath\">"
"</span></td></tr>");
httpd_resp_sendstr_chunk(req, "</table>");
httpd_resp_sendstr_chunk(req, "<script type=\"text/javascript\" src=\"/file_server.js\"></script>");
httpd_resp_sendstr_chunk(req, "<script type=\"text/javascript\">initFileServer();</script>");
std::string _zw = std::string(dirpath);
_zw = _zw.substr(8, _zw.length() - 8);
_zw = "/delete/" + _zw + "?task=deldircontent";
_zw = "/delete/" + _zw + "?task=deldircontent";
// Send file-list table definition and column labels
httpd_resp_sendstr_chunk(req, "<table id=\"files_table\">"
"<col width=\"800px\"><col width=\"300px\"><col width=\"300px\"><col width=\"100px\">"
"<thead><tr><th>Name</th><th>Type</th><th>Size</th>");
/* Send file-list table definition and column labels */
httpd_resp_sendstr_chunk(req,
"<table id=\"files_table\">"
"<col width=\"800px\" /><col width=\"300px\" /><col width=\"300px\" /><col width=\"100px\" />"
"<thead><tr><th>Name</th><th>Type</th><th>Size</th>");
if (!readonly) {
httpd_resp_sendstr_chunk(req, "<th>"
"<form method=\"post\" action=\"");
httpd_resp_sendstr_chunk(req, "<th><form method=\"post\" action=\"");
httpd_resp_sendstr_chunk(req, _zw.c_str());
httpd_resp_sendstr_chunk(req,
"\"><button type=\"submit\">DELETE ALL!</button></form>"
"</th></tr>");
httpd_resp_sendstr_chunk(req, "\"><button type=\"submit\">DELETE ALL!</button></form></th></tr>");
}
httpd_resp_sendstr_chunk(req, "</thead><tbody>\n");
/* Iterate over all files / folders and fetch their names and sizes */
while ((entry = readdir(dir)) != NULL) {
if (strcmp("wlan.ini", entry->d_name) != 0 ) // wlan.ini soll nicht angezeigt werden!
{
// Iterate over all files / folders and fetch their names and sizes
while ((entry = readdir(pdir)) != NULL) {
// wlan.ini soll nicht angezeigt werden!
if (strcmp("wlan.ini", entry->d_name) != 0) {
entrytype = (entry->d_type == DT_DIR ? "directory" : "file");
strlcpy(entrypath + dirpath_len, entry->d_name, sizeof(entrypath) - dirpath_len);
ESP_LOGD(TAG, "Entrypath: %s", entrypath);
if (stat(entrypath, &entry_stat) == -1) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to stat " + string(entrytype) + ": " + string(entry->d_name));
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to stat " + std::string(entrytype) + ": " + std::string(entry->d_name));
continue;
}
@@ -283,22 +277,25 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const
}
}
ESP_LOGI(TAG, "Found %s: %s (%s bytes)", entrytype, entry->d_name, entrysize);
ESP_LOGD(TAG, "Found %s: %s (%s bytes)", entrytype, entry->d_name, entrysize);
/* Send chunk of HTML file containing table entries with file name and size */
// Send chunk of HTML file containing table entries with file name and size
httpd_resp_sendstr_chunk(req, "<tr><td><a href=\"");
httpd_resp_sendstr_chunk(req, "/fileserver");
httpd_resp_sendstr_chunk(req, uripath);
httpd_resp_sendstr_chunk(req, entry->d_name);
if (entry->d_type == DT_DIR) {
httpd_resp_sendstr_chunk(req, "/");
}
httpd_resp_sendstr_chunk(req, "\">");
httpd_resp_sendstr_chunk(req, entry->d_name);
httpd_resp_sendstr_chunk(req, "</a></td><td>");
httpd_resp_sendstr_chunk(req, entrytype);
httpd_resp_sendstr_chunk(req, "</td><td>");
httpd_resp_sendstr_chunk(req, entrysize);
if (!readonly) {
httpd_resp_sendstr_chunk(req, "</td><td>");
httpd_resp_sendstr_chunk(req, "<form method=\"post\" action=\"/delete");
@@ -306,31 +303,28 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const
httpd_resp_sendstr_chunk(req, entry->d_name);
httpd_resp_sendstr_chunk(req, "\"><button type=\"submit\">Delete</button></form>");
}
httpd_resp_sendstr_chunk(req, "</td></tr>\n");
}
}
closedir(dir);
/* Finish the file list table */
closedir(pdir);
// Finish the file list table
httpd_resp_sendstr_chunk(req, "</tbody></table>");
/* Send remaining chunk of HTML file to complete it */
// Send remaining chunk of HTML file to complete it
httpd_resp_sendstr_chunk(req, "</body></html>");
/* Send empty chunk to signal HTTP response completion */
// Send empty chunk to signal HTTP response completion
httpd_resp_sendstr_chunk(req, NULL);
return ESP_OK;
}
/*
#define IS_FILE_EXT(filename, ext) \
(strcasecmp(&filename[strlen(filename) - sizeof(ext) + 1], ext) == 0)
*/
static esp_err_t logfileact_get_full_handler(httpd_req_t *req) {
return send_logfile(req, true);
}
static esp_err_t logfileact_get_last_part_handler(httpd_req_t *req) {
return send_logfile(req, false);
}
@@ -339,7 +333,6 @@ static esp_err_t datafileact_get_full_handler(httpd_req_t *req) {
return send_datafile(req, true);
}
static esp_err_t datafileact_get_last_part_handler(httpd_req_t *req) {
return send_datafile(req, false);
}
@@ -424,7 +417,6 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
return ESP_OK;
}
static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
{
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "log_get_last_part_handler");
@@ -510,7 +502,6 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
return ESP_OK;
}
/* Handler to download a file kept on the server */
static esp_err_t download_get_handler(httpd_req_t *req)
{
@@ -528,7 +519,6 @@ static esp_err_t download_get_handler(httpd_req_t *req)
// filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path,
// req->uri, sizeof(filepath));
if (!filename) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Filename is too long");
/* Respond with 414 Error */
@@ -759,7 +749,6 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
httpd_resp_set_hdr(req, "Location", directory.c_str());
httpd_resp_sendstr(req, "File uploaded successfully");
return ESP_OK;
}
@@ -770,7 +759,6 @@ static esp_err_t delete_post_handler(httpd_req_t *req)
char filepath[FILE_PATH_MAX];
struct stat file_stat;
//////////////////////////////////////////////////////////////
char _query[200];
char _valuechar[30];
@@ -893,13 +881,11 @@ static esp_err_t delete_post_handler(httpd_req_t *req)
}
}
httpd_resp_set_hdr(req, "Location", directory.c_str());
httpd_resp_sendstr(req, "File successfully deleted");
return ESP_OK;
}
void delete_all_in_directory(std::string _directory)
{
struct dirent *entry;
@@ -1137,8 +1123,6 @@ void unzip(std::string _in_zip_file, std::string _target_directory){
ESP_LOGD(TAG, "Success.");
}
void register_server_file_uri(httpd_handle_t server, const char *base_path)
{
static struct file_server_data *server_data = NULL;
@@ -1164,8 +1148,6 @@ void register_server_file_uri(httpd_handle_t server, const char *base_path)
strlcpy(server_data->base_path, base_path,
sizeof(server_data->base_path));
/* URI handler for getting uploaded files */
// char zw[sizeof(serverprefix)+1];
// strcpy(zw, serverprefix);
@@ -1180,7 +1162,6 @@ void register_server_file_uri(httpd_handle_t server, const char *base_path)
};
httpd_register_uri_handler(server, &file_download);
httpd_uri_t file_datafileact = {
.uri = "/datafileact", // Match all URIs of type /path/to/file
.method = HTTP_GET,
@@ -1189,7 +1170,6 @@ void register_server_file_uri(httpd_handle_t server, const char *base_path)
};
httpd_register_uri_handler(server, &file_datafileact);
httpd_uri_t file_datafile_last_part_handle = {
.uri = "/data", // Match all URIs of type /path/to/file
.method = HTTP_GET,
@@ -1206,7 +1186,6 @@ void register_server_file_uri(httpd_handle_t server, const char *base_path)
};
httpd_register_uri_handler(server, &file_logfileact);
httpd_uri_t file_logfile_last_part_handle = {
.uri = "/log", // Match all URIs of type /path/to/file
.method = HTTP_GET,
@@ -1215,7 +1194,6 @@ void register_server_file_uri(httpd_handle_t server, const char *base_path)
};
httpd_register_uri_handler(server, &file_logfile_last_part_handle);
/* URI handler for uploading files to server */
httpd_uri_t file_upload = {
.uri = "/upload/*", // Match all URIs of type /upload/path/to/file
@@ -1233,5 +1211,4 @@ void register_server_file_uri(httpd_handle_t server, const char *base_path)
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &file_delete);
}