📂 Adds filesystem service
This commit is contained in:
@@ -52,7 +52,8 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd
|
||||
#endif
|
||||
_restartService(server, &_securitySettingsService),
|
||||
_factoryResetService(server, &ESPFS, &_securitySettingsService),
|
||||
_systemStatus(server, &_securitySettingsService) {
|
||||
_systemStatus(server, &_securitySettingsService),
|
||||
_fileExplorer(server, &_securitySettingsService){
|
||||
}
|
||||
|
||||
void ESP32SvelteKit::begin() {
|
||||
@@ -178,8 +179,8 @@ void ESP32SvelteKit::startServices() {
|
||||
#if FT_ENABLED(FT_BATTERY)
|
||||
_batteryService.begin();
|
||||
#endif
|
||||
|
||||
_taskManager.begin();
|
||||
_fileExplorer.begin();
|
||||
}
|
||||
|
||||
void ESP32SvelteKit::_loop() {
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <AnalyticsService.h>
|
||||
#include <AuthenticationService.h>
|
||||
#include <BatteryService.h>
|
||||
#include <FileExplorerService.h>
|
||||
#include <DownloadFirmwareService.h>
|
||||
#include <ESPFS.h>
|
||||
#include <ESPmDNS.h>
|
||||
@@ -153,6 +154,11 @@ public:
|
||||
return &_taskManager;
|
||||
}
|
||||
|
||||
FileExplorer *getFileExplorer()
|
||||
{
|
||||
return &_fileExplorer;
|
||||
}
|
||||
|
||||
void factoryReset()
|
||||
{
|
||||
_factoryResetService.factoryReset();
|
||||
@@ -209,6 +215,7 @@ private:
|
||||
FactoryResetService _factoryResetService;
|
||||
SystemStatus _systemStatus;
|
||||
TaskManager _taskManager;
|
||||
FileExplorer _fileExplorer;
|
||||
|
||||
String _appName = APP_NAME;
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#ifndef FileExplorer_h
|
||||
#define FileExplorer_h
|
||||
|
||||
#include <ESPFS.h>
|
||||
#include <PsychicHttp.h>
|
||||
#include <SecurityManager.h>
|
||||
|
||||
#define FILE_EXPLORER_SERVICE_PATH "/api/files/list"
|
||||
|
||||
class FileExplorer
|
||||
{
|
||||
public:
|
||||
FileExplorer(PsychicHttpServer *server, SecurityManager *securityManager)
|
||||
: _server(server), _securityManager(securityManager)
|
||||
{
|
||||
}
|
||||
|
||||
void begin()
|
||||
{
|
||||
_server->on(FILE_EXPLORER_SERVICE_PATH, HTTP_GET,
|
||||
_securityManager->wrapRequest(std::bind(&FileExplorer::explore, this, std::placeholders::_1),
|
||||
AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
|
||||
ESP_LOGV("APStatus", "Registered GET endpoint: %s", FILE_EXPLORER_SERVICE_PATH);
|
||||
}
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
SecurityManager *_securityManager;
|
||||
esp_err_t explore(PsychicRequest *request)
|
||||
{
|
||||
return request->reply(200, "application/json", listFiles("/").c_str());
|
||||
}
|
||||
|
||||
String listFiles(const String &directory, bool isRoot = true)
|
||||
{
|
||||
File root = ESPFS.open(directory.startsWith("/") ? directory : "/" + directory);
|
||||
if (!root.isDirectory())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
File file = root.openNextFile();
|
||||
String output = isRoot ? "{ \"root\": {" : "{";
|
||||
|
||||
while (file)
|
||||
{
|
||||
if (file.isDirectory())
|
||||
{
|
||||
output += "\"" + String(file.name()) + "\": " + listFiles(file.name(), false) + ", ";
|
||||
}
|
||||
else
|
||||
{
|
||||
output += "\"" + String(file.name()) + "\": " + String(file.size()) + ", ";
|
||||
}
|
||||
file = root.openNextFile();
|
||||
}
|
||||
|
||||
if (output.endsWith(", "))
|
||||
{
|
||||
output.remove(output.length() - 2);
|
||||
}
|
||||
output += "}";
|
||||
if (isRoot)
|
||||
{
|
||||
output += "}";
|
||||
}
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // end FileExplorer_h
|
||||
Reference in New Issue
Block a user