Организация файлов, компилируемая версия

This commit is contained in:
Dmitry Borisenko
2021-12-22 14:49:25 +01:00
parent 24a9d55ea0
commit d208bbd426
9 changed files with 111 additions and 104 deletions

76
src/EspFileSystem.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include "EspFileSystem.h"
bool fileSystemInit() {
if (!FileFS.begin()) {
Serial.println("FS Init ERROR, may be FS was not flashed");
return false;
}
Serial.println("FS Init completed");
return true;
}
File seekFile(const String& filename, size_t position) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
Serial.println("[E] file error");
}
file.seek(position, SeekSet);
return file;
}
const String writeFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.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 = FileFS.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 filepath(const String& filename) {
return filename.startsWith("/") ? filename : "/" + filename;
}
bool cutFile(const String& src, const String& dst) {
String srcPath = filepath(src);
String dstPath = filepath(dst);
Serial.println("cut " + srcPath + " to " + dstPath);
if (!FileFS.exists(srcPath)) {
Serial.println("not exist: " + srcPath);
return false;
}
if (FileFS.exists(dstPath)) {
FileFS.remove(dstPath);
}
auto srcFile = FileFS.open(srcPath, "r");
auto dstFile = FileFS.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();
FileFS.remove(srcPath);
return true;
}

View File

@@ -1,6 +1,4 @@
#include "main.h"
#include "rest.h"
#include "Main.h"
void setup() {
Serial.begin(115200);

View File

@@ -1,55 +1,5 @@
#include "rest.h"
File seekFile(const String& filename, size_t position) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
Serial.println("[E] file error");
}
file.seek(position, SeekSet);
return file;
}
const String writeFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.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 = FileFS.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 filepath(const String& filename) {
return filename.startsWith("/") ? filename : "/" + filename;
}
bool fileSystemInit() {
if (!FileFS.begin()) {
Serial.println("FS Init ERROR, may be FS was not flashed");
return false;
}
Serial.println("FS Init completed");
return true;
}
String prettyBytes(size_t size) {
if (size < 1024)
return String(size) + "b";
@@ -60,28 +10,3 @@ String prettyBytes(size_t size) {
else
return String(size / 1024.0 / 1024.0 / 1024.0) + "GB";
}
bool cutFile(const String& src, const String& dst) {
String srcPath = filepath(src);
String dstPath = filepath(dst);
Serial.println("cut " + srcPath + " to " + dstPath);
if (!FileFS.exists(srcPath)) {
Serial.println("not exist: " + srcPath);
return false;
}
if (FileFS.exists(dstPath)) {
FileFS.remove(dstPath);
}
auto srcFile = FileFS.open(srcPath, "r");
auto dstFile = FileFS.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();
FileFS.remove(srcPath);
return true;
}