2020-03-25 09:00:55 +01:00
|
|
|
#include "wled.h"
|
2020-03-31 02:38:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Utility for SPIFFS filesystem
|
|
|
|
*/
|
2020-03-25 09:36:55 +01:00
|
|
|
|
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-09-13 22:00:47 +02:00
|
|
|
#ifdef WLED_DEBUG_FS
|
|
|
|
DEBUGFS_PRINT("Find ");
|
|
|
|
DEBUGFS_PRINTLN(target);
|
|
|
|
uint32_t s = millis();
|
|
|
|
#endif
|
|
|
|
|
2020-09-07 20:39:12 +02:00
|
|
|
if (!f || !f.size()) return false;
|
2020-05-05 09:01:09 +02:00
|
|
|
size_t targetLen = strlen(target);
|
2020-09-13 22:00:47 +02:00
|
|
|
|
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-09-09 09:51:04 +02:00
|
|
|
f.seek(0);
|
2020-05-05 09:01:09 +02:00
|
|
|
|
2020-05-08 22:53:59 +02:00
|
|
|
while (f.position() < f.size() -1) {
|
|
|
|
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);
|
2020-09-13 22:00:47 +02:00
|
|
|
DEBUGFS_PRINTF("Found at pos %d, took %d ms", f.position(), millis() - s);
|
2020-05-08 22:53:59 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:00:47 +02:00
|
|
|
DEBUGFS_PRINTF("No match, took %d ms\n", millis() - s);
|
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) {
|
2020-09-13 22:00:47 +02:00
|
|
|
#ifdef WLED_DEBUG_FS
|
|
|
|
DEBUGFS_PRINTF("Find %d spaces\n", targetLen);
|
|
|
|
uint32_t s = millis();
|
|
|
|
#endif
|
|
|
|
|
2020-09-07 20:39:12 +02:00
|
|
|
if (!f || !f.size()) return false;
|
2020-09-13 22:00:47 +02:00
|
|
|
|
2020-09-07 20:39:12 +02:00
|
|
|
uint16_t index = 0;
|
2020-05-08 22:53:59 +02:00
|
|
|
uint16_t bufsize = 0, count = 0;
|
|
|
|
byte buf[256];
|
2020-09-09 09:51:04 +02:00
|
|
|
f.seek(0);
|
2020-05-08 22:53:59 +02:00
|
|
|
|
|
|
|
while (f.position() < f.size() -1) {
|
|
|
|
bufsize = f.read(buf, 256);
|
|
|
|
count = 0;
|
2020-09-13 22:00:47 +02:00
|
|
|
|
2020-05-08 22:53:59 +02:00
|
|
|
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
|
2020-09-09 09:51:04 +02:00
|
|
|
f.seek((f.position() - bufsize) + count +1 - targetLen);
|
2020-09-13 22:00:47 +02:00
|
|
|
DEBUGFS_PRINTF("Found at pos %d, took %d ms", f.position(), millis() - s);
|
2020-05-08 22:53:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:00:47 +02:00
|
|
|
DEBUGFS_PRINTF("No match, took %d ms\n", millis() - s);
|
2020-05-08 22:53:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:00:47 +02:00
|
|
|
bool appendObjectToFile(File f, const char* key, JsonDocument* content, uint32_t s)
|
|
|
|
{
|
|
|
|
#ifdef WLED_DEBUG_FS
|
2020-09-14 00:31:38 +02:00
|
|
|
DEBUGFS_PRINTLN("Append");
|
2020-09-13 22:00:47 +02:00
|
|
|
uint32_t s1 = millis();
|
|
|
|
#endif
|
|
|
|
uint32_t pos = 0;
|
|
|
|
if (!f) return false;
|
|
|
|
if (f.size() < 3) f.print("{}");
|
|
|
|
|
|
|
|
//if there is enough empty space in file, insert there instead of appending
|
|
|
|
uint32_t contentLen = measureJson(*content);
|
|
|
|
DEBUGFS_PRINTF("CLen %d\n", contentLen);
|
|
|
|
if (bufferedFindSpace(contentLen + strlen(key) + 1, f)) {
|
|
|
|
if (f.position() > 2) f.write(','); //add comma if not first object
|
|
|
|
f.print(key);
|
|
|
|
serializeJson(*content, f);
|
2020-09-14 00:31:38 +02:00
|
|
|
DEBUGFS_PRINTF("Inserted, took %d ms (total %d)", millis() - s1, millis() - s);
|
2020-09-13 22:00:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check if last character in file is '}' (typical)
|
|
|
|
f.seek(1, SeekEnd);
|
|
|
|
if (f.read() == '}') pos = f.size() -1;
|
|
|
|
|
|
|
|
if (pos == 0) //not found
|
|
|
|
{
|
|
|
|
DEBUGFS_PRINTLN("not }");
|
|
|
|
while (bufferedFind("}",f)) //find last closing bracket in JSON if not last char
|
|
|
|
{
|
|
|
|
pos = f.position();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUGFS_PRINT("pos "); DEBUGFS_PRINTLN(pos);
|
|
|
|
if (pos > 2)
|
|
|
|
{
|
|
|
|
f.seek(pos, SeekSet);
|
|
|
|
f.write(',');
|
|
|
|
} else { //file content is not valid JSON object
|
|
|
|
f.seek(0, SeekSet);
|
|
|
|
f.write('{'); //start JSON
|
|
|
|
}
|
|
|
|
|
|
|
|
f.print(key);
|
|
|
|
|
|
|
|
//Append object
|
|
|
|
serializeJson(*content, f);
|
|
|
|
f.write('}');
|
|
|
|
|
|
|
|
f.close();
|
|
|
|
DEBUGFS_PRINTF("Appended, took %d ms (total %d)", millis() - s1, millis() - s);
|
|
|
|
}
|
|
|
|
|
2020-05-08 22:53:59 +02:00
|
|
|
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
|
|
|
{
|
2020-09-13 22:00:47 +02:00
|
|
|
uint32_t s = 0; //timing
|
|
|
|
#ifdef WLED_DEBUG_FS
|
|
|
|
DEBUGFS_PRINTF("Write to %s with key %s >>>\n", file, key);
|
|
|
|
serializeJson(*content, Serial); DEBUGFS_PRINTLN();
|
|
|
|
s = millis();
|
|
|
|
#endif
|
|
|
|
|
2020-05-05 09:01:09 +02:00
|
|
|
uint32_t pos = 0;
|
2020-09-09 09:51:04 +02:00
|
|
|
File f = WLED_FS.open(file, "r+");
|
2020-09-13 22:00:47 +02:00
|
|
|
if (!f && !WLED_FS.exists(file)) f = WLED_FS.open(file, "w+");
|
|
|
|
if (!f) {
|
|
|
|
DEBUGFS_PRINTLN("Failed to open!");
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-05 09:01:09 +02:00
|
|
|
|
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-09-13 22:00:47 +02:00
|
|
|
return appendObjectToFile(f, key, content, s);
|
2020-05-05 09:01:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//exists
|
|
|
|
pos = f.position();
|
|
|
|
//measure out end of old object
|
2020-09-07 20:39:12 +02:00
|
|
|
StaticJsonDocument<1024> doc;
|
2020-05-05 09:01:09 +02:00
|
|
|
deserializeJson(doc, f);
|
|
|
|
uint32_t pos2 = f.position();
|
2020-09-09 09:51:04 +02:00
|
|
|
|
2020-05-05 09:01:09 +02:00
|
|
|
uint32_t oldLen = pos2 - pos;
|
2020-09-13 22:00:47 +02:00
|
|
|
#ifdef WLED_DEBUG_FS
|
|
|
|
DEBUGFS_PRINTF("Old obj len %d >>> ", oldLen);
|
|
|
|
serializeJson(doc, Serial);
|
|
|
|
DEBUGFS_PRINTLN();
|
|
|
|
#endif
|
2020-05-05 09:01:09 +02:00
|
|
|
|
2020-05-08 22:53:59 +02:00
|
|
|
if (!content->isNull() && measureJson(*content) <= oldLen) //replace
|
2020-05-05 09:01:09 +02:00
|
|
|
{
|
2020-09-14 00:31:38 +02:00
|
|
|
DEBUGFS_PRINTLN("replace");
|
2020-09-07 20:39:12 +02:00
|
|
|
f.seek(pos);
|
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
|
2020-09-14 00:31:38 +02:00
|
|
|
DEBUGFS_PRINTLN("delete");
|
2020-09-07 20:39:12 +02:00
|
|
|
pos -= strlen(key);
|
2020-09-09 09:51:04 +02:00
|
|
|
if (pos > 3) pos--; //also delete leading comma if not first object
|
2020-09-07 20:39:12 +02:00
|
|
|
f.seek(pos);
|
2020-05-05 09:01:09 +02:00
|
|
|
for (uint32_t i = pos; i < pos2; i++) {
|
|
|
|
f.write(' ');
|
|
|
|
}
|
2020-09-13 22:00:47 +02:00
|
|
|
if (!content->isNull()) return appendObjectToFile(f, key, content, s);
|
2020-05-05 09:01:09 +02:00
|
|
|
}
|
2020-05-08 22:53:59 +02:00
|
|
|
f.close();
|
2020-09-13 22:00:47 +02:00
|
|
|
DEBUGFS_PRINTF("Deleted, took %d ms\n", millis() - s);
|
|
|
|
return true;
|
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)
|
|
|
|
{
|
2020-09-13 22:00:47 +02:00
|
|
|
#ifdef WLED_DEBUG_FS
|
|
|
|
DEBUGFS_PRINTF("Read from %s with key %s >>>\n", file, key);
|
|
|
|
uint32_t s = millis();
|
|
|
|
#endif
|
2020-09-09 09:51:04 +02:00
|
|
|
File f = WLED_FS.open(file, "r");
|
2020-05-05 09:01:09 +02:00
|
|
|
if (!f) return false;
|
2020-09-09 09:51:04 +02:00
|
|
|
|
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
|
|
|
{
|
|
|
|
f.close();
|
2020-09-13 22:00:47 +02:00
|
|
|
DEBUGFS_PRINTLN("Obj not found.");
|
2020-05-05 09:01:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializeJson(*dest, f);
|
|
|
|
|
|
|
|
f.close();
|
2020-09-13 22:00:47 +02:00
|
|
|
DEBUGFS_PRINTF("Read, took %d ms\n", millis() - s);
|
2020-05-05 09:01:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-31 00:38:51 +01:00
|
|
|
|
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";
|
2016-11-19 19:39:17 +01:00
|
|
|
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";
|
2016-11-19 19:39:17 +01:00
|
|
|
else if(filename.endsWith(".png")) return "image/png";
|
2019-03-16 02:09:37 +01:00
|
|
|
// else if(filename.endsWith(".gif")) return "image/gif";
|
2016-11-19 19:39:17 +01:00
|
|
|
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";
|
2016-11-19 19:39:17 +01:00
|
|
|
return "text/plain";
|
|
|
|
}
|
|
|
|
|
2019-03-16 02:09:37 +01:00
|
|
|
bool handleFileRead(AsyncWebServerRequest* request, String path){
|
|
|
|
DEBUG_PRINTLN("FileRead: " + path);
|
2016-11-19 19:39:17 +01:00
|
|
|
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";
|
2020-09-09 09:51:04 +02:00
|
|
|
if(WLED_FS.exists(pathWithGz)){
|
|
|
|
request->send(WLED_FS, pathWithGz, contentType);
|
2016-11-19 19:39:17 +01:00
|
|
|
return true;
|
2020-05-05 09:01:09 +02:00
|
|
|
}*/
|
2020-09-09 09:51:04 +02:00
|
|
|
if(WLED_FS.exists(path)) {
|
|
|
|
request->send(WLED_FS, path, contentType);
|
2019-03-16 02:09:37 +01:00
|
|
|
return true;
|
2016-11-26 19:34:05 +01:00
|
|
|
}
|
2019-03-16 02:09:37 +01:00
|
|
|
return false;
|
2016-11-19 19:39:17 +01:00
|
|
|
}
|
2017-05-08 21:00:06 +02:00
|
|
|
|
|
|
|
#else
|
2019-03-16 02:09:37 +01:00
|
|
|
bool handleFileRead(AsyncWebServerRequest*, String path){return false;}
|
2017-05-08 21:00:06 +02:00
|
|
|
#endif
|