2020-09-02 22:34:49 +03:00
|
|
|
|
#include "Utils/FileUtils.h"
|
|
|
|
|
|
#include "Utils/PrintMessage.h"
|
|
|
|
|
|
|
2020-09-17 17:27:44 +03:00
|
|
|
|
|
2020-09-02 22:34:49 +03:00
|
|
|
|
|
|
|
|
|
|
const String filepath(const String& filename) {
|
|
|
|
|
|
return filename.startsWith("/") ? filename : "/" + filename;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool fileSystemInit() {
|
|
|
|
|
|
if (!LittleFS.begin()) {
|
2020-09-17 17:27:44 +03:00
|
|
|
|
SerialPrint("[E]","module","init");
|
2020-09-02 22:34:49 +03:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void removeFile(const String& filename) {
|
|
|
|
|
|
String path = filepath(filename);
|
|
|
|
|
|
if (LittleFS.exists(path)) {
|
|
|
|
|
|
if (!LittleFS.remove(path)) {
|
2020-09-17 17:27:44 +03:00
|
|
|
|
SerialPrint("[E]","module","remove " + path);
|
2020-09-02 22:34:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2020-09-17 17:27:44 +03:00
|
|
|
|
SerialPrint("I","module","not exist" + path);
|
2020-09-02 22:34:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
File seekFile(const String& filename, size_t position) {
|
|
|
|
|
|
String path = filepath(filename);
|
|
|
|
|
|
auto file = LittleFS.open(path, "r");
|
|
|
|
|
|
if (!file) {
|
2020-09-17 17:27:44 +03:00
|
|
|
|
SerialPrint("[E]","module","open " + path);
|
2020-09-02 22:34:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
// поставим курсор в начало файла
|
|
|
|
|
|
file.seek(position, SeekSet);
|
|
|
|
|
|
return file;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String readFileString(const String& filename, const String& to_find) {
|
|
|
|
|
|
String path = filepath(filename);
|
|
|
|
|
|
String res = "failed";
|
|
|
|
|
|
auto file = LittleFS.open(path, "r");
|
|
|
|
|
|
if (!file) {
|
|
|
|
|
|
return "failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (file.find(to_find.c_str())) {
|
|
|
|
|
|
res = file.readStringUntil('\n');
|
|
|
|
|
|
}
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String addFileLn(const String& filename, const String& str) {
|
|
|
|
|
|
String path = filepath(filename);
|
|
|
|
|
|
auto file = LittleFS.open(path, "a");
|
|
|
|
|
|
if (!file) {
|
|
|
|
|
|
return "failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
file.println(str);
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return "sucсess";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String addFile(const String& filename, const String& str) {
|
|
|
|
|
|
String path = filepath(filename);
|
|
|
|
|
|
auto file = LittleFS.open(path, "a");
|
|
|
|
|
|
if (!file) {
|
|
|
|
|
|
return "failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
file.print(str);
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return "sucсess";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool copyFile(const String& src, const String& dst, bool overwrite) {
|
|
|
|
|
|
String srcPath = filepath(src);
|
|
|
|
|
|
String dstPath = filepath(dst);
|
2020-09-17 17:27:44 +03:00
|
|
|
|
SerialPrint("I","module","copy " + srcPath + " to " + dstPath);
|
2020-09-02 22:34:49 +03:00
|
|
|
|
if (!LittleFS.exists(srcPath)) {
|
2020-09-17 17:27:44 +03:00
|
|
|
|
SerialPrint("[E]","module","not exist: " + srcPath);
|
2020-09-02 22:34:49 +03:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (LittleFS.exists(dstPath)) {
|
|
|
|
|
|
if (!overwrite) {
|
2020-09-17 17:27:44 +03:00
|
|
|
|
SerialPrint("[E]","module","already exist: " + dstPath);
|
2020-09-02 22:34:49 +03:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
LittleFS.remove(dstPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
auto srcFile = LittleFS.open(srcPath, "r");
|
|
|
|
|
|
auto dstFile = LittleFS.open(dstPath, "w");
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t buf[512];
|
|
|
|
|
|
while (srcFile.available()) {
|
|
|
|
|
|
size_t len = srcFile.read(buf, 512);
|
|
|
|
|
|
dstFile.write(buf, len);
|
|
|
|
|
|
}
|
|
|
|
|
|
srcFile.close();
|
|
|
|
|
|
dstFile.close();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String writeFile(const String& filename, const String& str) {
|
|
|
|
|
|
String path = filepath(filename);
|
|
|
|
|
|
auto file = LittleFS.open(path, "w");
|
|
|
|
|
|
if (!file) {
|
|
|
|
|
|
return "failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
file.print(str);
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return "sucсess";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String readFile(const String& filename, size_t max_size) {
|
|
|
|
|
|
String path = filepath(filename);
|
|
|
|
|
|
auto file = LittleFS.open(path, "r");
|
|
|
|
|
|
if (!file) {
|
|
|
|
|
|
return "failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
size_t size = file.size();
|
|
|
|
|
|
if (size > max_size) {
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return "large";
|
|
|
|
|
|
}
|
|
|
|
|
|
String temp = file.readString();
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return temp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String getFileSize(const String filename) {
|
|
|
|
|
|
String filepath(filename);
|
|
|
|
|
|
auto file = LittleFS.open(filepath, "r");
|
|
|
|
|
|
if (!file) {
|
|
|
|
|
|
return "failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
size_t size = file.size();
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return String(size);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String getFSSizeInfo() {
|
|
|
|
|
|
String res;
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
|
FSInfo info;
|
|
|
|
|
|
if (LittleFS.info(info)) {
|
|
|
|
|
|
res = prettyBytes(info.usedBytes) + " of " + prettyBytes(info.totalBytes);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res = "error";
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
res = prettyBytes(LittleFS.usedBytes()) + " of " + prettyBytes(LittleFS.totalBytes());
|
|
|
|
|
|
#endif
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String getConfigFile(uint8_t preset, ConfigType_t type) {
|
|
|
|
|
|
char buf[64];
|
|
|
|
|
|
sprintf(buf, "/conf/%s%03d.txt", (type == CT_CONFIG) ? "c" : "s", preset);
|
|
|
|
|
|
return String(buf);
|
|
|
|
|
|
}
|