WLED/wled00/file.cpp

242 lines
6.4 KiB
C++
Raw Normal View History

#include "wled.h"
/*
* Utility for SPIFFS filesystem
*/
//filesystem
#ifndef WLED_DISABLE_FILESYSTEM
#include <FS.h>
#ifdef ARDUINO_ARCH_ESP32
#include "SPIFFS.h"
#endif
#include "SPIFFSEditor.h"
#endif
2020-05-05 09:01:09 +02:00
#ifndef WLED_DISABLE_FILESYSTEM
2020-05-08 22:53:59 +02:00
//find() that reads and buffers data from file stream in 256-byte blocks.
//Significantly faster, f.find(key) can take SECONDS for multi-kB files
bool bufferedFind(const char *target, File f) {
2020-05-05 09:01:09 +02:00
size_t targetLen = strlen(target);
2020-05-08 22:53:59 +02:00
Serial.println(target);
//Serial.println(f.position());
2020-05-05 09:01:09 +02:00
size_t index = 0;
2020-05-08 22:53:59 +02:00
byte c;
uint16_t bufsize = 0, count = 0;
byte buf[256];
2020-05-05 09:01:09 +02:00
2020-05-08 22:53:59 +02:00
while (f.position() < f.size() -1) {
//c = f.read();
//Serial.println(f.position());
bufsize = f.read(buf, 256);
count = 0;
while (count < bufsize) {
if(buf[count] != target[index])
2020-05-05 09:01:09 +02:00
index = 0; // reset index if any char does not match
2020-05-08 22:53:59 +02:00
if(buf[count] == target[index]) {
if(++index >= targetLen) { // return true if all chars in the target match
f.seek((f.position() - bufsize) + count +1);
return true;
}
2020-05-05 09:01:09 +02:00
}
2020-05-08 22:53:59 +02:00
count++;
2020-05-05 09:01:09 +02:00
}
}
return false;
}
2020-05-08 22:53:59 +02:00
//find empty spots in file stream in 256-byte blocks.
bool bufferedFindSpace(uint16_t targetLen, File f) {
size_t index = 0;
byte c;
uint16_t bufsize = 0, count = 0;
byte buf[256];
while (f.position() < f.size() -1) {
bufsize = f.read(buf, 256);
count = 0;
while (count < bufsize) {
if(buf[count] != ' ')
index = 0; // reset index if not space
if(buf[count] == ' ') {
if(++index >= targetLen) { // return true if space long enough
f.seek(f.position() - targetLen);
return true;
}
}
count++;
}
}
return false;
}
bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* content)
2020-05-05 09:01:09 +02:00
{
char objKey[10];
sprintf(objKey, "\"%ld\":", id);
2020-05-08 22:53:59 +02:00
writeObjectToFile(file, objKey, content);
2020-05-05 09:01:09 +02:00
}
2020-05-08 22:53:59 +02:00
bool writeObjectToFile(const char* file, const char* key, JsonDocument* content)
2020-05-05 09:01:09 +02:00
{
uint32_t pos = 0;
2020-05-08 22:53:59 +02:00
File f = SPIFFS.open(file, "r+");
2020-05-05 09:01:09 +02:00
if (!f) f = SPIFFS.open(file,"w");
if (!f) return false;
//f.setTimeout(1);
f.seek(0, SeekSet);
2020-05-08 22:53:59 +02:00
if (!bufferedFind(key, f)) //key does not exist in file
2020-05-05 09:01:09 +02:00
{
2020-05-08 22:53:59 +02:00
return appendObjectToFile(file, key, content, f);
2020-05-05 09:01:09 +02:00
}
//exists
pos = f.position();
//measure out end of old object
StaticJsonDocument<512> doc;
deserializeJson(doc, f);
uint32_t pos2 = f.position();
uint32_t oldLen = pos2 - pos;
2020-05-08 22:53:59 +02:00
if (!content->isNull() && measureJson(*content) <= oldLen) //replace
2020-05-05 09:01:09 +02:00
{
2020-05-08 22:53:59 +02:00
serializeJson(*content, f);
2020-05-05 09:01:09 +02:00
//pad rest
for (uint32_t i = f.position(); i < pos2; i++) {
f.write(' ');
}
} else { //delete
pos -+ strlen(key);
oldLen = pos2 - pos;
f.seek(pos, SeekSet);
for (uint32_t i = pos; i < pos2; i++) {
f.write(' ');
}
2020-05-08 22:53:59 +02:00
if (!content->isNull()) return appendObjectToFile(file, key, content, f);
2020-05-05 09:01:09 +02:00
}
f.close();
}
2020-05-08 22:53:59 +02:00
bool appendObjectToFile(const char* file, const char* key, JsonDocument* content, File input)
2020-05-05 09:01:09 +02:00
{
2020-05-08 22:53:59 +02:00
Serial.println("Append");
2020-05-05 09:01:09 +02:00
uint32_t pos = 0;
File f = (input) ? input : SPIFFS.open(file, "r+");
if (!f) f = SPIFFS.open(file,"w");
if (!f) return false;
if (f.size() < 3) f.print("{}");
//if there is enough empty space in file, insert there instead of appending
2020-05-08 22:53:59 +02:00
uint32_t contentLen = measureJson(*content);
Serial.print("clen"); Serial.println(contentLen);
if (bufferedFindSpace(contentLen, f)) {
Serial.println("space");
serializeJson(*content, f);
return true;
2020-05-05 09:01:09 +02:00
}
//check if last character in file is '}' (typical)
f.seek(1, SeekEnd);
2020-05-08 22:53:59 +02:00
if (f.read() == '}') pos = f.size() -1;
2020-05-05 09:01:09 +02:00
if (pos == 0) //not found
{
2020-05-08 22:53:59 +02:00
Serial.println("not}");
while (bufferedFind("}",f)) //find last closing bracket in JSON if not last char
2020-05-05 09:01:09 +02:00
{
pos = f.position();
}
}
2020-05-08 22:53:59 +02:00
Serial.print("pos"); Serial.println(pos);
if (pos < 3)
2020-05-05 09:01:09 +02:00
{
f.seek(pos -1, SeekSet);
f.write(',');
} else { //file content is not valid JSON object
f.seek(0, SeekSet);
f.write('{'); //start JSON
}
2020-05-08 22:53:59 +02:00
//f.print("\"");
2020-05-05 09:01:09 +02:00
f.print(key);
2020-05-08 22:53:59 +02:00
//f.print("\":");
2020-05-05 09:01:09 +02:00
//Append object
2020-05-08 22:53:59 +02:00
serializeJson(*content, f);
2020-05-05 09:01:09 +02:00
f.write('}');
2020-05-08 22:53:59 +02:00
f.close();
2020-05-05 09:01:09 +02:00
}
bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest)
{
char objKey[10];
sprintf(objKey, "\"%ld\":", id);
readObjectFromFile(file, objKey, dest);
}
bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest)
{
//if (id == playlistId) return true;
//playlist is already loaded, but we can't be sure that file hasn't changed since loading
File f = SPIFFS.open(file, "r");
if (!f) return false;
2020-05-08 22:53:59 +02:00
//f.setTimeout(0);
//Serial.println(key);
if (!bufferedFind(key, f)) //key does not exist in file
2020-05-05 09:01:09 +02:00
{
f.close();
return false;
}
deserializeJson(*dest, f);
f.close();
return true;
}
#endif
2019-03-16 02:09:37 +01:00
#if !defined WLED_DISABLE_FILESYSTEM && defined WLED_ENABLE_FS_SERVING
//Un-comment any file types you need
String getContentType(AsyncWebServerRequest* request, String filename){
if(request->hasArg("download")) return "application/octet-stream";
else if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
2019-03-16 02:09:37 +01:00
// else if(filename.endsWith(".css")) return "text/css";
// else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".json")) return "application/json";
else if(filename.endsWith(".png")) return "image/png";
2019-03-16 02:09:37 +01:00
// else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
2019-03-16 02:09:37 +01:00
// else if(filename.endsWith(".xml")) return "text/xml";
// else if(filename.endsWith(".pdf")) return "application/x-pdf";
// else if(filename.endsWith(".zip")) return "application/x-zip";
// else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
2019-03-16 02:09:37 +01:00
bool handleFileRead(AsyncWebServerRequest* request, String path){
DEBUG_PRINTLN("FileRead: " + path);
if(path.endsWith("/")) path += "index.htm";
2019-03-16 02:09:37 +01:00
String contentType = getContentType(request, path);
2020-05-05 09:01:09 +02:00
/*String pathWithGz = path + ".gz";
2019-03-16 02:09:37 +01:00
if(SPIFFS.exists(pathWithGz)){
request->send(SPIFFS, pathWithGz, contentType);
return true;
2020-05-05 09:01:09 +02:00
}*/
2019-03-16 02:09:37 +01:00
if(SPIFFS.exists(path)) {
request->send(SPIFFS, path, contentType);
return true;
}
2019-03-16 02:09:37 +01:00
return false;
}
#else
2019-03-16 02:09:37 +01:00
bool handleFileRead(AsyncWebServerRequest*, String path){return false;}
#endif