libretiny

This commit is contained in:
Mit4el
2024-09-20 12:45:17 +03:00
parent b32abb5a28
commit 596eb9cad4
78 changed files with 8577 additions and 63 deletions

View File

@@ -231,7 +231,12 @@ bool handleFileRead(String path) {
return the path of the closest parent still existing
*/
String lastExistingParent(String path) {
while (!path.isEmpty() && !FileFS.exists(path)) {
#ifndef LIBRETINY
while (!path.isEmpty() && !FileFS.exists(path))
#else
while (!path.length()==0 && !FileFS.exists(path))
#endif
{
if (path.lastIndexOf('/') > 0) {
path = path.substring(0, path.lastIndexOf('/'));
} else {
@@ -278,7 +283,7 @@ void handleFileUpload() {
}
}
#ifdef ESP8266
#if defined ESP8266
void deleteRecursive(String path) {
File file = FileFS.open(path, "r");
bool isDir = file.isDirectory();
@@ -299,14 +304,14 @@ void deleteRecursive(String path) {
}
#endif
#ifdef ESP32
#if defined ESP32 || defined LIBRETINY
struct treename {
uint8_t type;
char *name;
};
void deleteRecursive(String path) {
fs::File dir = FileFS.open(path);
fs::File dir = FileFS.open(path.c_str());
if (!dir.isDirectory()) {
Serial.printf("%s is a file\n", path);
@@ -321,7 +326,11 @@ void deleteRecursive(String path) {
while (entry = dir.openNextFile()) {
if (entry.isDirectory()) {
#if defined ESP32
deleteRecursive(entry.path());
#elif defined LIBRETINY
deleteRecursive(entry.fullName());
#endif
} else {
String tmpname = path + "/" + strdup(entry.name()); // buffer file name
entry.close();
@@ -342,10 +351,15 @@ void deleteRecursive(String path) {
*/
void handleFileDelete() {
String path = HTTP.arg(0);
#ifndef LIBRETINY
if (path.isEmpty() || path == "/") {
return replyBadRequest("BAD PATH");
}
#else
if (path.length()==0 || path == "/") {
return replyBadRequest("BAD PATH");
}
#endif
// DBG_OUTPUT_PORT.println(String("handleFileDelete: ") + path);
if (!FileFS.exists(path)) {
return replyNotFound(FPSTR(FILE_NOT_FOUND));
@@ -368,10 +382,15 @@ void handleFileDelete() {
*/
void handleFileCreate() {
String path = HTTP.arg("path");
#ifndef LIBRETINY
if (path.isEmpty()) {
return replyBadRequest(F("PATH ARG MISSING"));
}
#else
if (path.length()==0) {
return replyBadRequest(F("PATH ARG MISSING"));
}
#endif
#ifdef USE_SPIFFS
if (checkForUnsupportedPath(path).length() > 0) {
return replyServerError(F("INVALID FILENAME"));
@@ -386,7 +405,11 @@ void handleFileCreate() {
}
String src = HTTP.arg("src");
#ifndef LIBRETINY
if (src.isEmpty()) {
#else
if (src.length()==0) {
#endif
// No source specified: creation
// DBG_OUTPUT_PORT.println(String("handleFileCreate: ") + path);
if (path.endsWith("/")) {
@@ -404,6 +427,9 @@ void handleFileCreate() {
#endif
#ifdef ESP32
file.write(0);
#endif
#ifdef LIBRETINY
file.write((uint8_t)0);
#endif
file.close();
} else {
@@ -509,7 +535,7 @@ void handleFileList() {
}
#endif
#ifdef ESP32
#if defined ESP32
void handleFileList() {
if (!HTTP.hasArg("dir")) {
HTTP.send(500, "text/plain", "BAD ARGS");
@@ -517,15 +543,29 @@ void handleFileList() {
}
String path = HTTP.arg("dir");
// DBG_OUTPUT_PORT.println("handleFileList: " + path);
if (path != "/" && !FileFS.exists(path)) {
return replyBadRequest("BAD PATH");
}
//path = "/build/";
Serial.println("handleFileList: " + path);
#if defined LIBRETINY
File root = FileFS.open(path.c_str());
//Dir root = FileFS.openDir(path);
Serial.println("handleFileList FIRST OPEN Name: " + String(root.name()));
#else
File root = FileFS.open(path);
#endif
path = String();
String output = "[";
if (root.isDirectory()) {
Serial.println("handleFileList IS DIR: " + String(root.name()));
//root.close();
File file = root.openNextFile();
Serial.println("handleFileList openNextFile: " + String(file.name()));
while (file) {
if (output != "[") {
output += ',';
}
@@ -549,6 +589,72 @@ void handleFileList() {
}
#endif
#if defined LIBRETINY
void handleFileList() {
if (!HTTP.hasArg("dir")) {
HTTP.send(500, "text/plain", "BAD ARGS");
return;
}
String path = HTTP.arg("dir");
if (path != "/" && !FileFS.exists(path)) {
return replyBadRequest("BAD PATH");
}
lfs_dir_t dir;
struct lfs_info info;
int err = lfs_dir_open(&lfs, &dir, path);
if (err) {
HTTP.send(500, "text/plain", "FAIL OPEN DIR");
return;
}
String output = "[";
while (true) {
int res = lfs_dir_read(&lfs, &dir, &info);
if (res < 0) {
lfs_dir_close(&lfs, &dir);
return err;
}
if (!res) {
break;
}
Serial.printf("%s %d", info.name, info.type);
if (output != "[") {
output += ',';
}
output += "{\"type\":\"";
// output += (file.isDirectory()) ? "dir" : "file";
if (info.type == LFS_TYPE_DIR) {
output += "dir";
} else {
output += F("file\",\"size\":\"");
output += info.size;
}
output += "\",\"name\":\"";
output += String(info.name);
output += "\"}";
//file = root.openNextFile();
}
err = lfs_dir_close(&lfs, &dir);
if (err) {
HTTP.send(500, "text/plain", "FAIL CLOSE DIR");
return;
}
output += "]";
HTTP.send(200, "text/json", output);
}
#endif
/*
The "Not Found" handler catches all URI not explicitly declared in code
First try to find and return the requested file from the filesystem,
@@ -560,6 +666,9 @@ void handleNotFound() {
#endif
#ifdef ESP32
String uri = WebServer::urlDecode(HTTP.uri()); // required to read paths with blanks
#endif
#ifdef LIBRETINY
String uri = WebServer::urlDecode(HTTP.uri()); // required to read paths with blanks
#endif
if (handleFileRead(uri)) {
return;