Optimize exclude file logic in SPIFFSEditor
This commit is contained in:
parent
e54369756a
commit
05306e407f
|
@ -266,6 +266,16 @@ const uint8_t edit_htm_gz[] PROGMEM = {
|
||||||
0xE8, 0x9D, 0x36, 0x92, 0x29, 0x00, 0x00
|
0xE8, 0x9D, 0x36, 0x92, 0x29, 0x00, 0x00
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define SPIFFS_MAXLENGTH_FILEPATH 32
|
||||||
|
const char *excludeListFile = "/.exclude.files";
|
||||||
|
|
||||||
|
typedef struct ExcludeListS {
|
||||||
|
char *item;
|
||||||
|
ExcludeListS *next;
|
||||||
|
} ExcludeList;
|
||||||
|
|
||||||
|
static ExcludeList *excludes = NULL;
|
||||||
|
|
||||||
static bool matchWild(const char *pattern, const char *testee) {
|
static bool matchWild(const char *pattern, const char *testee) {
|
||||||
const char *nxPat = NULL, *nxTst = NULL;
|
const char *nxPat = NULL, *nxTst = NULL;
|
||||||
|
|
||||||
|
@ -288,47 +298,79 @@ static bool matchWild(const char *pattern, const char *testee) {
|
||||||
return (*pattern == 0);
|
return (*pattern == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SPIFFS_MAXLENGTH_FILEPATH 32
|
static bool addExclude(const char *item){
|
||||||
|
size_t len = strlen(item);
|
||||||
|
if(!len){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ExcludeList *e = (ExcludeList *)malloc(sizeof(ExcludeList));
|
||||||
|
if(!e){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
e->item = (char *)malloc(len+1);
|
||||||
|
if(!e->item){
|
||||||
|
free(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memcpy(e->item, item, len+1);
|
||||||
|
e->next = excludes;
|
||||||
|
excludes = e;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool isExcluded(fs::FS _fs, const char *filename) {
|
static void loadExcludeList(fs::FS &_fs, const char *filename){
|
||||||
const char *excludeList = EXCLUDELIST;
|
static char linebuf[SPIFFS_MAXLENGTH_FILEPATH];
|
||||||
static char linebuf[SPIFFS_MAXLENGTH_FILEPATH];
|
fs::File excludeFile=_fs.open(filename, "r");
|
||||||
fs::File excludeFile=_fs.open(excludeList, "r");
|
if(!excludeFile){
|
||||||
if(!excludeFile){
|
//addExclude("/*.js.gz");
|
||||||
excludeFile=_fs.open(excludeList, "w");
|
return;
|
||||||
excludeFile.println("/*.js.gz");
|
}
|
||||||
excludeFile.println(excludeList);
|
#ifdef ESP32
|
||||||
excludeFile.close();
|
if(excludeFile.isDirectory()){
|
||||||
excludeFile=_fs.open(excludeList, "r");
|
excludeFile.close();
|
||||||
}
|
return;
|
||||||
if (excludeFile.size() > 0){
|
}
|
||||||
uint8_t idx;
|
#endif
|
||||||
bool isOverflowed = false;
|
if (excludeFile.size() > 0){
|
||||||
while (excludeFile.available()){
|
uint8_t idx;
|
||||||
linebuf[0] = '\0';
|
bool isOverflowed = false;
|
||||||
idx = 0;
|
while (excludeFile.available()){
|
||||||
int lastChar;
|
linebuf[0] = '\0';
|
||||||
do {
|
idx = 0;
|
||||||
lastChar = excludeFile.read();
|
int lastChar;
|
||||||
if(lastChar != '\r'){
|
do {
|
||||||
linebuf[idx++] = (char) lastChar;
|
lastChar = excludeFile.read();
|
||||||
|
if(lastChar != '\r'){
|
||||||
|
linebuf[idx++] = (char) lastChar;
|
||||||
|
}
|
||||||
|
} while ((lastChar >= 0) && (lastChar != '\n') && (idx < SPIFFS_MAXLENGTH_FILEPATH));
|
||||||
|
|
||||||
|
if(isOverflowed){
|
||||||
|
isOverflowed = (lastChar != '\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
isOverflowed = (idx >= SPIFFS_MAXLENGTH_FILEPATH);
|
||||||
|
linebuf[idx-1] = '\0';
|
||||||
|
if(!addExclude(linebuf)){
|
||||||
|
excludeFile.close();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} while ((lastChar >= 0) && (lastChar != '\n') && (idx < SPIFFS_MAXLENGTH_FILEPATH));
|
|
||||||
|
|
||||||
if(isOverflowed){
|
|
||||||
isOverflowed = (lastChar != '\n');
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
isOverflowed = (idx >= SPIFFS_MAXLENGTH_FILEPATH);
|
|
||||||
linebuf[idx-1] = '\0';
|
|
||||||
|
|
||||||
if (matchWild(linebuf, filename)){
|
|
||||||
excludeFile.close();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
excludeFile.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isExcluded(fs::FS &_fs, const char *filename) {
|
||||||
|
if(excludes == NULL){
|
||||||
|
loadExcludeList(_fs, excludeListFile);
|
||||||
|
}
|
||||||
|
ExcludeList *e = excludes;
|
||||||
|
while(e){
|
||||||
|
if (matchWild(e->item, filename)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
e = e->next;
|
||||||
}
|
}
|
||||||
excludeFile.close();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -353,13 +395,27 @@ bool SPIFFSEditor::canHandle(AsyncWebServerRequest *request){
|
||||||
return true;
|
return true;
|
||||||
if(request->hasParam("edit")){
|
if(request->hasParam("edit")){
|
||||||
request->_tempFile = _fs.open(request->arg("edit"), "r");
|
request->_tempFile = _fs.open(request->arg("edit"), "r");
|
||||||
if(!request->_tempFile)
|
if(!request->_tempFile){
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
#ifdef ESP32
|
||||||
|
if(request->_tempFile.isDirectory()){
|
||||||
|
request->_tempFile.close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if(request->hasParam("download")){
|
if(request->hasParam("download")){
|
||||||
request->_tempFile = _fs.open(request->arg("download"), "r");
|
request->_tempFile = _fs.open(request->arg("download"), "r");
|
||||||
if(!request->_tempFile)
|
if(!request->_tempFile){
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
#ifdef ESP32
|
||||||
|
if(request->_tempFile.isDirectory()){
|
||||||
|
request->_tempFile.close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
request->addInterestingHeader("If-Modified-Since");
|
request->addInterestingHeader("If-Modified-Since");
|
||||||
return true;
|
return true;
|
||||||
|
@ -397,12 +453,12 @@ void SPIFFSEditor::handleRequest(AsyncWebServerRequest *request){
|
||||||
while(dir.next()){
|
while(dir.next()){
|
||||||
fs::File entry = dir.openFile("r");
|
fs::File entry = dir.openFile("r");
|
||||||
#endif
|
#endif
|
||||||
/*if (isExcluded(_fs, entry.name())) {
|
if (isExcluded(_fs, entry.name())) {
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
entry = dir.openNextFile();
|
entry = dir.openNextFile();
|
||||||
#endif
|
#endif
|
||||||
continue;
|
continue;
|
||||||
}*/
|
}
|
||||||
if (output != "[") output += ',';
|
if (output != "[") output += ',';
|
||||||
output += "{\"type\":\"";
|
output += "{\"type\":\"";
|
||||||
output += "file";
|
output += "file";
|
||||||
|
@ -417,6 +473,9 @@ void SPIFFSEditor::handleRequest(AsyncWebServerRequest *request){
|
||||||
entry.close();
|
entry.close();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#ifdef ESP32
|
||||||
|
dir.close();
|
||||||
|
#endif
|
||||||
output += "]";
|
output += "]";
|
||||||
request->send(200, "text/json", output);
|
request->send(200, "text/json", output);
|
||||||
output = String();
|
output = String();
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
#define SPIFFSEditor_H_
|
#define SPIFFSEditor_H_
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
|
|
||||||
#define EXCLUDELIST "/.exclude.files"
|
|
||||||
|
|
||||||
class SPIFFSEditor: public AsyncWebHandler {
|
class SPIFFSEditor: public AsyncWebHandler {
|
||||||
private:
|
private:
|
||||||
fs::FS _fs;
|
fs::FS _fs;
|
||||||
|
|
Loading…
Reference in New Issue