Skip to content
Open
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
117 changes: 83 additions & 34 deletions FSexplorer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
** setup()
** {
** setupFSexplorer();
** httpServer.serveStatic("/FSexplorer.png", LittleFS, "/FSexplorer.png");
** httpServer.serveStatic("/FSexplorer.png", SystemFS, "/FSexplorer.png");
** httpServer.on("/", sendIndexPage);
** httpServer.on("/index", sendIndexPage);
** httpServer.on("/index.html",sendIndexPage);
Expand Down Expand Up @@ -61,18 +61,18 @@ const char Header[] = "HTTP/1.1 303 OK\r\nLocation:FSexplorer.html\r\nCache-Cont

//=====================================================================================
void startWebserver(){
if (!LittleFS.exists("/index.html")) {
httpServer.serveStatic("/", LittleFS, "/FSexplorer.html");
httpServer.serveStatic("/index", LittleFS, "/FSexplorer.html");
httpServer.serveStatic("/index.html", LittleFS, "/FSexplorer.html");
if (!SystemFS.exists("/index.html")) {
httpServer.serveStatic("/", SystemFS, "/FSexplorer.html");
httpServer.serveStatic("/index", SystemFS, "/FSexplorer.html");
httpServer.serveStatic("/index.html", SystemFS, "/FSexplorer.html");
} else{
httpServer.serveStatic("/", LittleFS, "/index.html");
httpServer.serveStatic("/index", LittleFS, "/index.html");
httpServer.serveStatic("/index.html", LittleFS, "/index.html");
httpServer.serveStatic("/", SystemFS, "/index.html");
httpServer.serveStatic("/index", SystemFS, "/index.html");
httpServer.serveStatic("/index.html", SystemFS, "/index.html");
}
httpServer.serveStatic("/FSexplorer.png", LittleFS, "/FSexplorer.png");
httpServer.serveStatic("/index.css", LittleFS, "/index.css");
httpServer.serveStatic("/index.js", LittleFS, "/index.js");
httpServer.serveStatic("/FSexplorer.png", SystemFS, "/FSexplorer.png");
httpServer.serveStatic("/index.css", SystemFS, "/index.css");
httpServer.serveStatic("/index.js", SystemFS, "/index.js");
//otgw pic functions
httpServer.on("/pic", upgradepic);
// all other api calls are catched in FSexplorer onNotFounD!
Expand All @@ -86,20 +86,22 @@ void startWebserver(){
}
//=====================================================================================
void setupFSexplorer(){
LittleFS.begin();
if (LittleFS.exists("/FSexplorer.html"))
SystemFS.begin();
if (SystemFS.exists("/FSexplorer.html"))
{
httpServer.serveStatic("/FSexplorer.html", LittleFS, "/FSexplorer.html");
httpServer.serveStatic("/FSexplorer", LittleFS, "/FSexplorer.html");
httpServer.serveStatic("/FSexplorer.html", SystemFS, "/FSexplorer.html");
httpServer.serveStatic("/FSexplorer", SystemFS, "/FSexplorer.html");
}
else
{
httpServer.send(200, "text/html", Helper); //Upload the FSexplorer.html
}
httpServer.on("/api/firmwarefilelist", apifirmwarefilelist);
httpServer.on("/api/listfiles", apilistfiles);
httpServer.on("/api/listuserfiles", apilistuserfiles);
httpServer.on("/LittleFSformat", formatLittleFS);
httpServer.on("/upload", HTTP_POST, []() {}, handleFileUpload);
httpServer.on("/upload", HTTP_POST, []() {}, handleSysFileUpload);
httpServer.on("/uploadusr", HTTP_POST, []() {}, handleUserFileUpload);
httpServer.on("/ReBoot", reBootESP);
httpServer.on("/ResetWireless", resetWirelessButton);

Expand All @@ -120,6 +122,7 @@ void setupFSexplorer(){
{
if (bDebugRestAPI) DebugTf("next: handleFile(%s)\r\n"
, String(httpServer.urlDecode(httpServer.uri())).c_str());

if (!handleFile(httpServer.urlDecode(httpServer.uri())))
{
httpServer.send(404, "text/plain", "FileNotFound\r\n");
Expand All @@ -139,14 +142,14 @@ void apifirmwarefilelist() {

s = buffer;
s += sprintf(buffer, "[");
dir = LittleFS.openDir("/");
dir = SystemFS.openDir("/");
while (dir.next()) {
if (dir.fileName().endsWith(".hex")) {
version="";
fwversion="";
String verfile = "/" + dir.fileName();
verfile.replace(".hex", ".ver");
f = LittleFS.open(verfile, "r");
f = SystemFS.open(verfile, "r");
if (f) {
version = f.readStringUntil('\n');
version.trim();
Expand All @@ -156,7 +159,7 @@ void apifirmwarefilelist() {
DebugTf("GetVersion(%s) returned %s\n", dir.fileName().c_str(), fwversion.c_str());
if (fwversion.length() && strcmp(fwversion.c_str(),version.c_str())) { // versions do not match
version=fwversion; // assign hex file version to version
if (f = LittleFS.open(verfile, "w")) { // write to .ver file
if (f = SystemFS.open(verfile, "w")) { // write to .ver file
DebugTf("writing %s to %s\n",version.c_str(),verfile.c_str());
f.print(version + "\n");
f.close();
Expand All @@ -173,8 +176,20 @@ void apifirmwarefilelist() {

//=====================================================================================

void apilistfiles() {
apilistFSfiles(SystemFS);
}

void apilistuserfiles() {
if(bUserFSpresent) {
apilistFSfiles(UserFS);
} else {
httpServer.send(200, "application/json", "");
// httpServer.send(404, "text/plain", "");
}
}

void apilistfiles() // Senden aller Daten an den Client
void apilistFSfiles(FS thisFS) // Senden aller Daten an den Client
{
FSInfo LittleFSinfo;

Expand All @@ -187,7 +202,7 @@ void apilistfiles() // Senden aller Daten an den Client
_fileMeta dirMap[MAX_FILES_IN_LIST+1];
int fileNr = 0;

Dir dir = LittleFS.openDir("/"); // List files on LittleFS
Dir dir = thisFS.openDir("/"); // List files on SystemFS
while (dir.next() && (fileNr < MAX_FILES_IN_LIST))
{
dirMap[fileNr].Name[0] = '\0';
Expand Down Expand Up @@ -231,7 +246,7 @@ void apilistfiles() // Senden aller Daten an den Client
temp += R"({"name":")" + String(dirMap[f].Name) + R"(","size":")" + formatBytes(dirMap[f].Size) + R"("},)";
}

LittleFS.info(LittleFSinfo);
thisFS.info(LittleFSinfo);
temp += R"({"usedBytes":")" + formatBytes(LittleFSinfo.usedBytes * 1.05) + R"(",)" + // Berechnet den verwendeten Speicherplatz + 5% Sicherheitsaufschlag
R"("totalBytes":")" + formatBytes(LittleFSinfo.totalBytes) + R"(","freeBytes":")" + // Zeigt die Größe des Speichers
(LittleFSinfo.totalBytes - (LittleFSinfo.usedBytes * 1.05)) + R"("}])"; // Berechnet den freien Speicherplatz + 5% Sicherheitsaufschlag
Expand All @@ -243,23 +258,57 @@ void apilistfiles() // Senden aller Daten an den Client

//=====================================================================================
bool handleFile(String&& path)
{
bool success = false;

if (!SystemFS.exists("/FSexplorer.html")) httpServer.send(200, "text/html", Helper); //Upload the FSexplorer.html
if (path.endsWith("/")) path += "index.html";

success = httpServer.hasArg("user") ? handleFSfile(UserFS, path) : handleFSfile(SystemFS, path);

return success;

} // handleFile()

bool handleFSfile(FS sourceFS, String& path)
{
if (httpServer.hasArg("delete"))
{
DebugTf("Delete -> [%s]\n\r", httpServer.arg("delete").c_str());
LittleFS.remove(httpServer.arg("delete")); // Datei löschen
sourceFS.remove(httpServer.arg("delete")); // Datei löschen
httpServer.sendContent(Header);
return true;
}
if (!LittleFS.exists("/FSexplorer.html")) httpServer.send(200, "text/html", Helper); //Upload the FSexplorer.html
if (path.endsWith("/")) path += "index.html";
return LittleFS.exists(path) ? ({File f = LittleFS.open(path, "r"); httpServer.streamFile(f, contentType(path)); f.close(); true;}) : false;

if(!sourceFS.exists(path)) { return false;}

} // handleFile()
DebugTf("Serving file [%s] from -> %s partition \n\r", path.c_str(), httpServer.hasArg("user") ? "user" : "system");

File f = sourceFS.open(path, "r");
httpServer.streamFile(f, contentType(path));
f.close();

return true;

} // handleFSfile()


//=====================================================================================
void handleSysFileUpload()
{
handleFSFileUpload(SystemFS);
}

//=====================================================================================

void handleUserFileUpload()
{
handleFSFileUpload(UserFS);
}

//=====================================================================================
void handleFileUpload()

void handleFSFileUpload(FS destinationFS)
{
static File fsUploadFile;
HTTPUpload& upload = httpServer.upload();
Expand All @@ -270,7 +319,7 @@ void handleFileUpload()
upload.filename = upload.filename.substring(upload.filename.length() - 30, upload.filename.length()); // Dateinamen auf 30 Zeichen kürzen
}
Debugln("FileUpload Name: " + upload.filename);
fsUploadFile = LittleFS.open("/" + httpServer.urlDecode(upload.filename), "w");
fsUploadFile = destinationFS.open("/" + httpServer.urlDecode(upload.filename), "w");
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
Expand All @@ -292,9 +341,9 @@ void handleFileUpload()
//=====================================================================================
void formatLittleFS()
{ //Formatiert den Speicher
if (!LittleFS.exists("/!format")) return;
DebugTln(F("Format LittleFS"));
LittleFS.format();
if (!SystemFS.exists("/!format")) return;
DebugTln(F("Format SystemFS"));
SystemFS.format();
httpServer.sendContent(Header);

} // formatLittleFS()
Expand Down Expand Up @@ -330,8 +379,8 @@ const String &contentType(String& filename)
bool freeSpace(uint16_t const& printsize)
{
FSInfo LittleFSinfo;
LittleFS.info(LittleFSinfo);
Debugln(formatBytes(LittleFSinfo.totalBytes - (LittleFSinfo.usedBytes * 1.05)) + " im LittleFS frei");
SystemFS.info(LittleFSinfo);
Debugln(formatBytes(LittleFSinfo.totalBytes - (LittleFSinfo.usedBytes * 1.05)) + " im SystemFS frei");
return (LittleFSinfo.totalBytes - (LittleFSinfo.usedBytes * 1.05) > printsize) ? true : false;

} // freeSpace()
Expand Down
38 changes: 34 additions & 4 deletions MQTTstuff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,36 @@ void doAutoConfigure(bool bForcaAll = false){
}
// bool success = doAutoConfigure("config"); // the string "config" should match every line non-comment in mqttha.cfg
}
//===========================================================================================

bool copyMQTTcfg() {

DebugTln(F("Error: mqttha.cfg not present in user filesystem. Copying!"));

String sLine = "";

const char *cfgFilename = "/mqttha.cfg";
SystemFS.begin();
UserFS.begin();

File fin = SystemFS.open(cfgFilename, "r");
File fout = UserFS.open(cfgFilename, "w");

if ((!fin) || (!fout)) {
DebugTln(F("Error: couldn't open source or destination for copying!"));
return false;
}

while (fin.available()) {
sLine = fin.readStringUntil('\n');
fout.println(sLine.c_str());
}

fin.close();
fout.close();
return true;
}

//===========================================================================================
bool doAutoConfigureMsgid(byte OTid)
{
Expand All @@ -550,14 +580,14 @@ bool doAutoConfigureMsgid(byte OTid)
//Let's open the MQTT autoconfig file
File fh; //filehandle
const char *cfgFilename = "/mqttha.cfg";
LittleFS.begin();
UserFS.begin();

if (!LittleFS.exists(cfgFilename)) {
if (!UserFS.exists(cfgFilename)) {
DebugTln(F("Error: confuration file not found."));
return _result;
if(!copyMQTTcfg()) { return _result; }
}

fh = LittleFS.open(cfgFilename, "r");
fh = SystemFS.open(cfgFilename, "r");

if (!fh) {
DebugTln(F("Error: could not open confuration file."));
Expand Down
8 changes: 4 additions & 4 deletions OTGW-Core.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2007,13 +2007,13 @@ void refreshpic(String filename, String version) {
http.begin(client, "http://otgw.tclcode.com/download/" + filename);
code = http.GET();
if (code == HTTP_CODE_OK) {
File f = LittleFS.open("/" + filename, "w");
File f = SystemFS.open("/" + filename, "w");
if (f) {
http.writeToStream(&f);
f.close();
String verfile = "/" + filename;
verfile.replace(".hex", ".ver");
f = LittleFS.open(verfile, "w");
f = SystemFS.open(verfile, "w");
if (f) {
f.print(latest + "\n");
f.close();
Expand All @@ -2037,9 +2037,9 @@ void upgradepic() {
refreshpic(filename, version);
} else if (action == "delete") {
String path = "/" + filename;
LittleFS.remove(path);
SystemFS.remove(path);
path.replace(".hex", ".ver");
LittleFS.remove(path);
SystemFS.remove(path);
}
httpServer.sendHeader("Location", "index.html#tabPICflash", true);
httpServer.send(303, "text/html", "<a href='index.html#tabPICflash'>Return</a>");
Expand Down
Loading