Skip to content

Commit 5b10751

Browse files
authored
Update WebServer.ino
* Enable FAT and LittleFS filesystems as configured. * use new versions of RequestHandler::canHandle and RequestHandler::canUpload
1 parent 70786dc commit 5b10751

File tree

1 file changed

+71
-24
lines changed

1 file changed

+71
-24
lines changed

libraries/WebServer/examples/WebServer/WebServer.ino

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,27 @@
1818
//
1919
// Please use the following Arduino IDE configuration
2020
//
21-
// * Board: ESP32 Dev Module
22-
// * Partition Scheme: Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS)
21+
// * Board: "ESP32 Dev Module" or other board with ESP32
22+
// * Partition Scheme: "Default 4MB with spiffs" or any other scheme with spiffs or FAT
2323
// but LittleFS will be used in the partition (not SPIFFS)
2424
// * other setting as applicable
2525
//
2626
// Changelog:
2727
// 21.07.2021 creation, first version
2828
// 08.01.2023 ESP32 version with ETag
29+
// 02.08.2024 support LitteFS and FAT file systems (on large Flash chips)
2930

3031
#include <Arduino.h>
3132
#include <WebServer.h>
3233
#include <WiFi.h>
3334

3435
#include "secrets.h" // add WLAN Credentials in here.
3536

37+
#include "esp_partition.h" // to check existing data partitions in Flash memory
38+
3639
#include <FS.h> // File System for Web Server Files
37-
#include <LittleFS.h> // This file system is used.
40+
#include <LittleFS.h> // Use LittleFSThis file system is used.
41+
#include <FFat.h> // or.. FAT
3842

3943
// mark parameters not used in example
4044
#define UNUSED __attribute__((unused))
@@ -51,6 +55,9 @@
5155
// need a WebServer for http access on port 80.
5256
WebServer server(80);
5357

58+
// The file system in use...
59+
fs::FS *fsys = nullptr;
60+
5461
// The text of builtin files are in this header file
5562
#include "builtinfiles.h"
5663

