Tasmota/tasmota/xdrv_50_filesystem.ino

1135 lines
31 KiB
Arduino
Raw Normal View History

2020-12-31 13:19:50 +00:00
/*
2021-01-06 13:41:23 +00:00
xdrv_50_filesystem.ino - unified file system for Tasmota
2020-12-31 13:19:50 +00:00
Copyright (C) 2020 Gerhard Mutz and Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-01-04 14:52:32 +00:00
#ifdef USE_UFILESYS
/*********************************************************************************************\
This driver adds universal file system support for
- ESP8266 (sd card or littlefs on > 1 M devices with special linker file e.g. eagle.flash.4m2m.ld)
(makes no sense on 1M devices without sd card)
- ESP32 (sd card or littlefs or sfatfile system).
2021-01-04 14:52:32 +00:00
The sd card chip select is the standard SDCARD_CS or when not found SDCARD_CS_PIN and initializes
the FS System Pointer ufsp which can be used by all standard file system calls.
The only specific call is UfsInfo() which gets the total size (0) and free size (1).
2021-01-04 14:52:32 +00:00
A button is created in the setup section to show up the file directory to download and upload files
subdirectories are supported.
2020-12-31 13:19:50 +00:00
Supported commands:
2020-12-31 13:19:50 +00:00
ufs fs info
ufstype get filesytem type 0=none 1=SD 2=Flashfile
ufssize total size in kB
ufsfree free size in kB
2021-01-04 14:52:32 +00:00
\*********************************************************************************************/
2020-12-31 13:19:50 +00:00
2021-01-06 13:41:23 +00:00
#define XDRV_50 50
2020-12-31 13:19:50 +00:00
2021-01-04 14:52:32 +00:00
#ifndef SDCARD_CS_PIN
#define SDCARD_CS_PIN 4
#endif
2020-12-31 13:19:50 +00:00
#define UFS_TNONE 0
#define UFS_TSDC 1
#define UFS_TFAT 2
#define UFS_TLFS 3
2021-01-20 14:43:26 +00:00
/*
// In tasmota.ino
2020-12-31 13:19:50 +00:00
#ifdef ESP8266
#include <LittleFS.h>
#include <SPI.h>
#ifdef USE_SDCARD
2020-12-31 13:19:50 +00:00
#include <SD.h>
#include <SDFAT.h>
2021-01-04 14:52:32 +00:00
#endif // USE_SDCARD
#endif // ESP8266
#ifdef ESP32
2020-12-31 17:05:42 +00:00
#include <LITTLEFS.h>
#ifdef USE_SDCARD
2020-12-31 13:19:50 +00:00
#include <SD.h>
2021-01-04 14:52:32 +00:00
#endif // USE_SDCARD
2020-12-31 13:19:50 +00:00
#include "FFat.h"
#include "FS.h"
2021-01-04 14:52:32 +00:00
#endif // ESP32
2021-01-20 14:43:26 +00:00
*/
2020-12-31 13:19:50 +00:00
2021-01-15 15:17:25 +00:00
// Global file system pointer
2020-12-31 13:19:50 +00:00
FS *ufsp;
2021-01-15 15:17:25 +00:00
// Flash file system pointer
2021-01-06 09:51:22 +00:00
FS *ffsp;
2021-01-15 15:17:25 +00:00
// Local pointer for file managment
2021-01-07 09:57:24 +00:00
FS *dfsp;
2020-12-31 13:19:50 +00:00
char ufs_path[48];
File ufs_upload_file;
2021-01-07 09:57:24 +00:00
uint8_t ufs_dir;
2021-01-15 15:17:25 +00:00
// 0 = None, 1 = SD, 2 = ffat, 3 = littlefs
2020-12-31 13:19:50 +00:00
uint8_t ufs_type;
2021-01-07 09:57:24 +00:00
uint8_t ffs_type;
struct {
2021-02-16 11:19:40 +00:00
char run_file[48];
int run_file_pos = -1;
bool run_file_mutex = 0;
bool download_busy;
} UfsData;
2021-01-11 16:44:54 +00:00
/*********************************************************************************************/
2020-12-31 13:19:50 +00:00
2021-01-15 15:17:25 +00:00
// Init flash file system
2021-01-08 15:22:06 +00:00
void UfsInitOnce(void) {
2020-12-31 13:19:50 +00:00
ufs_type = 0;
2021-01-06 09:51:22 +00:00
ffsp = 0;
2021-01-07 09:57:24 +00:00
ufs_dir = 0;
2021-01-01 10:38:01 +00:00
2020-12-31 13:19:50 +00:00
#ifdef ESP8266
2021-01-08 18:28:05 +00:00
ffsp = &LittleFS;
2020-12-31 17:05:42 +00:00
if (!LittleFS.begin()) {
ffsp = nullptr;
2020-12-31 13:19:50 +00:00
return;
}
2021-01-04 14:52:32 +00:00
#endif // ESP8266
2021-01-08 18:28:05 +00:00
2021-01-04 14:52:32 +00:00
#ifdef ESP32
2020-12-31 19:22:54 +00:00
// try lfs first
2021-07-18 22:14:10 +01:00
ffsp = &LittleFS;
if (!LittleFS.begin(true)) {
2020-12-31 19:22:54 +00:00
// ffat is second
2021-01-08 18:28:05 +00:00
ffsp = &FFat;
2020-12-31 19:22:54 +00:00
if (!FFat.begin(true)) {
ffsp = nullptr;
2020-12-31 15:41:58 +00:00
return;
}
2021-01-08 18:28:05 +00:00
ffs_type = UFS_TFAT;
ufs_type = ffs_type;
ufsp = ffsp;
dfsp = ffsp;
2020-12-31 13:19:50 +00:00
return;
}
2021-01-04 14:52:32 +00:00
#endif // ESP32
2021-01-08 18:28:05 +00:00
ffs_type = UFS_TLFS;
ufs_type = ffs_type;
ufsp = ffsp;
dfsp = ffsp;
}
2021-02-16 11:19:40 +00:00
// Called from tasmota.ino at restart. This inits flash file only
2021-01-08 15:22:06 +00:00
void UfsInit(void) {
2021-02-16 11:19:40 +00:00
UfsData.run_file_pos = -1;
2021-01-08 15:22:06 +00:00
UfsInitOnce();
if (ufs_type) {
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("UFS: FlashFS mounted with %d kB free"), UfsInfo(1, 0));
2021-01-08 18:28:05 +00:00
}
}
#ifdef USE_SDCARD
void UfsCheckSDCardInit(void) {
if (TasmotaGlobal.spi_enabled) {
int8_t cs = SDCARD_CS_PIN;
if (PinUsed(GPIO_SDCARD_CS)) {
cs = Pin(GPIO_SDCARD_CS);
}
2021-10-19 07:08:55 +01:00
2021-01-09 07:51:27 +00:00
#ifdef EPS8266
SPI.begin();
#endif // EPS8266
#ifdef ESP32
SPI.begin(Pin(GPIO_SPI_CLK), Pin(GPIO_SPI_MISO), Pin(GPIO_SPI_MOSI), -1);
#endif // ESP32
2021-01-08 18:28:05 +00:00
if (SD.begin(cs)) {
#ifdef ESP8266
ufsp = &SDFS;
#endif // ESP8266
#ifdef ESP32
ufsp = &SD;
#endif // ESP32
ufs_type = UFS_TSDC;
dfsp = ufsp;
2021-01-09 07:51:27 +00:00
if (ffsp) {ufs_dir = 1;}
2021-01-08 18:28:05 +00:00
// make sd card the global filesystem
#ifdef ESP8266
// on esp8266 sdcard info takes several seconds !!!, so we ommit it here
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("UFS: SDCard mounted"));
2021-01-08 18:28:05 +00:00
#endif // ESP8266
#ifdef ESP32
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("UFS: SDCard mounted with %d kB free"), UfsInfo(1, 0));
2021-01-08 18:28:05 +00:00
#endif // ESP32
}
2021-01-08 15:22:06 +00:00
}
}
2021-01-08 18:28:05 +00:00
#endif // USE_SDCARD
2021-01-08 15:22:06 +00:00
uint32_t UfsInfo(uint32_t sel, uint32_t type) {
2021-01-10 18:42:33 +00:00
uint64_t result = 0;
2021-01-07 09:57:24 +00:00
FS *ifsp = ufsp;
uint8_t itype = ufs_type;
if (type) {
ifsp = ffsp;
itype = ffs_type;
}
2021-01-04 14:52:32 +00:00
2021-01-01 15:48:52 +00:00
#ifdef ESP8266
2021-01-04 14:52:32 +00:00
FSInfo64 fsinfo;
#endif // ESP8266
2020-12-31 13:19:50 +00:00
2021-01-07 09:57:24 +00:00
switch (itype) {
2020-12-31 13:19:50 +00:00
case UFS_TSDC:
#ifdef USE_SDCARD
2021-01-04 14:52:32 +00:00
#ifdef ESP8266
2021-01-07 09:57:24 +00:00
ifsp->info64(fsinfo);
2021-01-01 15:48:52 +00:00
if (sel == 0) {
result = fsinfo.totalBytes;
} else {
result = (fsinfo.totalBytes - fsinfo.usedBytes);
}
2021-01-04 14:52:32 +00:00
#endif // ESP8266
#ifdef ESP32
if (sel == 0) {
result = SD.totalBytes();
} else {
result = (SD.totalBytes() - SD.usedBytes());
}
#endif // ESP32
#endif // USE_SDCARD
2020-12-31 13:19:50 +00:00
break;
2020-12-31 17:05:42 +00:00
case UFS_TLFS:
2020-12-31 13:19:50 +00:00
#ifdef ESP8266
2021-01-07 09:57:24 +00:00
ifsp->info64(fsinfo);
2020-12-31 13:19:50 +00:00
if (sel == 0) {
result = fsinfo.totalBytes;
} else {
result = (fsinfo.totalBytes - fsinfo.usedBytes);
}
2021-01-04 14:52:32 +00:00
#endif // ESP8266
#ifdef ESP32
2020-12-31 17:05:42 +00:00
if (sel == 0) {
2021-07-18 22:14:10 +01:00
result = LittleFS.totalBytes();
2020-12-31 17:05:42 +00:00
} else {
2021-07-18 22:14:10 +01:00
result = LittleFS.totalBytes() - LittleFS.usedBytes();
2020-12-31 17:05:42 +00:00
}
#endif // ESP32
2020-12-31 17:05:42 +00:00
break;
case UFS_TFAT:
#ifdef ESP32
2020-12-31 13:19:50 +00:00
if (sel == 0) {
result = FFat.totalBytes();
} else {
result = FFat.freeBytes();
}
#endif // ESP32
2020-12-31 13:19:50 +00:00
break;
2020-12-31 17:05:42 +00:00
2020-12-31 13:19:50 +00:00
}
return result / 1024;
2020-12-31 13:19:50 +00:00
}
#if USE_LONG_FILE_NAMES>0
#undef REJCMPL
#define REJCMPL 6
#else
#undef REJCMPL
#define REJCMPL 8
#endif
uint8_t UfsReject(char *name) {
2020-12-31 13:19:50 +00:00
char *lcp = strrchr(name,'/');
if (lcp) {
name = lcp + 1;
}
2021-01-04 14:52:32 +00:00
while (*name=='/') { name++; }
if (*name=='.') { return 1; }
2020-12-31 13:19:50 +00:00
2021-01-04 14:52:32 +00:00
if (!strncasecmp(name, "SPOTLI~1", REJCMPL)) { return 1; }
if (!strncasecmp(name, "TRASHE~1", REJCMPL)) { return 1; }
if (!strncasecmp(name, "FSEVEN~1", REJCMPL)) { return 1; }
if (!strncasecmp(name, "SYSTEM~1", REJCMPL)) { return 1; }
if (!strncasecmp(name, "System Volume", 13)) { return 1; }
2020-12-31 13:19:50 +00:00
return 0;
}
/*********************************************************************************************\
* Tfs low level functions
\*********************************************************************************************/
bool TfsFileExists(const char *fname){
2021-01-15 15:17:25 +00:00
if (!ffs_type) { return false; }
bool yes = ffsp->exists(fname);
if (!yes) {
AddLog(LOG_LEVEL_DEBUG, PSTR("TFS: File '%s' not found"), fname +1); // Skip leading slash
}
return yes;
}
bool TfsSaveFile(const char *fname, const uint8_t *buf, uint32_t len) {
2021-01-15 15:17:25 +00:00
if (!ffs_type) { return false; }
#ifdef USE_WEBCAM
WcInterrupt(0); // Stop stream if active to fix TG1WDT_SYS_RESET
#endif
bool result = false;
File file = ffsp->open(fname, "w");
if (!file) {
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("TFS: Save failed"));
} else {
// This will timeout on ESP32-webcam
// But now solved with WcInterrupt(0) in support_esp.ino
file.write(buf, len);
/*
// This will still timeout on ESP32-webcam when wcresolution 10
uint32_t count = len / 512;
uint32_t chunk = len / count;
for (uint32_t i = 0; i < count; i++) {
file.write(buf + (i * chunk), chunk);
// do actually wait a little to allow ESP32 tasks to tick
// fixes task timeout in ESP32Solo1 style unicore code and webcam.
delay(10);
OsWatchLoop();
}
uint32_t left = len % count;
if (left) {
file.write(buf + (count * chunk), left);
}
*/
file.close();
result = true;
}
#ifdef USE_WEBCAM
WcInterrupt(1);
#endif
return result;
}
bool TfsInitFile(const char *fname, uint32_t len, uint8_t init_value) {
2021-01-15 15:17:25 +00:00
if (!ffs_type) { return false; }
File file = ffsp->open(fname, "w");
if (!file) {
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("TFS: Erase failed"));
return false;
}
for (uint32_t i = 0; i < len; i++) {
file.write(&init_value, 1);
}
file.close();
return true;
}
bool TfsLoadFile(const char *fname, uint8_t *buf, uint32_t len) {
2021-01-15 15:17:25 +00:00
if (!ffs_type) { return false; }
if (!TfsFileExists(fname)) { return false; }
File file = ffsp->open(fname, "r");
if (!file) {
AddLog(LOG_LEVEL_INFO, PSTR("TFS: File '%s' not found"), fname +1); // Skip leading slash
return false;
}
2022-01-09 17:24:39 +00:00
size_t flen = file.size();
if (len > flen){
len = flen;
}
file.read(buf, len);
file.close();
return true;
}
2021-01-08 15:22:06 +00:00
bool TfsDeleteFile(const char *fname) {
2021-01-15 15:17:25 +00:00
if (!ffs_type) { return false; }
2021-01-08 15:22:06 +00:00
if (!ffsp->remove(fname)) {
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("TFS: Delete failed"));
2021-01-08 15:22:06 +00:00
return false;
}
return true;
}
bool TfsRenameFile(const char *fname1, const char *fname2) {
if (!ffs_type) { return false; }
if (!ffsp->rename(fname1, fname2)) {
AddLog(LOG_LEVEL_INFO, PSTR("TFS: Rename failed"));
return false;
}
return true;
}
/*********************************************************************************************\
2021-02-16 12:00:10 +00:00
* File command execute support
\*********************************************************************************************/
2021-02-16 15:21:46 +00:00
bool UfsExecuteCommandFileReady(void) {
2021-02-16 14:54:53 +00:00
return (UfsData.run_file_pos < 0); // Check file ready to disable concurrency
2021-02-16 12:00:10 +00:00
}
2021-02-16 11:19:40 +00:00
2021-02-16 15:21:46 +00:00
void UfsExecuteCommandFileLoop(void) {
if (UfsExecuteCommandFileReady() || !ffs_type) { return; }
if (BACKLOG_EMPTY && strlen(UfsData.run_file) && !UfsData.run_file_mutex) {
2021-02-16 11:19:40 +00:00
File file = ffsp->open(UfsData.run_file, "r");
if (!file || !file.seek(UfsData.run_file_pos)) {
2021-02-16 14:54:53 +00:00
UfsData.run_file_pos = -1; // Signal file ready
return;
}
2021-02-16 11:19:40 +00:00
UfsData.run_file_mutex = true;
char cmd_line[512];
cmd_line[0] = '\0'; // Clear in case of re-entry
while (file.available()) {
2021-02-16 11:19:40 +00:00
uint16_t index = 0;
bool comment = false;
2021-02-16 11:19:40 +00:00
while (file.available()) {
uint8_t buf[1];
file.read(buf, 1);
if ((buf[0] == '\n') || (buf[0] == '\r')) {
break; // End of command with linefeed or carriage return
2021-02-16 11:19:40 +00:00
}
// else if (index && !comment && (buf[0] == ';')) {
// break; // End of command on multi command line
// }
2021-02-16 11:19:40 +00:00
else if ((0 == index) && isspace(buf[0])) {
// Skip leading spaces (' ','\t','\n','\v','\f','\r')
}
else if ((0 == index) && (buf[0] == ';')) {
comment = true; // Ignore comment lines until linefeed or carriage return
2021-02-16 11:19:40 +00:00
}
else if (!comment && (index < sizeof(cmd_line) - 2)) {
cmd_line[index++] = buf[0]; // Build command
2021-02-16 11:19:40 +00:00
}
2021-02-15 17:01:02 +00:00
}
if ((index > 0) && (index < sizeof(cmd_line) - 1)) {
cmd_line[index] = '\0'; // Valid command received
2021-02-16 11:19:40 +00:00
break;
}
}
2021-02-16 11:19:40 +00:00
UfsData.run_file_pos = (file.available()) ? file.position() : -1;
file.close();
if (strlen(cmd_line)) {
ExecuteCommand(cmd_line, SRC_FILE);
}
2021-02-16 11:19:40 +00:00
UfsData.run_file_mutex = false;
}
2021-02-16 11:19:40 +00:00
}
2021-02-16 14:54:53 +00:00
bool UfsExecuteCommandFile(const char *fname) {
// Check for non-concurrency and file existance
2021-02-16 15:21:46 +00:00
if (UfsExecuteCommandFileReady() && TfsFileExists(fname)) {
2021-02-16 14:54:53 +00:00
snprintf(UfsData.run_file, sizeof(UfsData.run_file), fname);
UfsData.run_file_pos = 0; // Signal start of file
return true;
2021-02-16 11:19:40 +00:00
}
2021-02-16 14:54:53 +00:00
return false;
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
const int UFS_FILENAME_SIZE = 48;
char* UfsFilename(char* fname, char* fname_in) {
fname_in = Trim(fname_in); // Remove possible leading spaces
snprintf_P(fname, UFS_FILENAME_SIZE, PSTR("%s%s"), ('/' == fname_in[0]) ? "" : "/", fname_in);
return fname;
}
2021-01-08 15:22:06 +00:00
const char kUFSCommands[] PROGMEM = "Ufs|" // Prefix
2021-02-16 11:19:40 +00:00
"|Type|Size|Free|Delete|Rename|Run";
2020-12-31 13:19:50 +00:00
void (* const kUFSCommand[])(void) PROGMEM = {
2021-02-16 11:19:40 +00:00
&UFSInfo, &UFSType, &UFSSize, &UFSFree, &UFSDelete, &UFSRename, &UFSRun};
2020-12-31 13:19:50 +00:00
void UFSInfo(void) {
2021-01-15 15:17:25 +00:00
Response_P(PSTR("{\"Ufs\":{\"Type\":%d,\"Size\":%d,\"Free\":%d}"), ufs_type, UfsInfo(0, 0), UfsInfo(1, 0));
if (ffs_type && (ffs_type != ufs_type)) {
ResponseAppend_P(PSTR(",{\"Type\":%d,\"Size\":%d,\"Free\":%d}"), ffs_type, UfsInfo(0, 1), UfsInfo(1, 1));
}
ResponseJsonEnd();
2020-12-31 13:19:50 +00:00
}
2021-01-04 14:52:32 +00:00
void UFSType(void) {
2021-01-15 15:17:25 +00:00
if (ffs_type && (ffs_type != ufs_type)) {
Response_P(PSTR("{\"%s\":[%d,%d]}"), XdrvMailbox.command, ufs_type, ffs_type);
} else {
ResponseCmndNumber(ufs_type);
}
2020-12-31 13:19:50 +00:00
}
2021-01-08 15:22:06 +00:00
void UFSSize(void) {
2021-01-15 15:17:25 +00:00
if (ffs_type && (ffs_type != ufs_type)) {
Response_P(PSTR("{\"%s\":[%d,%d]}"), XdrvMailbox.command, UfsInfo(0, 0), UfsInfo(0, 1));
} else {
ResponseCmndNumber(UfsInfo(0, 0));
}
2020-12-31 13:19:50 +00:00
}
2021-01-08 15:22:06 +00:00
void UFSFree(void) {
2021-01-15 15:17:25 +00:00
if (ffs_type && (ffs_type != ufs_type)) {
Response_P(PSTR("{\"%s\":[%d,%d]}"), XdrvMailbox.command, UfsInfo(1, 0), UfsInfo(1, 1));
} else {
ResponseCmndNumber(UfsInfo(1, 0));
}
2020-12-31 13:19:50 +00:00
}
2021-01-08 15:22:06 +00:00
void UFSDelete(void) {
2021-01-15 15:17:25 +00:00
// UfsDelete sdcard or flashfs file if only one of them available
// UfsDelete2 flashfs file if available
2021-01-08 15:22:06 +00:00
if (XdrvMailbox.data_len > 0) {
char fname[UFS_FILENAME_SIZE];
UfsFilename(fname, XdrvMailbox.data);
2021-01-15 15:17:25 +00:00
bool result = false;
if (ffs_type && (ffs_type != ufs_type) && (2 == XdrvMailbox.index)) {
result = TfsDeleteFile(fname);
2021-01-15 15:17:25 +00:00
} else {
result = (ufs_type && ufsp->remove(fname));
2021-01-15 15:17:25 +00:00
}
if (!result) {
2021-02-16 15:21:46 +00:00
ResponseCmndFailed();
2021-01-08 15:22:06 +00:00
} else {
ResponseCmndDone();
}
}
}
void UFSRename(void) {
// UfsRename sdcard or flashfs file if only one of them available
// UfsRename2 flashfs file if available
if (XdrvMailbox.data_len > 0) {
bool result = false;
char *fname1 = strtok(XdrvMailbox.data, ",");
char *fname2 = strtok(nullptr, ",");
if (fname1 && fname2) {
char fname_old[UFS_FILENAME_SIZE];
UfsFilename(fname_old, fname1);
char fname_new[UFS_FILENAME_SIZE];
UfsFilename(fname_new, fname2);
if (ffs_type && (ffs_type != ufs_type) && (2 == XdrvMailbox.index)) {
result = TfsRenameFile(fname_old, fname_new);
} else {
result = (ufs_type && ufsp->rename(fname_old, fname_new));
}
}
if (!result) {
2021-02-16 15:21:46 +00:00
ResponseCmndFailed();
} else {
ResponseCmndDone();
2021-01-08 15:22:06 +00:00
}
}
}
2021-02-16 11:19:40 +00:00
void UFSRun(void) {
if (XdrvMailbox.data_len > 0) {
char fname[UFS_FILENAME_SIZE];
if (UfsExecuteCommandFile(UfsFilename(fname, XdrvMailbox.data))) {
2021-02-16 11:19:40 +00:00
ResponseClear();
} else {
2021-02-16 15:21:46 +00:00
ResponseCmndFailed();
2021-02-16 11:19:40 +00:00
}
2021-02-17 10:06:48 +00:00
} else {
bool not_active = UfsExecuteCommandFileReady();
UfsData.run_file_pos = -1;
ResponseCmndChar(not_active ? PSTR(D_JSON_DONE) : PSTR(D_JSON_ABORTED));
2021-02-16 11:19:40 +00:00
}
}
/*********************************************************************************************\
* Web support
\*********************************************************************************************/
2021-01-08 16:31:16 +00:00
#ifdef USE_WEBSERVER
2020-12-31 13:19:50 +00:00
const char UFS_WEB_DIR[] PROGMEM =
"<p><form action='" "ufsd" "' method='get'><button>" "%s" "</button></form></p>";
2021-01-06 16:54:03 +00:00
2020-12-31 13:19:50 +00:00
const char UFS_FORM_FILE_UPLOAD[] PROGMEM =
2021-01-04 14:52:32 +00:00
"<div id='f1' name='f1' style='display:block;'>"
2021-01-06 16:54:03 +00:00
"<fieldset><legend><b>&nbsp;" D_MANAGE_FILE_SYSTEM "&nbsp;</b></legend>";
const char UFS_FORM_FILE_UPGc[] PROGMEM =
"<div style='text-align:left;color:#%06x;'>" D_FS_SIZE " %s MB - " D_FS_FREE " %s MB";
2021-01-07 09:57:24 +00:00
const char UFS_FORM_FILE_UPGc1[] PROGMEM =
2021-01-27 11:14:33 +00:00
" &nbsp;&nbsp;<a href='http://%_I/ufsd?dir=%d'>%s</a>";
2021-01-07 09:57:24 +00:00
const char UFS_FORM_FILE_UPGc2[] PROGMEM =
"</div>";
2021-01-07 09:57:24 +00:00
2020-12-31 13:19:50 +00:00
const char UFS_FORM_FILE_UPG[] PROGMEM =
2021-01-04 14:52:32 +00:00
"<form method='post' action='ufsu' enctype='multipart/form-data'>"
2021-01-06 16:54:03 +00:00
"<br><input type='file' name='ufsu'><br>"
"<br><button type='submit' onclick='eb(\"f1\").style.display=\"none\";eb(\"f2\").style.display=\"block\";this.form.submit();'>" D_START " %s</button></form>"
"<br>";
2020-12-31 13:19:50 +00:00
const char UFS_FORM_SDC_DIRa[] PROGMEM =
2021-01-06 16:54:03 +00:00
"<div style='text-align:left;overflow:auto;height:250px;'>";
2020-12-31 13:19:50 +00:00
const char UFS_FORM_SDC_DIRc[] PROGMEM =
2021-01-04 14:52:32 +00:00
"</div>";
2020-12-31 13:19:50 +00:00
const char UFS_FORM_FILE_UPGb[] PROGMEM =
2021-04-11 14:22:57 +01:00
"<form method='get' action='ufse'><input type='hidden' file='" D_NEW_FILE "'>"
2021-10-21 17:35:25 +01:00
"<button type='submit'>" D_CREATE_NEW_FILE "</button></form>";
2021-10-19 07:08:55 +01:00
const char UFS_FORM_FILE_UPGb1[] PROGMEM =
"<input type='checkbox' id='shf' onclick='sf(eb(\"shf\").checked);' name='shf'>" D_SHOW_HIDDEN_FILES "</input>";
const char UFS_FORM_FILE_UPGb2[] PROGMEM =
2021-01-04 14:52:32 +00:00
"</fieldset>"
"</div>"
"<div id='f2' name='f2' style='display:none;text-align:center;'><b>" D_UPLOAD_STARTED " ...</b></div>";
const char UFS_FORM_SDC_DIR_NORMAL[] PROGMEM =
"";
const char UFS_FORM_SDC_DIR_HIDDABLE[] PROGMEM =
" class='hf'";
2020-12-31 13:19:50 +00:00
const char UFS_FORM_SDC_DIRd[] PROGMEM =
2021-01-04 14:52:32 +00:00
"<pre><a href='%s' file='%s'>%s</a></pre>";
2020-12-31 13:19:50 +00:00
const char UFS_FORM_SDC_DIRb[] PROGMEM =
"<pre%s><a href='%s' file='%s'>%s</a> %s %8d %s %s</pre>";
2020-12-31 13:19:50 +00:00
const char UFS_FORM_SDC_HREF[] PROGMEM =
2021-04-11 14:22:57 +01:00
"ufsd?download=%s/%s";
2021-01-09 15:33:23 +00:00
#ifdef GUI_TRASH_FILE
const char UFS_FORM_SDC_HREFdel[] PROGMEM =
2021-04-11 14:22:57 +01:00
//"<a href=ufsd?delete=%s/%s>&#128465;</a>"; // 🗑️
"<a href='ufsd?delete=%s/%s' onclick=\"return confirm('" D_CONFIRM_FILE_DEL "')\">&#128293;</a>"; // 🔥
2021-01-09 15:33:23 +00:00
#endif // GUI_TRASH_FILE
2021-04-11 14:22:57 +01:00
#ifdef GUI_EDIT_FILE
#define FILE_BUFFER_SIZE 1024
const char UFS_FORM_SDC_HREFedit[] PROGMEM =
"<a href='ufse?file=%s/%s'>&#x1F4DD;</a>"; // 📝
2021-04-11 14:22:57 +01:00
const char HTTP_EDITOR_FORM_START[] PROGMEM =
2021-04-16 16:24:49 +01:00
"<fieldset><legend><b>&nbsp;" D_EDIT_FILE "&nbsp;</b></legend>"
"<form>"
"<label for='name'>" D_FILE ":</label><input type='text' id='name' name='name' value='%s'><br><hr width='98%%'>"
2021-10-20 21:49:50 +01:00
"<textarea id='content' name='content' wrap='off' rows='8' cols='80' style='font-size: 12pt'>";
2021-04-11 14:22:57 +01:00
const char HTTP_EDITOR_FORM_END[] PROGMEM =
2021-04-16 16:24:49 +01:00
"</textarea>"
"<button name='save' type='submit' formmethod='post' formenctype='multipart/form-data' formaction='/ufse' class='button bgrn'>" D_SAVE "</button>"
"</form></fieldset>";
2021-04-11 14:22:57 +01:00
#endif // #ifdef GUI_EDIT_FILE
void UfsDirectory(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_MANAGE_FILE_SYSTEM));
2020-12-31 13:19:50 +00:00
uint8_t depth = 0;
strcpy(ufs_path, "/");
2021-01-18 20:48:04 +00:00
if (Webserver->hasArg(F("download"))) {
String stmp = Webserver->arg(F("download"));
2020-12-31 13:19:50 +00:00
char *cp = (char*)stmp.c_str();
if (UfsDownloadFile(cp)) {
2020-12-31 13:19:50 +00:00
// is directory
strcpy(ufs_path, cp);
2021-01-17 11:30:20 +00:00
} else {
return;
2020-12-31 13:19:50 +00:00
}
}
2021-01-18 20:48:04 +00:00
if (Webserver->hasArg(F("dir"))) {
String stmp = Webserver->arg(F("dir"));
2021-01-07 09:57:24 +00:00
ufs_dir = atoi(stmp.c_str());
if (ufs_dir == 1) {
dfsp = ufsp;
} else {
if (ffsp) {
dfsp = ffsp;
}
}
}
2021-01-18 20:48:04 +00:00
if (Webserver->hasArg(F("delete"))) {
String stmp = Webserver->arg(F("delete"));
2021-01-09 15:33:23 +00:00
char *cp = (char*)stmp.c_str();
dfsp->remove(cp);
}
2021-01-06 16:54:03 +00:00
WSContentStart_P(PSTR(D_MANAGE_FILE_SYSTEM));
2020-12-31 13:19:50 +00:00
WSContentSendStyle();
2021-01-06 16:54:03 +00:00
WSContentSend_P(UFS_FORM_FILE_UPLOAD);
char ts[FLOATSZ];
dtostrfd((float)UfsInfo(0, ufs_dir == 2 ? 1:0) / 1000, 3, ts);
char fs[FLOATSZ];
dtostrfd((float)UfsInfo(1, ufs_dir == 2 ? 1:0) / 1000, 3, fs);
WSContentSend_PD(UFS_FORM_FILE_UPGc, WebColor(COL_TEXT), ts, fs);
2021-01-06 16:54:03 +00:00
2021-01-07 09:57:24 +00:00
if (ufs_dir) {
2021-01-27 11:14:33 +00:00
WSContentSend_P(UFS_FORM_FILE_UPGc1, (uint32_t)WiFi.localIP(), (ufs_dir == 1)?2:1, (ufs_dir == 1)?PSTR("SDCard"):PSTR("FlashFS"));
2021-01-07 09:57:24 +00:00
}
WSContentSend_P(UFS_FORM_FILE_UPGc2);
2021-01-18 20:48:04 +00:00
WSContentSend_P(UFS_FORM_FILE_UPG, PSTR(D_SCRIPT_UPLOAD));
2021-01-06 16:54:03 +00:00
2020-12-31 13:19:50 +00:00
WSContentSend_P(UFS_FORM_SDC_DIRa);
if (ufs_type) {
UfsListDir(ufs_path, depth);
2020-12-31 13:19:50 +00:00
}
WSContentSend_P(UFS_FORM_SDC_DIRc);
2021-10-21 17:35:25 +01:00
#ifdef GUI_EDIT_FILE
2020-12-31 13:19:50 +00:00
WSContentSend_P(UFS_FORM_FILE_UPGb);
2021-10-21 17:35:25 +01:00
#endif
2021-10-19 07:08:55 +01:00
if (!isSDC()) {
WSContentSend_P(UFS_FORM_FILE_UPGb1);
}
WSContentSend_P(UFS_FORM_FILE_UPGb2);
WSContentSpaceButton(BUTTON_MANAGEMENT);
2020-12-31 13:19:50 +00:00
WSContentStop();
Web.upload_file_type = UPL_UFSFILE;
2020-12-31 13:19:50 +00:00
}
2021-10-19 07:08:55 +01:00
// return true if SDC
bool isSDC(void) {
#ifndef SDC_HIDE_INVISIBLES
return false;
#else
if (((uint32_t)ufsp != (uint32_t)ffsp) && ((uint32_t)ffsp == (uint32_t)dfsp)) return false;
if (((uint32_t)ufsp == (uint32_t)ffsp) && (ufs_type != UFS_TSDC)) return false;
return true;
#endif
}
void UfsListDir(char *path, uint8_t depth) {
2021-10-19 07:08:55 +01:00
char name[48];
2020-12-31 13:19:50 +00:00
char npath[128];
char format[12];
2021-01-18 20:48:04 +00:00
sprintf(format, PSTR("%%-%ds"), 24 - depth);
2020-12-31 13:19:50 +00:00
2021-01-07 09:57:24 +00:00
File dir = dfsp->open(path, UFS_FILE_READ);
2020-12-31 13:19:50 +00:00
if (dir) {
dir.rewindDirectory();
if (strlen(path)>1) {
2021-04-11 14:22:57 +01:00
ext_snprintf_P(npath, sizeof(npath), PSTR("ufsd?download=%s"), path);
2021-01-04 14:52:32 +00:00
for (uint32_t cnt = strlen(npath) - 1; cnt > 0; cnt--) {
if (npath[cnt] == '/') {
if (npath[cnt - 1] == '=') {
npath[cnt + 1] = 0;
} else {
npath[cnt] = 0;
}
2020-12-31 13:19:50 +00:00
break;
}
}
2021-01-18 20:48:04 +00:00
WSContentSend_P(UFS_FORM_SDC_DIRd, npath, path, PSTR(".."));
2020-12-31 13:19:50 +00:00
}
char *ep;
while (true) {
File entry = dir.openNextFile();
if (!entry) {
break;
}
// esp32 returns path here, shorten to filename
ep = (char*)entry.name();
2021-01-04 14:52:32 +00:00
if (*ep == '/') { ep++; }
2020-12-31 13:19:50 +00:00
char *lcp = strrchr(ep,'/');
if (lcp) {
ep = lcp + 1;
}
2021-01-06 16:54:03 +00:00
uint32_t tm = entry.getLastWrite();
String tstr = GetDT(tm);
2020-12-31 13:19:50 +00:00
char *pp = path;
2021-01-04 14:52:32 +00:00
if (!*(pp + 1)) { pp++; }
2020-12-31 13:19:50 +00:00
char *cp = name;
// osx formatted disks contain a lot of stuff we dont want
bool hiddable = UfsReject((char*)ep);
2020-12-31 13:19:50 +00:00
2021-10-19 07:08:55 +01:00
if (!hiddable || !isSDC() ) {
for (uint8_t cnt = 0; cnt<depth; cnt++) {
*cp++ = '-';
}
2021-01-01 07:41:36 +00:00
sprintf(cp, format, ep);
if (entry.isDirectory()) {
ext_snprintf_P(npath, sizeof(npath), UFS_FORM_SDC_HREF, pp, ep);
WSContentSend_P(UFS_FORM_SDC_DIRd, npath, ep, name);
uint8_t plen = strlen(path);
if (plen > 1) {
strcat(path, "/");
}
strcat(path, ep);
UfsListDir(path, depth + 4);
path[plen] = 0;
} else {
2021-01-09 15:33:23 +00:00
#ifdef GUI_TRASH_FILE
char delpath[128];
ext_snprintf_P(delpath, sizeof(delpath), UFS_FORM_SDC_HREFdel, pp, ep);
2021-01-09 15:33:23 +00:00
#else
char delpath[2];
delpath[0]=0;
2021-01-09 15:33:23 +00:00
#endif // GUI_TRASH_FILE
2021-04-11 14:22:57 +01:00
#ifdef GUI_EDIT_FILE
char editpath[128];
ext_snprintf_P(editpath, sizeof(editpath), UFS_FORM_SDC_HREFedit, pp, ep);
2021-04-11 14:22:57 +01:00
#else
char editpath[2];
editpath[0]=0;
2021-04-11 14:22:57 +01:00
#endif // GUI_TRASH_FILE
ext_snprintf_P(npath, sizeof(npath), UFS_FORM_SDC_HREF, pp, ep);
WSContentSend_P(UFS_FORM_SDC_DIRb, hiddable ? UFS_FORM_SDC_DIR_HIDDABLE : UFS_FORM_SDC_DIR_NORMAL, npath, ep, name, tstr.c_str(), entry.size(), delpath, editpath);
2020-12-31 13:19:50 +00:00
}
entry.close();
}
2021-10-19 07:08:55 +01:00
}
2020-12-31 13:19:50 +00:00
dir.close();
}
}
2021-01-17 11:30:20 +00:00
#ifdef ESP32
#define ESP32_DOWNLOAD_TASK
#endif // ESP32
2021-01-11 16:44:54 +00:00
uint8_t UfsDownloadFile(char *file) {
2020-12-31 13:19:50 +00:00
File download_file;
2021-01-07 09:57:24 +00:00
if (!dfsp->exists(file)) {
2021-04-11 14:22:57 +01:00
AddLog(LOG_LEVEL_INFO, PSTR("UFS: File '%s' not found"), file);
2021-01-04 14:52:32 +00:00
return 0;
}
2020-12-31 13:19:50 +00:00
2021-01-07 09:57:24 +00:00
download_file = dfsp->open(file, UFS_FILE_READ);
2021-01-04 14:52:32 +00:00
if (!download_file) {
2021-04-11 14:22:57 +01:00
AddLog(LOG_LEVEL_INFO, PSTR("UFS: Could not open file '%s'"), file);
2021-01-04 14:52:32 +00:00
return 0;
}
2020-12-31 13:19:50 +00:00
2021-01-04 14:52:32 +00:00
if (download_file.isDirectory()) {
download_file.close();
return 1;
}
2020-12-31 13:19:50 +00:00
2021-01-17 11:30:20 +00:00
#ifndef ESP32_DOWNLOAD_TASK
2021-01-11 16:44:54 +00:00
WiFiClient download_Client;
2021-01-04 14:52:32 +00:00
uint32_t flen = download_file.size();
2020-12-31 13:19:50 +00:00
2021-01-04 14:52:32 +00:00
download_Client = Webserver->client();
Webserver->setContentLength(flen);
2020-12-31 13:19:50 +00:00
2021-01-04 14:52:32 +00:00
char attachment[100];
char *cp;
for (uint32_t cnt = strlen(file); cnt >= 0; cnt--) {
if (file[cnt] == '/') {
cp = &file[cnt + 1];
break;
2020-12-31 13:19:50 +00:00
}
2021-01-04 14:52:32 +00:00
}
snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=%s"), cp);
Webserver->sendHeader(F("Content-Disposition"), attachment);
2021-02-02 13:57:53 +00:00
WSSend(200, CT_APP_STREAM, "");
2021-01-04 14:52:32 +00:00
uint8_t buff[512];
uint32_t bread;
// transfer is about 150kb/s
uint32_t cnt = 0;
while (download_file.available()) {
bread = download_file.read(buff, sizeof(buff));
uint32_t bw = download_Client.write((const char*)buff, bread);
if (!bw) { break; }
cnt++;
if (cnt > 7) {
cnt = 0;
//if (glob_script_mem.script_loglevel & 0x80) {
// this indeed multitasks, but is slower 50 kB/s
// loop();
//}
2020-12-31 13:19:50 +00:00
}
2021-01-04 14:52:32 +00:00
delay(0);
2021-01-11 16:44:54 +00:00
OsWatchLoop();
2021-01-04 14:52:32 +00:00
}
download_file.close();
download_Client.stop();
2021-01-17 11:30:20 +00:00
#endif // ESP32_DOWNLOAD_TASK
2021-01-11 16:44:54 +00:00
2021-01-17 11:30:20 +00:00
#ifdef ESP32_DOWNLOAD_TASK
2021-01-11 16:44:54 +00:00
download_file.close();
if (UfsData.download_busy == true) {
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("UFS: Download is busy"));
2021-01-11 16:44:54 +00:00
return 0;
}
UfsData.download_busy = true;
2021-01-11 16:44:54 +00:00
char *path = (char*)malloc(128);
strcpy(path,file);
BaseType_t ret = xTaskCreatePinnedToCore(donload_task, "DT", 6000, (void*)path, 3, nullptr, 1);
if (ret != pdPASS)
AddLog(LOG_LEVEL_INFO, PSTR("UFS: Download task failed with %d"), ret);
yield();
2021-01-17 11:30:20 +00:00
#endif // ESP32_DOWNLOAD_TASK
2021-01-11 16:44:54 +00:00
2021-01-04 14:52:32 +00:00
return 0;
2020-12-31 13:19:50 +00:00
}
2021-01-11 16:44:54 +00:00
2021-01-17 11:30:20 +00:00
#ifdef ESP32_DOWNLOAD_TASK
2021-01-11 16:44:54 +00:00
#ifndef DOWNLOAD_SIZE
#define DOWNLOAD_SIZE 4096
2021-01-17 11:30:20 +00:00
#endif // DOWNLOAD_SIZE
2021-01-11 16:44:54 +00:00
void donload_task(void *path) {
File download_file;
WiFiClient download_Client;
char *file = (char*) path;
download_file = dfsp->open(file, UFS_FILE_READ);
free(file);
uint32_t flen = download_file.size();
download_Client = Webserver->client();
Webserver->setContentLength(flen);
char attachment[100];
char *cp;
for (uint32_t cnt = strlen(file); cnt >= 0; cnt--) {
if (file[cnt] == '/') {
cp = &file[cnt + 1];
break;
}
}
snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=%s"), cp);
Webserver->sendHeader(F("Content-Disposition"), attachment);
2021-02-02 13:57:53 +00:00
WSSend(200, CT_APP_STREAM, "");
2021-01-11 16:44:54 +00:00
uint8_t *buff = (uint8_t*)malloc(DOWNLOAD_SIZE);
if (buff) {
uint32_t bread;
while (download_file.available()) {
bread = download_file.read(buff, DOWNLOAD_SIZE);
uint32_t bw = download_Client.write((const char*)buff, bread);
if (!bw) { break; }
}
free(buff);
}
download_file.close();
download_Client.stop();
UfsData.download_busy = false;
2021-01-11 16:44:54 +00:00
vTaskDelete( NULL );
}
2021-01-17 11:30:20 +00:00
#endif // ESP32_DOWNLOAD_TASK
2021-01-11 16:44:54 +00:00
bool UfsUploadFileOpen(const char* upload_filename) {
char npath[48];
snprintf_P(npath, sizeof(npath), PSTR("%s/%s"), ufs_path, upload_filename);
dfsp->remove(npath);
ufs_upload_file = dfsp->open(npath, UFS_FILE_WRITE);
return (ufs_upload_file);
}
bool UfsUploadFileWrite(uint8_t *upload_buf, size_t current_size) {
if (ufs_upload_file) {
ufs_upload_file.write(upload_buf, current_size);
2020-12-31 13:19:50 +00:00
} else {
return false;
2020-12-31 13:19:50 +00:00
}
return true;
}
void UfsUploadFileClose(void) {
ufs_upload_file.close();
2020-12-31 13:19:50 +00:00
}
2021-04-11 14:22:57 +01:00
//******************************************************************************************
// File Editor
//******************************************************************************************
#ifdef GUI_EDIT_FILE
void UfsEditor(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
AddLog(LOG_LEVEL_DEBUG, PSTR("UFS: UfsEditor GET"));
2021-04-16 16:24:49 +01:00
char fname_input[UFS_FILENAME_SIZE];
2021-04-11 14:22:57 +01:00
if (Webserver->hasArg(F("file"))) {
2021-04-16 16:24:49 +01:00
WebGetArg(PSTR("file"), fname_input, sizeof(fname_input));
} else {
snprintf_P(fname_input, sizeof(fname_input), PSTR(D_NEW_FILE));
2021-04-11 14:22:57 +01:00
}
2021-04-16 16:24:49 +01:00
char fname[UFS_FILENAME_SIZE];
UfsFilename(fname, fname_input); // Trim spaces and add slash
2021-04-11 14:22:57 +01:00
2022-01-01 14:06:33 +00:00
AddLog(LOG_LEVEL_DEBUG, PSTR("UFS: UfsEditor: file=%s, ffs_type=%d, TfsFileExist=%d"), fname, ffs_type, dfsp->exists(fname));
2021-04-11 14:22:57 +01:00
WSContentStart_P(PSTR(D_EDIT_FILE));
WSContentSendStyle();
2021-04-16 16:24:49 +01:00
char *bfname = fname +1;
WSContentSend_P(HTTP_EDITOR_FORM_START, bfname); // Skip leading slash
2021-04-11 14:22:57 +01:00
2022-01-01 14:06:33 +00:00
if (ffs_type && dfsp->exists(fname)) {
File fp = dfsp->open(fname, "r");
2021-04-11 14:22:57 +01:00
if (!fp) {
AddLog(LOG_LEVEL_DEBUG, PSTR("UFS: UfsEditor: file open failed"));
WSContentSend_P(D_NEW_FILE);
2021-04-16 16:24:49 +01:00
} else {
uint8_t *buf = (uint8_t*)malloc(FILE_BUFFER_SIZE+1);
size_t filelen = fp.size();
AddLog(LOG_LEVEL_DEBUG, PSTR("UFS: UfsEditor: file len=%d"), filelen);
while (filelen > 0) {
size_t l = fp.read(buf, FILE_BUFFER_SIZE);
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("UFS: UfsEditor: read=%d"), l);
if (l < 0) { break; }
buf[l] = '\0';
2021-05-25 17:53:10 +01:00
WSContentSend_P(PSTR("%s"), buf);
2021-04-16 16:24:49 +01:00
filelen -= l;
}
fp.close();
free(buf);
AddLog(LOG_LEVEL_DEBUG, PSTR("UFS: UfsEditor: read done"));
2021-04-11 14:22:57 +01:00
}
2021-04-16 16:24:49 +01:00
} else {
2021-04-11 14:22:57 +01:00
WSContentSend_P(D_NEW_FILE);
}
WSContentSend_P(HTTP_EDITOR_FORM_END);
WSContentSend_P(UFS_WEB_DIR, PSTR(D_MANAGE_FILE_SYSTEM));
WSContentStop();
}
void UfsEditorUpload(void) {
AddLog(LOG_LEVEL_DEBUG, PSTR("UFS: UfsEditor: file upload"));
if (!HttpCheckPriviledgedAccess()) { return; }
if (!Webserver->hasArg("name")) {
AddLog(LOG_LEVEL_ERROR, PSTR("UFS: UfsEditor: file upload - no filename"));
WSSend(400, CT_PLAIN, F("400: Bad request - no filename"));
return;
}
2021-04-16 16:24:49 +01:00
char fname_input[UFS_FILENAME_SIZE];
WebGetArg(PSTR("name"), fname_input, sizeof(fname_input));
char fname[UFS_FILENAME_SIZE];
UfsFilename(fname, fname_input); // Trim spaces and add slash
AddLog(LOG_LEVEL_DEBUG, PSTR("UFS: UfsEditor: file '%s'"), fname);
2021-04-11 14:22:57 +01:00
if (!Webserver->hasArg("content")) {
AddLog(LOG_LEVEL_ERROR, PSTR("UFS: UfsEditor: file upload - no content"));
WSSend(400, CT_PLAIN, F("400: Bad request - no content"));
return;
}
String content = Webserver->arg("content");
2022-01-01 14:06:33 +00:00
if (!dfsp) {
2021-04-11 14:22:57 +01:00
Web.upload_error = 1;
AddLog(LOG_LEVEL_ERROR, PSTR("UFS: UfsEditor: 507: no storage available"));
WSSend(507, CT_PLAIN, F("507: no storage available"));
return;
}
2022-01-01 14:06:33 +00:00
File fp = dfsp->open(fname, "w");
2021-04-16 16:24:49 +01:00
if (!fp) {
2021-04-11 14:22:57 +01:00
Web.upload_error = 1;
2021-04-16 16:24:49 +01:00
AddLog(LOG_LEVEL_ERROR, PSTR("UFS: UfsEditor: 400: invalid file name '%s'"), fname);
2021-04-11 14:22:57 +01:00
WSSend(400, CT_PLAIN, F("400: bad request - invalid filename"));
return;
}
if (*content.c_str()) {
content.replace("\r\n", "\n");
content.replace("\r", "\n");
}
if (!fp.print(content)) {
2021-04-16 16:24:49 +01:00
AddLog(LOG_LEVEL_ERROR, PSTR("UFS: UfsEditor: write error on '%s'"), fname);
2021-04-11 14:22:57 +01:00
}
fp.close();
Webserver->sendHeader(F("Location"),F("/ufsu"));
Webserver->send(303);
}
2021-04-16 16:24:49 +01:00
#endif // GUI_EDIT_FILE
2021-04-11 14:22:57 +01:00
2021-01-08 16:31:16 +00:00
#endif // USE_WEBSERVER
2020-12-31 13:19:50 +00:00
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
2021-01-06 13:41:23 +00:00
bool Xdrv50(uint8_t function) {
2020-12-31 13:19:50 +00:00
bool result = false;
switch (function) {
2021-02-16 15:21:46 +00:00
case FUNC_LOOP:
UfsExecuteCommandFileLoop();
break;
2021-01-08 18:28:05 +00:00
#ifdef USE_SDCARD
case FUNC_PRE_INIT:
UfsCheckSDCardInit();
break;
#endif // USE_SDCARD
case FUNC_MQTT_INIT:
2021-02-16 14:54:53 +00:00
if (!TasmotaGlobal.no_autoexec) {
UfsExecuteCommandFile(TASM_FILE_AUTOEXEC);
}
break;
2020-12-31 13:19:50 +00:00
case FUNC_COMMAND:
result = DecodeCommand(kUFSCommands, kUFSCommand);
break;
#ifdef USE_WEBSERVER
2021-01-06 16:54:03 +00:00
case FUNC_WEB_ADD_MANAGEMENT_BUTTON:
2020-12-31 13:19:50 +00:00
if (ufs_type) {
if (XdrvMailbox.index) {
XdrvMailbox.index++;
} else {
WSContentSend_PD(UFS_WEB_DIR, PSTR(D_MANAGE_FILE_SYSTEM));
}
2020-12-31 13:19:50 +00:00
}
break;
case FUNC_WEB_ADD_HANDLER:
2021-02-03 11:22:17 +00:00
// Webserver->on(F("/ufsd"), UfsDirectory);
// Webserver->on(F("/ufsu"), HTTP_GET, UfsDirectory);
// Webserver->on(F("/ufsu"), HTTP_POST,[](){Webserver->sendHeader(F("Location"),F("/ufsu"));Webserver->send(303);}, HandleUploadLoop);
Webserver->on("/ufsd", UfsDirectory);
Webserver->on("/ufsu", HTTP_GET, UfsDirectory);
Webserver->on("/ufsu", HTTP_POST,[](){Webserver->sendHeader(F("Location"),F("/ufsu"));Webserver->send(303);}, HandleUploadLoop);
2021-04-11 14:22:57 +01:00
#ifdef GUI_EDIT_FILE
Webserver->on("/ufse", HTTP_GET, UfsEditor);
Webserver->on("/ufse", HTTP_POST, UfsEditorUpload);
#endif
2020-12-31 13:19:50 +00:00
break;
#endif // USE_WEBSERVER
}
return result;
}
#endif // USE_UFILESYS