Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 05306e4

Browse files
committed
Optimize exclude file logic in SPIFFSEditor
1 parent e543697 commit 05306e4

2 files changed

Lines changed: 98 additions & 41 deletions

File tree

src/SPIFFSEditor.cpp

Lines changed: 98 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ const uint8_t edit_htm_gz[] PROGMEM = {
266266
0xE8, 0x9D, 0x36, 0x92, 0x29, 0x00, 0x00
267267
};
268268

269+
#define SPIFFS_MAXLENGTH_FILEPATH 32
270+
const char *excludeListFile = "/.exclude.files";
271+
272+
typedef struct ExcludeListS {
273+
char *item;
274+
ExcludeListS *next;
275+
} ExcludeList;
276+
277+
static ExcludeList *excludes = NULL;
278+
269279
static bool matchWild(const char *pattern, const char *testee) {
270280
const char *nxPat = NULL, *nxTst = NULL;
271281

@@ -288,47 +298,79 @@ static bool matchWild(const char *pattern, const char *testee) {
288298
return (*pattern == 0);
289299
}
290300

291-
#define SPIFFS_MAXLENGTH_FILEPATH 32
301+
static bool addExclude(const char *item){
302+
size_t len = strlen(item);
303+
if(!len){
304+
return false;
305+
}
306+
ExcludeList *e = (ExcludeList *)malloc(sizeof(ExcludeList));
307+
if(!e){
308+
return false;
309+
}
310+
e->item = (char *)malloc(len+1);
311+
if(!e->item){
312+
free(e);
313+
return false;
314+
}
315+
memcpy(e->item, item, len+1);
316+
e->next = excludes;
317+
excludes = e;
318+
return true;
319+
}
292320

293-
static bool isExcluded(fs::FS _fs, const char *filename) {
294-
const char *excludeList = EXCLUDELIST;
295-
static char linebuf[SPIFFS_MAXLENGTH_FILEPATH];
296-
fs::File excludeFile=_fs.open(excludeList, "r");
297-
if(!excludeFile){
298-
excludeFile=_fs.open(excludeList, "w");
299-
excludeFile.println("/*.js.gz");
300-
excludeFile.println(excludeList);
301-
excludeFile.close();
302-
excludeFile=_fs.open(excludeList, "r");
303-
}
304-
if (excludeFile.size() > 0){
305-
uint8_t idx;
306-
bool isOverflowed = false;
307-
while (excludeFile.available()){
308-
linebuf[0] = '\0';
309-
idx = 0;
310-
int lastChar;
311-
do {
312-
lastChar = excludeFile.read();
313-
if(lastChar != '\r'){
314-
linebuf[idx++] = (char) lastChar;
315-
}
316-
} while ((lastChar >= 0) && (lastChar != '\n') && (idx < SPIFFS_MAXLENGTH_FILEPATH));
321+
static void loadExcludeList(fs::FS &_fs, const char *filename){
322+
static char linebuf[SPIFFS_MAXLENGTH_FILEPATH];
323+
fs::File excludeFile=_fs.open(filename, "r");
324+
if(!excludeFile){
325+
//addExclude("/*.js.gz");
326+
return;
327+
}
328+
#ifdef ESP32
329+
if(excludeFile.isDirectory()){
330+
excludeFile.close();
331+
return;
332+
}
333+
#endif
334+
if (excludeFile.size() > 0){
335+
uint8_t idx;
336+
bool isOverflowed = false;
337+
while (excludeFile.available()){
338+
linebuf[0] = '\0';
339+
idx = 0;
340+
int lastChar;
341+
do {
342+
lastChar = excludeFile.read();
343+
if(lastChar != '\r'){
344+
linebuf[idx++] = (char) lastChar;
345+
}
346+
} while ((lastChar >= 0) && (lastChar != '\n') && (idx < SPIFFS_MAXLENGTH_FILEPATH));
317347

318-
if(isOverflowed){
319-
isOverflowed = (lastChar != '\n');
320-
continue;
348+
if(isOverflowed){
349+
isOverflowed = (lastChar != '\n');
350+
continue;
351+
}
352+
isOverflowed = (idx >= SPIFFS_MAXLENGTH_FILEPATH);
353+
linebuf[idx-1] = '\0';
354+
if(!addExclude(linebuf)){
355+
excludeFile.close();
356+
return;
357+
}
321358
}
322-
isOverflowed = (idx >= SPIFFS_MAXLENGTH_FILEPATH);
323-
linebuf[idx-1] = '\0';
359+
}
360+
excludeFile.close();
361+
}
324362

325-
if (matchWild(linebuf, filename)){
326-
excludeFile.close();
327-
return true;
328-
}
363+
static bool isExcluded(fs::FS &_fs, const char *filename) {
364+
if(excludes == NULL){
365+
loadExcludeList(_fs, excludeListFile);
366+
}
367+
ExcludeList *e = excludes;
368+
while(e){
369+
if (matchWild(e->item, filename)){
370+
return true;
329371
}
372+
e = e->next;
330373
}
331-
excludeFile.close();
332374
return false;
333375
}
334376

@@ -353,13 +395,27 @@ bool SPIFFSEditor::canHandle(AsyncWebServerRequest *request){
353395
return true;
354396
if(request->hasParam("edit")){
355397
request->_tempFile = _fs.open(request->arg("edit"), "r");
356-
if(!request->_tempFile)
398+
if(!request->_tempFile){
357399
return false;
400+
}
401+
#ifdef ESP32
402+
if(request->_tempFile.isDirectory()){
403+
request->_tempFile.close();
404+
return false;
405+
}
406+
#endif
358407
}
359408
if(request->hasParam("download")){
360409
request->_tempFile = _fs.open(request->arg("download"), "r");
361-
if(!request->_tempFile)
410+
if(!request->_tempFile){
362411
return false;
412+
}
413+
#ifdef ESP32
414+
if(request->_tempFile.isDirectory()){
415+
request->_tempFile.close();
416+
return false;
417+
}
418+
#endif
363419
}
364420
request->addInterestingHeader("If-Modified-Since");
365421
return true;
@@ -397,12 +453,12 @@ void SPIFFSEditor::handleRequest(AsyncWebServerRequest *request){
397453
while(dir.next()){
398454
fs::File entry = dir.openFile("r");
399455
#endif
400-
/*if (isExcluded(_fs, entry.name())) {
456+
if (isExcluded(_fs, entry.name())) {
401457
#ifdef ESP32
402458
entry = dir.openNextFile();
403459
#endif
404460
continue;
405-
}*/
461+
}
406462
if (output != "[") output += ',';
407463
output += "{\"type\":\"";
408464
output += "file";
@@ -417,6 +473,9 @@ void SPIFFSEditor::handleRequest(AsyncWebServerRequest *request){
417473
entry.close();
418474
#endif
419475
}
476+
#ifdef ESP32
477+
dir.close();
478+
#endif
420479
output += "]";
421480
request->send(200, "text/json", output);
422481
output = String();

src/SPIFFSEditor.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#define SPIFFSEditor_H_
33
#include <ESPAsyncWebServer.h>
44

5-
#define EXCLUDELIST "/.exclude.files"
6-
75
class SPIFFSEditor: public AsyncWebHandler {
86
private:
97
fs::FS _fs;

0 commit comments

Comments
 (0)