Skip to content

Commit

Permalink
Refactor file handling API and add endpoint to delete files
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Oct 19, 2024
1 parent aa9f36e commit 1690148
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
7 changes: 4 additions & 3 deletions include/WebApi_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ enum WebApiError {
DtuInvalidCmtFrequency,
DtuInvalidCmtCountry,

ConfigBase = 3000,
ConfigNotDeleted,
ConfigSuccess,
FileBase = 3000,
FileNotDeleted,
FileSuccess,
FileDeleteSuccess,

InverterBase = 4000,
InverterSerialZero,
Expand Down
1 change: 1 addition & 0 deletions include/WebApi_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class WebApiFileClass {
private:
void onFileGet(AsyncWebServerRequest* request);
void onFileDelete(AsyncWebServerRequest* request);
void onFileDeleteAll(AsyncWebServerRequest* request);
void onFileListGet(AsyncWebServerRequest* request);
void onFileUploadFinish(AsyncWebServerRequest* request);
void onFileUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final);
Expand Down
108 changes: 73 additions & 35 deletions src/WebApi_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,40 @@ void WebApiFileClass::init(AsyncWebServer& server, Scheduler& scheduler)

server.on("/api/file/get", HTTP_GET, std::bind(&WebApiFileClass::onFileGet, this, _1));
server.on("/api/file/delete", HTTP_POST, std::bind(&WebApiFileClass::onFileDelete, this, _1));
server.on("/api/file/delete_all", HTTP_POST, std::bind(&WebApiFileClass::onFileDeleteAll, this, _1));
server.on("/api/file/list", HTTP_GET, std::bind(&WebApiFileClass::onFileListGet, this, _1));
server.on("/api/file/upload", HTTP_POST,
std::bind(&WebApiFileClass::onFileUploadFinish, this, _1),
std::bind(&WebApiFileClass::onFileUpload, this, _1, _2, _3, _4, _5, _6));
}

void WebApiFileClass::onFileListGet(AsyncWebServerRequest* request)
{
if (!WebApi.checkCredentials(request)) {
return;
}

AsyncJsonResponse* response = new AsyncJsonResponse();
auto& root = response->getRoot();
auto data = root.to<JsonArray>();

File rootfs = LittleFS.open("/");
File file = rootfs.openNextFile();
while (file) {
if (file.isDirectory()) {
continue;
}
JsonObject obj = data.add<JsonObject>();
obj["name"] = String(file.name());
obj["size"] = file.size();

file = rootfs.openNextFile();
}
file.close();

WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
}

void WebApiFileClass::onFileGet(AsyncWebServerRequest* request)
{
if (!WebApi.checkCredentials(request)) {
Expand Down Expand Up @@ -62,69 +90,63 @@ void WebApiFileClass::onFileDelete(AsyncWebServerRequest* request)

auto& retMsg = response->getRoot();

if (!(root["delete"].is<bool>())) {
if (!(root["file"].is<String>())) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
return;
}

if (root["delete"].as<bool>() == false) {
retMsg["message"] = "Not deleted anything!";
retMsg["code"] = WebApiError::ConfigNotDeleted;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
String name = "/" + root["file"].as<String>();
if (!LittleFS.exists(name)) {
request->send(404);
return;
}

LittleFS.remove(name);

retMsg["type"] = "success";
retMsg["message"] = "Configuration resettet. Rebooting now...";
retMsg["code"] = WebApiError::ConfigSuccess;
retMsg["message"] = "File deleted";
retMsg["code"] = WebApiError::FileDeleteSuccess;

WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);

Utils::removeAllFiles();
RestartHelper.triggerRestart();
}

void WebApiFileClass::onFileListGet(AsyncWebServerRequest* request)
void WebApiFileClass::onFileDeleteAll(AsyncWebServerRequest* request)
{
if (!WebApi.checkCredentials(request)) {
return;
}

AsyncJsonResponse* response = new AsyncJsonResponse();
auto& root = response->getRoot();
auto data = root["configs"].to<JsonArray>();
JsonDocument root;
if (!WebApi.parseRequestData(request, response, root)) {
return;
}

File rootfs = LittleFS.open("/");
File file = rootfs.openNextFile();
while (file) {
if (file.isDirectory()) {
continue;
}
JsonObject obj = data.add<JsonObject>();
obj["name"] = String(file.name());
auto& retMsg = response->getRoot();

file = rootfs.openNextFile();
if (!(root["delete"].is<bool>())) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
return;
}
file.close();

WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
}

void WebApiFileClass::onFileUploadFinish(AsyncWebServerRequest* request)
{
if (!WebApi.checkCredentials(request)) {
if (root["delete"].as<bool>() == false) {
retMsg["message"] = "Not deleted anything!";
retMsg["code"] = WebApiError::FileNotDeleted;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
return;
}

// the request handler is triggered after the upload has finished...
// create the response, add header, and send response
retMsg["type"] = "success";
retMsg["message"] = "Configuration resettet. Rebooting now...";
retMsg["code"] = WebApiError::FileSuccess;

AsyncWebServerResponse* response = request->beginResponse(200, "text/plain", "OK");
response->addHeader("Connection", "close");
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);

Utils::removeAllFiles();
RestartHelper.triggerRestart();
}

Expand Down Expand Up @@ -154,3 +176,19 @@ void WebApiFileClass::onFileUpload(AsyncWebServerRequest* request, String filena
request->_tempFile.close();
}
}

void WebApiFileClass::onFileUploadFinish(AsyncWebServerRequest* request)
{
if (!WebApi.checkCredentials(request)) {
return;
}

// the request handler is triggered after the upload has finished...
// create the response, add header, and send response

AsyncWebServerResponse* response = request->beginResponse(200, "text/plain", "OK");
response->addHeader("Connection", "close");
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
RestartHelper.triggerRestart();
}

0 comments on commit 1690148

Please sign in to comment.