Skip to content

Commit

Permalink
added spiffs export to .tar.gz
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Jan 31, 2025
1 parent b31700e commit 37455f0
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion examples/ESP32/WebServer_mod_gzip/WebServer_mod_gzip.ino
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ void setup()
// mod_gzip.enableCache(); // cache gz files
mod_gzip.disableCache(); // ignore existing gz files, compress on the fly

server.on("/json", []() {

server.on("/json", []() { // send gz compressed JSON
int responseCode = 200;
const char* myJsonData = "{\"ceci\":\"cela\",\"couci\":\"couça\",\"patati\":\"patata\"}";
server.sendHeader(String(F("Content-Type")), String(F("application/json")), true);
Expand All @@ -221,6 +222,27 @@ void setup()
LZPacker::compress( (uint8_t*)myJsonData, strlen(myJsonData), &server.client() );
});


server.on("/spiffs.tar.gz", []() { // compress SPIFFS files/folders on the fly
int responseCode = 200;
server.sendHeader(String(F("Content-Type")), String(F("application/tar+gzip")), true);
//server.sendHeader(String(F("Content-Encoding")), String(F("gzip")));
server.sendHeader(String(F("Connection")), String(F("close")));
// building HTTP response without "Content-Length" header isn't 100% standard, so we have to do this
String HTTPResponse = String(F("HTTP/1.1"))+' '+String(responseCode)+' '+server.responseCodeToString(responseCode)+"\r\n";
size_t headersCount = server.responseHeaders();
for(size_t i=0;i<headersCount;i++)
HTTPResponse.concat(server.responseHeaderName(i) + F(": ") + server.responseHeader(i) + F("\r\n"));
HTTPResponse.concat(F("\r\n"));
// sent HTTP response
server.client().write(HTTPResponse.c_str(), HTTPResponse.length());

std::vector<TAR::dir_entity_t> dirEntities; // storage for scanned dir entities
TarPacker::collectDirEntities(&dirEntities, &tarGzFS, "/", 10); // collect dir and files
TarGzPacker::compress(&tarGzFS, dirEntities, &server.client());
});


server.addMiddleware( &mod_gzip );

server.begin();
Expand Down

0 comments on commit 37455f0

Please sign in to comment.