implemented spiffs with http web access
This commit is contained in:
parent
00a60102b4
commit
c1f4678cdb
|
|
@ -103,7 +103,7 @@ static const char* get_path_from_uri(char *dest, const char *base_path,
|
||||||
|
|
||||||
/* Construct full path (base + path) */
|
/* Construct full path (base + path) */
|
||||||
strcpy(dest, base_path);
|
strcpy(dest, base_path);
|
||||||
strlcpy(dest + base_pathlen, uri, pathlen + 1);
|
strlcpy(dest + base_pathlen, uri + (sizeof("/files")-1), pathlen + 1 - (sizeof("/files")-1));
|
||||||
|
|
||||||
/* Return pointer to path, skipping the base */
|
/* Return pointer to path, skipping the base */
|
||||||
return dest + base_pathlen;
|
return dest + base_pathlen;
|
||||||
|
|
@ -150,8 +150,8 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath)
|
||||||
/* Send file-list table definition and column labels */
|
/* Send file-list table definition and column labels */
|
||||||
httpd_resp_sendstr_chunk(
|
httpd_resp_sendstr_chunk(
|
||||||
req,
|
req,
|
||||||
"<table class=\"fixed\" border=\"1\">"
|
"<table class=\"fixed\" border=\"0\">"
|
||||||
"<col width=\"800px\" /><col width=\"300px\" /><col width=\"300px\" /><col width=\"100px\" />"
|
"<col width=\"600px\" /><col width=\"200px\" /><col width=\"200px\" /><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</th></tr></thead>"
|
||||||
"<tbody>");
|
"<tbody>");
|
||||||
|
|
||||||
|
|
@ -184,8 +184,8 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath)
|
||||||
httpd_resp_sendstr_chunk(req, "</td><td>");
|
httpd_resp_sendstr_chunk(req, "</td><td>");
|
||||||
httpd_resp_sendstr_chunk(req, entrysize);
|
httpd_resp_sendstr_chunk(req, entrysize);
|
||||||
httpd_resp_sendstr_chunk(req, "</td><td>");
|
httpd_resp_sendstr_chunk(req, "</td><td>");
|
||||||
httpd_resp_sendstr_chunk(req, "<form method=\"post\" action=\"/delete");
|
httpd_resp_sendstr_chunk(req, "<form method=\"post\" action=\"/files/delete/");
|
||||||
httpd_resp_sendstr_chunk(req, req->uri);
|
//httpd_resp_sendstr_chunk(req, req->uri);
|
||||||
httpd_resp_sendstr_chunk(req, entry->d_name);
|
httpd_resp_sendstr_chunk(req, entry->d_name);
|
||||||
httpd_resp_sendstr_chunk(req, "\"><button type=\"submit\">Delete</button></form>");
|
httpd_resp_sendstr_chunk(req, "\"><button type=\"submit\">Delete</button></form>");
|
||||||
httpd_resp_sendstr_chunk(req, "</td></tr>\n");
|
httpd_resp_sendstr_chunk(req, "</td></tr>\n");
|
||||||
|
|
@ -228,7 +228,7 @@ esp_err_t download_get_handler(httpd_req_t *req)
|
||||||
/* If name has trailing '/', respond with directory contents */
|
/* If name has trailing '/', respond with directory contents */
|
||||||
if (filename[strlen(filename) - 1] == '/')
|
if (filename[strlen(filename) - 1] == '/')
|
||||||
{
|
{
|
||||||
return http_resp_dir_html(req, "/data/");
|
return http_resp_dir_html(req, filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stat(filepath, &file_stat) == -1)
|
if (stat(filepath, &file_stat) == -1)
|
||||||
|
|
@ -241,8 +241,6 @@ esp_err_t download_get_handler(httpd_req_t *req)
|
||||||
return index_html_get_handler(req);
|
return index_html_get_handler(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ESP_LOGE(TAG, "Failed to stat file : %s", filepath);
|
ESP_LOGE(TAG, "Failed to stat file : %s", filepath);
|
||||||
/* Respond with 404 Not Found */
|
/* Respond with 404 Not Found */
|
||||||
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
|
||||||
|
|
@ -310,7 +308,7 @@ esp_err_t upload_post_handler(httpd_req_t *req)
|
||||||
/* Skip leading "/upload" from URI to get filename */
|
/* Skip leading "/upload" from URI to get filename */
|
||||||
/* Note sizeof() counts NULL termination hence the -1 */
|
/* Note sizeof() counts NULL termination hence the -1 */
|
||||||
const char *filename = get_path_from_uri(filepath, ((struct file_server_data*) req->user_ctx)->base_path2,
|
const char *filename = get_path_from_uri(filepath, ((struct file_server_data*) req->user_ctx)->base_path2,
|
||||||
req->uri + sizeof("/files/upload") - 1,
|
req->uri + sizeof("/upload") - 1,
|
||||||
sizeof(filepath));
|
sizeof(filepath));
|
||||||
ESP_LOGW(TAG, "FILE_POST_URI %s", req->uri);
|
ESP_LOGW(TAG, "FILE_POST_URI %s", req->uri);
|
||||||
ESP_LOGW(TAG, "FILE_POST_FILEPATH %s", filepath);
|
ESP_LOGW(TAG, "FILE_POST_FILEPATH %s", filepath);
|
||||||
|
|
@ -420,7 +418,7 @@ esp_err_t upload_post_handler(httpd_req_t *req)
|
||||||
|
|
||||||
/* Redirect onto root to see the updated file list */
|
/* Redirect onto root to see the updated file list */
|
||||||
httpd_resp_set_status(req, "303 See Other");
|
httpd_resp_set_status(req, "303 See Other");
|
||||||
httpd_resp_set_hdr(req, "Location", "/");
|
httpd_resp_set_hdr(req, "Location", "/files/");
|
||||||
#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER
|
#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER
|
||||||
httpd_resp_set_hdr(req, "Connection", "close");
|
httpd_resp_set_hdr(req, "Connection", "close");
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -468,7 +466,7 @@ esp_err_t delete_post_handler(httpd_req_t *req)
|
||||||
|
|
||||||
/* Redirect onto root to see the updated file list */
|
/* Redirect onto root to see the updated file list */
|
||||||
httpd_resp_set_status(req, "303 See Other");
|
httpd_resp_set_status(req, "303 See Other");
|
||||||
httpd_resp_set_hdr(req, "Location", "/");
|
httpd_resp_set_hdr(req, "Location", "/files/");
|
||||||
#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER
|
#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER
|
||||||
httpd_resp_set_hdr(req, "Connection", "close");
|
httpd_resp_set_hdr(req, "Connection", "close");
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -173,10 +173,11 @@ static esp_err_t POSTHandler(httpd_req_t *req)
|
||||||
ESP_LOGI(TAG, "POST request handle");
|
ESP_LOGI(TAG, "POST request handle");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (true)
|
if (memmem(req->uri, strlen(req->uri), "/files/upload/", sizeof("/files/upload/")-1))
|
||||||
{
|
|
||||||
return upload_post_handler(req);
|
return upload_post_handler(req);
|
||||||
}
|
|
||||||
|
if (memmem(req->uri, strlen(req->uri), "/files/delete/", sizeof("/files/delete/")-1))
|
||||||
|
return delete_post_handler(req);
|
||||||
|
|
||||||
char *buf = ((struct file_server_data*) req->user_ctx)->scratch;
|
char *buf = ((struct file_server_data*) req->user_ctx)->scratch;
|
||||||
int received;
|
int received;
|
||||||
|
|
@ -261,12 +262,9 @@ static esp_err_t GETHandler(httpd_req_t *req)
|
||||||
ESP_LOGI(TAG, "GET request handle");
|
ESP_LOGI(TAG, "GET request handle");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ESP_LOGW(TAG, "URI %s", req->uri);
|
//Route to file server GET handler
|
||||||
//if (!strcmp(req->uri, "/files/"))
|
if (memmem(req->uri, strlen(req->uri), "/files/", sizeof("/files/") - 1))
|
||||||
if(memmem(req->uri, strlen(req->uri), "/files/", 7))
|
|
||||||
{
|
|
||||||
return download_get_handler(req);
|
return download_get_handler(req);
|
||||||
}
|
|
||||||
|
|
||||||
char filepath[FILE_PATH_MAX];
|
char filepath[FILE_PATH_MAX];
|
||||||
espfs_file_t *file;
|
espfs_file_t *file;
|
||||||
|
|
@ -275,9 +273,6 @@ static esp_err_t GETHandler(httpd_req_t *req)
|
||||||
uint32_t bufSize; //size of ram buffer for chunk of data, read from file
|
uint32_t bufSize; //size of ram buffer for chunk of data, read from file
|
||||||
uint32_t readBytes; //number of bytes, read from file. used for information only
|
uint32_t readBytes; //number of bytes, read from file. used for information only
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const char *filename = get_path_from_uri(filepath,
|
const char *filename = get_path_from_uri(filepath,
|
||||||
((struct file_server_data*) req->user_ctx)->base_path,
|
((struct file_server_data*) req->user_ctx)->base_path,
|
||||||
req->uri,
|
req->uri,
|
||||||
|
|
@ -291,8 +286,6 @@ static esp_err_t GETHandler(httpd_req_t *req)
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Redirect request to /index.html */
|
/* Redirect request to /index.html */
|
||||||
if (filename[strlen(filename) - 1] == '/')
|
if (filename[strlen(filename) - 1] == '/')
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<table class="fixed" border="0">
|
<table class="fixed" border="0">
|
||||||
<col width="1000px" /><col width="500px" />
|
<col width="700px" /><col width="500px" />
|
||||||
<tr><td>
|
<tr><td>
|
||||||
<h2>ESP32 File Server</h2>
|
<h4>ESP32 File Server</h4>
|
||||||
</td><td>
|
</td><td>
|
||||||
<table border="0">
|
<table border="0">
|
||||||
<tr>
|
<tr>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user