Add filsystem_list

This commit is contained in:
fvanroie 2023-01-16 23:51:24 +01:00
parent 0dd8e3dc2b
commit eaa9d34a1a
2 changed files with 58 additions and 9 deletions

View File

@ -1,8 +1,10 @@
/* MIT License - Copyright (c) 2019-2022 Francis Van Roie
/* MIT License - Copyright (c) 2019-2023 Francis Van Roie
For full license information read the LICENSE file in the project folder */
#ifdef ARDUINO
#include <Arduino.h>
#include "ArduinoLog.h"
#include "FS.h"
#endif
#include "hasp_conf.h" // include first
@ -10,12 +12,12 @@
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
#ifndef HASP_ONLINE_CMD
#define HASP_ONLINE_CMD "jsonl {\"page\":0,\"id\":239,\"obj\":\"msgbox\",\"text\":\"%ip%\", \"auto_close\":20000}"
#define HASP_ONLINE_CMD "jsonl {\"page\":0,\"id\":239,\"obj\":\"msgbox\",\"text\":\"%ip%\",\"auto_close\":20000}"
#endif
#ifndef HASP_OFFLINE_CMD
#define HASP_OFFLINE_CMD \
"jsonl {\"page\":0,\"id\":239,\"obj\":\"msgbox\",\"text\":\"" D_NETWORK_OFFLINE "\", \"auto_close\":20000}"
"jsonl {\"page\":0,\"id\":239,\"obj\":\"msgbox\",\"text\":\"" D_NETWORK_OFFLINE "\",\"auto_close\":20000}"
#endif
#ifndef HASP_PAGES_JSONL
@ -70,7 +72,7 @@ void filesystemUnzip(const char*, const char* filename, uint8_t source)
// fh.filename_length, fh.extra_length);
}
char name[257] = {0};
name[0] = '/';
name[0] = '/';
len = zipfile.read((uint8_t*)&name[1], fh.filename_length);
if(len != fh.filename_length) {
@ -194,6 +196,50 @@ void filesystemList()
#endif
}
#if defined(ARDUINO_ARCH_ESP32)
String filesystem_list(fs::FS& fs, const char* dirname, uint8_t levels)
{
LOG_VERBOSE(TAG_FILE, "Listing directory: %s\n", dirname);
String data = "[";
File root = fs.open(dirname);
if(!root) {
LOG_WARNING(TAG_FILE, "Failed to open directory");
} else if(!root.isDirectory()) {
LOG_WARNING(TAG_FILE, "Not a directory");
} else {
File file = root.openNextFile();
while(file) {
if(data != "[") {
data += ",";
}
data += "{\"name\":\"";
data += file.name();
data += "\"";
if(file.isDirectory()) {
data += ",\"children\":";
if(levels) {
String dir = dirname;
dir += file.name();
dir += '/';
data += filesystem_list(fs, dir.c_str(), levels - 1);
} else {
data += "[]";
}
}
data += "}";
file = root.openNextFile();
}
root.close();
}
data += "]";
return data;
}
#endif
static void filesystem_write_file(const char* filename, const char* data)
{
if(HASP_FS.exists(filename)) return;

View File

@ -1,10 +1,11 @@
/* MIT License - Copyright (c) 2019-2022 Francis Van Roie
/* MIT License - Copyright (c) 2019-2023 Francis Van Roie
For full license information read the LICENSE file in the project folder */
#ifndef HASP_FILESYSTEM_H
#define HASP_FILESYSTEM_H
#include <Arduino.h>
#include <FS.h>
bool filesystemSetup(void);
@ -12,10 +13,6 @@ void filesystemList();
void filesystemInfo();
void filesystemSetupFiles();
#if defined(ARDUINO_ARCH_ESP32)
void filesystemUnzip(const char*, const char* filename, uint8_t source);
#endif
enum { ZIP_NO_COMPRESSION = 0, ZIP_DEFLTATE = 8 };
typedef uint16_t zip_compression_method_t;
@ -54,6 +51,7 @@ typedef struct
#endif // ESP_ARDUINO_VERSION
#endif
#elif defined(ARDUINO_ARCH_ESP8266)
// included by default
#include <LittleFS.h>
@ -65,4 +63,9 @@ typedef struct
#include <Esp.h>
#endif // ARDUINO_ARCH
#if defined(ARDUINO_ARCH_ESP32)
void filesystemUnzip(const char*, const char* filename, uint8_t source);
String filesystem_list(fs::FS& fs, const char* dirname, uint8_t levels);
#endif
#endif // HASP_FILESYSTEM_H