Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new FS API endpoints #663

Merged
merged 7 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 145 additions & 35 deletions uCNC/src/hal/mcus/esp32/esp32_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ uint16_t bt_settings_offset;
#define OTA_URI "/firmware"
#endif

#ifndef FS_WRITE_URI
#define FS_WRITE_URI "/fs"
#endif
#define FS_WRITE_GZ_SIZE 305
const char fs_write_page[305] PROGMEM = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x55, 0x51, 0x3d, 0x4f, 0xc3, 0x30, 0x10, 0xdd, 0x91, 0xf8, 0x0f, 0x87, 0x67, 0x52, 0x43, 0x27, 0x84, 0xec, 0x2c, 0x85, 0x4a, 0x4c, 0x74, 0x68, 0x85, 0x18, 0x2f, 0xf6, 0xb5, 0xb1, 0xe4, 0xd8, 0x56, 0x72, 0x69, 0x55, 0x7e, 0x3d, 0x97, 0xa4, 0x03, 0x0c, 0xfe, 0x7a, 0xf7, 0xee, 0xdd, 0xd3, 0xb3, 0x79, 0x78, 0xfb, 0xdc, 0xec, 0xbf, 0x77, 0xef, 0xd0, 0x72, 0x17, 0x6b, 0x33, 0xed, 0x10, 0x31, 0x9d, 0xac, 0xa2, 0xa4, 0xe4, 0x4d, 0xe8, 0x6b, 0xd3, 0x11, 0x23, 0xb8, 0x16, 0xfb, 0x81, 0xd8, 0xaa, 0xc3, 0x7e, 0x5b, 0xbd, 0xa8, 0x1b, 0x9a, 0xb0, 0x23, 0xab, 0xce, 0x81, 0x2e, 0x25,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xe7, 0xdf, 0xba, 0xbf, 0xfb, 0x05, 0x44, 0x67, 0x16, 0x56, 0xbf, 0x01, 0x00, 0x00};

WebServer web_server(WEBSERVER_PORT);
HTTPUpdateServer httpUpdater;
const char *update_path = OTA_URI;
Expand Down Expand Up @@ -415,30 +409,35 @@ extern "C"
#include <SPIFFS.h>
#define FLASH_FS SPIFFS
#endif
static File upload_file;

void fs_file_updater()
{
if (web_server.uri() != FS_WRITE_URI || web_server.method() != HTTP_POST || !web_server.hasArg("update"))
static File upload_file;
if (!web_server.uri().startsWith(FS_URI) || (web_server.method() != HTTP_POST && web_server.method() != HTTP_PUT))
{
return;
}

String urlpath = String((web_server.uri().substring(FS_URI_LEN).length() != 0) ? web_server.uri().substring(FS_URI_LEN) : "/");

if (!FLASH_FS.exists(urlpath))
{
return;
}

HTTPUpload &upload = web_server.upload();
if (upload.status == UPLOAD_FILE_START)
{
String filename = upload.filename;
if (web_server.hasArg("path"))
{
String path = web_server.arg("path");
filename = path + ((!filename.startsWith("/") && !path.startsWith("/")) ? "/" : "") + filename;
}
if (!filename.startsWith("/"))
if (web_server.method() == HTTP_POST)
{
filename = "/" + filename;
if (!urlpath.endsWith("/"))
{
urlpath.concat("/");
}

urlpath.concat(upload.filename);
}
upload_file = FLASH_FS.open(filename, "w");
filename = String();
upload_file = FLASH_FS.open(urlpath, "w");
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
Expand All @@ -456,6 +455,101 @@ extern "C"
}
}