@@ -65,7 +72,7 @@ void handleRedirect() {
6572
TRACE("Redirect...\n");
6673
String url = "/index.htm";
6774

68-
if (!LittleFS.exists(url)) {
75+
if (!fsys->exists(url)) {
6976
url = "/$upload.htm";
7077
}
7178

@@ -76,7 +83,7 @@ void handleRedirect() {
7683
// This function is called when the WebServer was requested to list all existing files in the filesystem.
7784
// a JSON array with file information is returned.
7885
void handleListFiles() {
79-
File dir = LittleFS.open("/", "r");
86+
File dir = fsys->open("/", "r");
8087
String result;
8188

8289
result += "[\n";
@@ -107,8 +114,15 @@ void handleSysInfo() {
107114
result += " \"Chip Revision\": " + String(ESP.getChipRevision()) + ",\n";
108115
result += " \"flashSize\": " + String(ESP.getFlashChipSize()) + ",\n";
109116
result += " \"freeHeap\": " + String(ESP.getFreeHeap()) + ",\n";
110-
result += " \"fsTotalBytes\": " + String(LittleFS.totalBytes()) + ",\n";
111-
result += " \"fsUsedBytes\": " + String(LittleFS.usedBytes()) + ",\n";
117+
118+
if (fsys == (fs::FS *)&FFat) {
119+
result += " \"fsTotalBytes\": " + String(FFat.totalBytes()) + ",\n";
120+
result += " \"fsUsedBytes\": " + String(FFat.usedBytes()) + ",\n";
121+
} else {
122+
result += " \"fsTotalBytes\": " + String(LittleFS.totalBytes()) + ",\n";
123+
result += " \"fsUsedBytes\": " + String(LittleFS.usedBytes()) + ",\n";
124+
}
125+
112126
result += "}";
113127

114128
server.sendHeader("Cache-Control", "no-cache");
@@ -132,11 +146,11 @@ public:
132146
// @param requestMethod method of the http request line.
133147
// @param requestUri request resource from the http request line.
134148
// @return true when method can be handled.
135-
bool canHandle(HTTPMethod requestMethod, String UNUSED uri) override {
149+
bool canHandle(WebServer &server, HTTPMethod requestMethod, String uri) override {
136150
return ((requestMethod == HTTP_POST) || (requestMethod == HTTP_DELETE));
137151
} // canHandle()
138152

139-
bool canUpload(String uri) override {
153+
bool canUpload(WebServer &server, String uri) override {
140154
// only allow upload on root fs level.
141155
return (uri == "/");
142156
} // canUpload()
@@ -148,15 +162,13 @@ public:
148162
fName = "/" + fName;
149163
}
150164

151-
TRACE("handle %s\n", fName.c_str());
152-
153165
if (requestMethod == HTTP_POST) {
154166
// all done in upload. no other forms.
155167

156168
} else if (requestMethod == HTTP_DELETE) {
157-
if (LittleFS.exists(fName)) {
169+
if (fsys->exists(fName)) {
158170
TRACE("DELETE %s\n", fName.c_str());
159-
LittleFS.remove(fName);
171+
fsys->remove(fName);
160172
}
161173
} // if
162174

@@ -165,7 +177,7 @@ public:
165177
} // handle()
166178

167179
// uploading process
168-
void upload(WebServer UNUSED &server, String UNUSED _requestUri, HTTPUpload &upload) override {
180+
void upload(WebServer UNUSED &server, String requestUri, HTTPUpload &upload) override {
169181
// ensure that filename starts with '/'
170182
static size_t uploadSize;
171183

@@ -178,10 +190,10 @@ public:
178190
}
179191
TRACE("start uploading file %s...\n", fName.c_str());
180192

181-
if (LittleFS.exists(fName)) {
182-
LittleFS.remove(fName);
193+
if (fsys->exists(fName)) {
194+
fsys->remove(fName);
183195
} // if
184-
_fsUploadFile = LittleFS.open(fName, "w");
196+
_fsUploadFile = fsys->open(fName, "w");
185197
uploadSize = 0;
186198

187199
} else if (upload.status == UPLOAD_FILE_WRITE) {
@@ -198,7 +210,7 @@ public:
198210
if (!fName.startsWith("/")) {
199211
fName = "/" + fName;
200212
}
201-
LittleFS.remove(fName);
213+
fsys->remove(fName);
202214
}
203215
uploadSize += upload.currentSize;
204216
// TRACE("free:: %d of %d\n", LittleFS.usedBytes(), LittleFS.totalBytes());
@@ -207,7 +219,7 @@ public:
207219
} // if
208220

209221
} else if (upload.status == UPLOAD_FILE_END) {
210-
TRACE("finished.\n");
222+
TRACE("upload done.\n");
211223
// Close the file
212224
if (_fsUploadFile) {
213225
_fsUploadFile.close();
@@ -231,14 +243,49 @@ void setup(void) {
231243

232244
TRACE("Starting WebServer example...\n");
233245

246+
// ----- check partitions for finding the fileystem type -----
247+
esp_partition_iterator_t i;
248+
249+
i = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, nullptr);
250+
if (i) {
251+
TRACE("FAT partition found.");
252+
fsys = &FFat;
253+
254+
} else {
255+
i = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, nullptr);
256+
if (i) {
257+
TRACE("SPIFFS partition found.");
258+
fsys = &LittleFS;
259+
260+
} else {
261+
TRACE("no partition found.");
262+
}
263+
}
264+
esp_partition_iterator_release(i);
265+
266+
// mount and format as needed
234267
TRACE("Mounting the filesystem...\n");
235-
if (!LittleFS.begin()) {
268+
if (!fsys) {
269+
TRACE("need to change the board configuration to include a partition for files.\n");
270+
delay(30000);
271+
272+
} else if ((fsys == (fs::FS *)&FFat) && (!FFat.begin())) {
273+
TRACE("could not mount the filesystem...\n");
274+
delay(2000);
275+
TRACE("formatting FAT...\n");
276+
FFat.format();
277+
delay(2000);
278+
TRACE("restarting...\n");
279+
delay(2000);
280+
ESP.restart();
281+
282+
} else if ((fsys == (fs::FS *)&LittleFS) && (!LittleFS.begin())) {
236283
TRACE("could not mount the filesystem...\n");
237284
delay(2000);
238-
TRACE("formatting...\n");
285+
TRACE("formatting LittleFS...\n");
239286
LittleFS.format();
240287
delay(2000);
241-
TRACE("restart.\n");
288+
TRACE("restarting...\n");
242289
delay(2000);
243290
ESP.restart();
244291
}
@@ -286,7 +333,7 @@ void setup(void) {
286333
// UPLOAD and DELETE of files in the file system using a request handler.
287334
server.addHandler(new FileServerHandler());
288335

289-
// // enable CORS header in webserver results
336+
// enable CORS header in webserver results
290337
server.enableCORS(true);
291338

292339
// enable ETAG header in webserver results (used by serveStatic handler)
@@ -306,7 +353,7 @@ void setup(void) {
306353
#endif
307354

308355
// serve all static files
309-
server.serveStatic("/", LittleFS, "/");
356+
server.serveStatic("/", *fsys, "/");
310357

311358
TRACE("Register default (not found) answer...\n");
312359

0 commit comments

Comments
 (0)