void fs_file_browser()
{
File fp;
char path[256];

// updated page
if (web_server.hasArg("update") && web_server.method() == HTTP_GET)
{
web_server.sendHeader("Content-Encoding", "gzip");
web_server.send_P(200, __romstr__("text/html"), fs_write_page, FS_WRITE_GZ_SIZE);
return;
}

String urlpath = String((web_server.uri().substring(FS_URI_LEN).length() != 0) ? web_server.uri().substring(FS_URI_LEN) : "/");

if (!FLASH_FS.exists(urlpath))
{
endpoint_send(404, "application/json", "{\"result\":\"notfound\"}");
return;
}

fp = FLASH_FS.open(urlpath, "r");

switch (web_server.method())
{
case HTTP_DELETE:
if (fp.isDirectory())
{
FLASH_FS.rmdir(urlpath);
}
else
{
FLASH_FS.remove(urlpath);
}
__FALL_THROUGH__
case HTTP_PUT:
case HTTP_POST:
if (web_server.hasArg("redirect"))
{
memset(path, 0, 256);
web_server.sendHeader("Location", web_server.arg("redirect"));
sprintf(path, "{\"redirect\":\"%s\"}", web_server.arg("redirect").c_str());
web_server.send(303, "application/json", path);
}
else
{
endpoint_send(200, "application/json", "{\"result\":\"ok\"}");
}

break;
default: // handle as get
if (fp.isDirectory())
{
// start chunck transmition;
endpoint_request_uri(path, 256);
endpoint_send(200, "application/json", NULL);
endpoint_send(200, "application/json", "{\"result\":\"ok\",\"path\":\"");
endpoint_send(200, "application/json", path);
endpoint_send(200, "application/json", "\",\"data\":[");
File file = fp.openNextFile();

while (file)
{
memset(path, 0, 256);
if (file.isDirectory())
{
sprintf(path, "{\"type\":\"dir\",\"name\":\"%s\",\"attr\":%d},", file.name(), 0);
}
else
{
sprintf(path, "{\"type\":\"file\",\"name\":\"%s\",\"attr\":0,\"size\":%lu,\"date\":0}", file.name(), (unsigned long int)file.size());
}

file = fp.openNextFile();
if (file)
{
// trailling comma
path[strlen(path)] = ',';
}
endpoint_send(200, "application/json", path);
}
endpoint_send(200, "application/json", "]}\n");
// close the stream
endpoint_send(200, "application/json", "");
}
else
{
web_server.streamFile(fp, "application/octet-stream");
}
break;
}

fp.close();
}

void endpoint_add(const char *uri, uint8_t method, endpoint_delegate request_handler, endpoint_delegate file_handler)
{
if (!method)
Expand Down Expand Up @@ -539,6 +633,36 @@ extern "C"
return false;
}

endpoint_upload_t endpoint_file_upload_status(void)
{
HTTPUpload &upload = web_server.upload();
endpoint_upload_t status = {.status=(uint8_t)upload.status, .data = upload.buf, .datalen = upload.currentSize};
return status;
}

uint8_t endpoint_request_method(void)
{
switch (web_server.method())
{
case HTTP_GET:
return ENDPOINT_GET;
case HTTP_POST:
return ENDPOINT_POST;
case HTTP_PUT:
return ENDPOINT_PUT;
case HTTP_DELETE:
return ENDPOINT_DELETE;
default:
return (ENDPOINT_OTHER | (uint8_t)web_server.method());
}
}

void endpoint_file_upload_name(char *filename, size_t maxlen)
{
HTTPUpload &upload = web_server.upload();
strncpy(filename, upload.filename.c_str(), maxlen);
}

#endif

#if defined(ENABLE_WIFI) && defined(MCU_HAS_WEBSOCKETS)
Expand Down Expand Up @@ -640,23 +764,9 @@ extern "C"
#ifndef CUSTOM_OTA_ENDPOINT
httpUpdater.setup(&web_server, update_path, update_username, update_password);
#endif
FLASH_FS.begin();
web_server.on(
FS_WRITE_URI, HTTP_GET, []()
{ web_server.sendHeader("Content-Encoding", "gzip");
web_server.send_P(200, __romstr__("text/html"), fs_write_page, FS_WRITE_GZ_SIZE); },
NULL);
web_server.on(
FS_WRITE_URI, HTTP_POST, []()
{
if(web_server.hasArg("redirect")){
web_server.sendHeader("Location", web_server.arg("redirect"));
web_server.send(303);
}
else{
web_server.send(200, "text/plain", "");
} },
fs_file_updater);
FLASH_FS.begin(true, FS_URI);
endpoint_add(FS_URI, HTTP_ANY, fs_file_browser, fs_file_updater);
endpoint_add(FS_URI "/*", HTTP_ANY, fs_file_browser, fs_file_updater);
web_server.begin();
#ifdef MCU_HAS_WEBSOCKETS
socket_server.begin();
Expand Down
Loading
Loading