/*
  xdrv_50_filesystem.ino - unified file system for Tasmota

  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/>.
*/

#ifdef USE_UFILESYS

// saves 80 bytes of flash, makes the UI cleaner for folders containing lots of files.
// disables recursive folder listing in file UI
//#define UFILESYS_NO_RECURSE_GUI

// Enables serving of static files on /fs/
// costs 1844 bytes of flash and 40 bytes of RAM
// ESP32 only (uses the ESP32 WebServer RequestHandler API)
// You could serve a whole webapp from Tas itself.
//#define UFILESYS_STATIC_SERVING
#if defined(ESP8266) && defined(UFILESYS_STATIC_SERVING)
  #undef UFILESYS_STATIC_SERVING
#endif

/*********************************************************************************************\
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).

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).

A button is created in the setup section to show up the file directory to download and upload files
subdirectories are supported.

Supported commands:
ufs       fs info
ufstype   get filesytem type 0=none 1=SD  2=Flashfile
ufssize   total size in kB
ufsfree   free size in kB
ufsdelete
ufsrename
ufsrun
ufsServe
ftp       start stop ftp server: 0 = OFF, 1 = SDC, 2 = FlashFile


\*********************************************************************************************/

#define XDRV_50           50

#define UFS_TNONE         0
#define UFS_TSDC          1
#define UFS_TFAT          2
#define UFS_TLFS          3

#define UFS_SDC           0
#define UFS_SDMMC         1

/*
// In tasmota.ino
#ifdef ESP8266
#include <LittleFS.h>
#include <SPI.h>
#ifdef USE_SDCARD
#include <SD.h>
#include <SDFAT.h>
#endif  // USE_SDCARD
#endif  // ESP8266
#ifdef ESP32
#include <LITTLEFS.h>
#ifdef USE_SDCARD
#include <SD.h>
#endif  // USE_SDCARD
#include "FFat.h"
#include "FS.h"
#endif  // ESP32
*/

const int UFS_FILENAME_SIZE = 50;

// Global file system pointer
FS *ufsp;
// Flash file system pointer
FS *ffsp;
// Local pointer for file managment
FS *dfsp;

char ufs_path[UFS_FILENAME_SIZE];
File ufs_upload_file;
uint8_t ufs_dir;
// 0 = None, 1 = SD, 2 = ffat, 3 = littlefs
uint8_t ufs_type;
uint8_t ffs_type;
// sd type 0 = SD spi interface, 1 = MMC interface
uint8_t sd_type;

struct {
  char run_file[UFS_FILENAME_SIZE];
  int run_file_pos = -1;
  bool run_file_mutex = 0;
  bool download_busy;
} UfsData;

/*********************************************************************************************/

// Init flash file system
void UfsInitOnce(void) {
  ufs_type = 0;
  ffsp = 0;
  ufs_dir = 0;

#ifdef ESP8266
  ffsp = &LittleFS;
  if (!LittleFS.begin()) {
    ffsp = nullptr;
    return;
  }
#endif  // ESP8266

#ifdef ESP32
  // try lfs first
  ffsp = &LittleFS;
 if (!LittleFS.begin(true, "") && !LittleFS.begin(true, "", 5, "fs_1")) {         // force empty mount point to make it the fallback FS
    // ffat is second
    ffsp = &FFat;
   if (!FFat.begin(true, "")) {
      ffsp = nullptr;
      return;
    }
    ffs_type = UFS_TFAT;
    ufs_type = ffs_type;
    ufsp = ffsp;
    dfsp = ffsp;
    return;
  }
#endif // ESP32
  ffs_type = UFS_TLFS;
  ufs_type = ffs_type;
  ufsp = ffsp;
  dfsp = ffsp;
}

// Called from tasmota.ino at restart. This inits flash file only
void UfsInit(void) {
  UfsData.run_file_pos = -1;
  UfsInitOnce();
  if (ufs_type) {
    AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "FlashFS mounted with %d kB free"), UfsInfo(1, 0));
  }
}

// simple put a zero at last '/'
// modifies input string
char *folderOnly(char *fname){
  for (int i = strlen(fname)-1; i >= 0; i--){
    if (fname[i] == '/'){
      fname[i] = 0;
      break;
    }
    fname[i] = 0;
  }
  if (!fname[0]){
    fname[0] = '/';
    fname[1] = 0;
  }
  return fname;
}

// returns everything after last '/' of whiole input if no '/'
char *fileOnly(char *fname){
  char *cp = fname;
  for (uint32_t cnt = strlen(fname); cnt >= 0; cnt--) {
    if (fname[cnt] == '/') {
      cp = &fname[cnt + 1];
      break;
    }
  }
  return cp;
}


#ifdef USE_SDCARD
bool sdcard_init = false;

void UfsCheckSDCardInit(void) {
  if (sdcard_init) { return; }
  // Try SPI mode first
  // SPI mode requires SDCARD_CS to be configured
/*
  if (TasmotaGlobal.spi_enabled && PinUsed(GPIO_SDCARD_CS)) {
    int8_t cs = Pin(GPIO_SDCARD_CS);
*/
  uint32_t spi_bus = 0;
  int8_t cs = -1;
  if (TasmotaGlobal.spi_enabled && PinUsed(GPIO_SDCARD_CS)) {
    cs = Pin(GPIO_SDCARD_CS);
  }
  if (TasmotaGlobal.spi_enabled2 && PinUsed(GPIO_SDCARD_CS, 1)) {
    spi_bus = 1;
    cs = Pin(GPIO_SDCARD_CS, 1);
  }
  if (cs > -1) {

#ifdef ESP8266
    SPI.begin();
    if (SD.begin(cs)) {
#endif // ESP8266
#ifdef ESP32
    SPIClass &bus = (1 == spi_bus) ? SPI_HSPI : SPI;
    bus.begin(Pin(GPIO_SPI_CLK, spi_bus), Pin(GPIO_SPI_MISO, spi_bus), Pin(GPIO_SPI_MOSI, spi_bus), -1);
    if (SD.begin(cs, bus)) {
#endif // ESP32
#ifdef ESP8266
      ufsp = &SDFS;
#endif  // ESP8266
#ifdef ESP32
      ufsp = &SD;
#endif  // ESP32

      ufs_type = UFS_TSDC;
      sd_type = UFS_SDC;
      dfsp = ufsp;
      if (ffsp) {ufs_dir = 1;}
      // make sd card the global filesystem
#ifdef ESP8266
      // on esp8266 sdcard info takes several seconds !!!, so we ommit it here
      AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "SDCard mounted"));
#endif // ESP8266
#ifdef ESP32
      AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "SDCard mounted (SPI bus%d) with %d kB free"), spi_bus +1, UfsInfo(1, 0));
#endif // ESP32
      sdcard_init = true;
    } else {
      AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "SDCard mount failed (SPI bus%d)"), spi_bus +1);
    }
  }

#if defined(ESP32) && defined(SOC_SDMMC_HOST_SUPPORTED)     // ESP32 and SDMMC supported (not Esp32C3)
  // check if SDIO is configured
  else if (PinUsed(GPIO_SDIO_CLK) && PinUsed(GPIO_SDIO_CMD) && PinUsed(GPIO_SDIO_D0)) {
    int32_t sdio_cmd = Pin(GPIO_SDIO_CMD);
    int32_t sdio_clk = Pin(GPIO_SDIO_CLK);
    int32_t sdio_d0 = Pin(GPIO_SDIO_D0);
    int32_t sdio_d1 = Pin(GPIO_SDIO_D1);
    int32_t sdio_d2 = Pin(GPIO_SDIO_D2);
    int32_t sdio_d3 = Pin(GPIO_SDIO_D3);
    bool bit_4_mode = (sdio_d1 >= 0) && (sdio_d2 >= 0) && (sdio_d3 >= 0);   // enable 4-bit mode if possible
    if (bit_4_mode) {
      // AddLog(LOG_LEVEL_DEBUG, "UFS: trying SDIO 4-bit clk=%i cmd=%i d0=%i d1=%i d2=%i d3=%i", sdio_clk, sdio_cmd, sdio_d0, sdio_d1, sdio_d2, sdio_d3);
      SD_MMC.setPins(sdio_clk, sdio_cmd, sdio_d0, sdio_d1, sdio_d2, sdio_d3);
    } else {
      // AddLog(LOG_LEVEL_DEBUG, "UFS: trying SDIO 1-bit clk=%i cmd=%i d0=%i", sdio_clk, sdio_cmd, sdio_d0);
      SD_MMC.setPins(sdio_clk, sdio_cmd, sdio_d0);
    }
#if defined(SOC_GP_LDO_SUPPORTED) && defined(SOC_SDMMC_IO_POWER_EXTERNAL)
    if (!LdoEnabled(4)) {
      SD_MMC.setPowerChannel(4);  // SD card IO power if not set by command Ldo4 (deprecated)
    }
#endif
    if (SD_MMC.begin("/sd", !bit_4_mode /*mode 1 bit*/, false /*format_if_failed*/)) {    // mount under "/sd" to be consistent with SD SPI
      ufsp = &SD_MMC;

      ufs_type = UFS_TSDC;
      sd_type = UFS_SDMMC;
      dfsp = ufsp;
      if (ffsp) {ufs_dir = 1;}
      // make sd card the global filesystem
      AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "SDCard mounted (SDIO %i-bit) with %d kB free"), bit_4_mode ? 4 : 1, UfsInfo(1, 0));
      sdcard_init = true;
    }
  }
#endif
}
#endif // USE_SDCARD

uint32_t UfsInfo(uint32_t sel, uint32_t type) {
  uint64_t result = 0;
  FS *ifsp = ufsp;
  uint8_t itype = ufs_type;
  if (type) {
    ifsp = ffsp;
    itype = ffs_type;
  }

#ifdef ESP8266
  FSInfo64 fsinfo;
#endif  // ESP8266

  switch (itype) {
    case UFS_TSDC:
#ifdef USE_SDCARD
#ifdef ESP8266
      ifsp->info64(fsinfo);
      if (sel == 0) {
        result = fsinfo.totalBytes;
      } else {
        result = (fsinfo.totalBytes - fsinfo.usedBytes);
      }
#endif  // ESP8266
#ifdef ESP32
#ifdef SOC_SDMMC_HOST_SUPPORTED
      if (sd_type == UFS_SDC) {
        if (sel == 0) {
          result = SD.totalBytes();
        } else {
          result = (SD.totalBytes() - SD.usedBytes());
        }
      } else if (sd_type == UFS_SDMMC) {
        if (sel == 0) {
          result = SD_MMC.totalBytes();
        } else {
          result = (SD_MMC.totalBytes() - SD_MMC.usedBytes());
        }
      } else {
        result = 0;
      }
#else
      if (sel == 0) {
        result = SD.totalBytes();
      } else {
        result = (SD.totalBytes() - SD.usedBytes());
      }
#endif // SOC_SDMMC_HOST_SUPPORTED
#endif  // ESP32
#endif  // USE_SDCARD
      break;

    case UFS_TLFS:
#ifdef ESP8266
      ifsp->info64(fsinfo);
      if (sel == 0) {
        result = fsinfo.totalBytes;
      } else {
        result = (fsinfo.totalBytes - fsinfo.usedBytes);
      }
#endif  // ESP8266
#ifdef ESP32
      if (sel == 0) {
        result = LittleFS.totalBytes();
      } else {
        result = LittleFS.totalBytes() - LittleFS.usedBytes();
      }
#endif  // ESP32
      break;

    case UFS_TFAT:
#ifdef ESP32
      if (sel == 0) {
        result = FFat.totalBytes();
      } else {
        result = FFat.freeBytes();
      }
#endif  // ESP32
      break;

  }
  return result / 1024;
}

uint32_t UfsSize(void) {
  return UfsInfo(0, ufs_dir == 2 ? 1:0);
}

uint32_t UfsFree(void) {
  return UfsInfo(1, ufs_dir == 2 ? 1:0);
}

#if USE_LONG_FILE_NAMES>0
#undef REJCMPL
#define REJCMPL 6
#else
#undef REJCMPL
#define REJCMPL 8
#endif

uint8_t UfsReject(char *name) {
  char *lcp = strrchr(name,'/');
  if (lcp) {
    name = lcp + 1;
  }

  while (*name=='/') { name++; }
  if (*name=='.') { return 1; }

  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; }
  return 0;
}

// return true if SDC
bool UfsIsSDC(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
}

/*********************************************************************************************\
 * Tfs low level functions
\*********************************************************************************************/

bool TfsFileExists(const char *fname){
  if (!ffs_type) { return false; }

  bool yes = ffsp->exists(fname);
  if (!yes) {
    AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TFS: File '%s' not found"), fname +1);  // Skip leading slash
  }
  return yes;
}

size_t TfsFileSize(const char *fname){
  if (!ffs_type) { return 0; }

  File file = ffsp->open(fname, "r");
  if (!file) { return 0; }
  size_t flen = file.size();
  file.close();
  return flen;
}

bool TfsSaveFile(const char *fname, const uint8_t *buf, uint32_t len) {
  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) {
    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) {
  if (!ffs_type) { return false; }

  File file = ffsp->open(fname, "w");
  if (!file) {
    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) {
  if (!ffs_type) { return false; }

  File file = ffsp->open(fname, "r");
  if (!file) {
    AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TFS: File '%s' not found"), fname +1);  // Skip leading slash
    return false;
  }

  size_t flen = file.size();
  if (len > flen) { len = flen; }           // Adjust requested length to smaller file length
  file.read(buf, len);
  file.close();
  return true;
}

String TfsLoadString(const char *fname) {
  // Use a reasonable amount of stack space considering 4k/8k available on ESP8266/ESP32 and manageable string length
  char buf[2048] = { 0 };                   // Prepare empty string of max 2047 characters on stack
  TfsLoadFile(fname, (uint8_t*)buf, 2047);  // Leave last position as end of string ('\0')
  return String(buf);                       // Received string or empty on error
}

bool TfsDeleteFile(const char *fname) {
  if (!ffs_type) { return false; }

  if (!ffsp->remove(fname)) {
    if (!ffsp->rmdir(fname)) {
      AddLog(LOG_LEVEL_INFO, PSTR("TFS: Delete failed"));
      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;
}

/*********************************************************************************************\
 * Log file
 * 
 * Enable with command `FileLog 1..4` or `FileLog 11..14`
 * Rotate max 16 x FILE_LOG_SIZE kB log files /log01 -> /log02 ... /log16 -> /log01 ...
 * Filesystem needs to be larger than 10k (FILE_LOG_FREE)
\*********************************************************************************************/

#ifndef FILE_LOG_SIZE
#define FILE_LOG_SIZE  100               // Log file size in kBytes (100kB is based on minimal filesystem of 320kB)
#endif
#define FILE_LOG_FREE  10                // Minimum free filesystem space in kBytes

#define FILE_LOG_COUNT 16                // Number of log files (max 16 as four bits are reserved for index)
#define FILE_LOG_NAME  "/log%02d"        // Log file name

// Log goes to the SD card when one is mounted, otherwise to the flash (as now)
FS *FileLogFs(void) {
  return (UFS_TSDC == ufs_type) ? ufsp : ffsp;
}

// Free space of exactly that file system (UfsInfo type 0 = ufsp, 1 = ffsp)
uint32_t FileLogFree(void) { 
  return UfsInfo(1, (FileLogFs() == ufsp) ? 0 : 1);
}

void FileLoggingAsync(bool refresh) {
  static uint32_t index = 1;             // Rotating log buffer entry pointer

  uint32_t filelog_level = Settings->filelog_level % 10;
  if (!ffs_type || !filelog_level) { return; }  // No filesystem or [FileLog] disabled
  if (refresh && !NeedLogRefresh(filelog_level, index)) { return; }  // No log buffer changes
  uint32_t filelog_option = Settings->filelog_level / 10;

  char fname[14];
  File file;
  uint32_t log_file_idx = Settings->mbflag2.log_file_idx;       // 0..15
  for (uint32_t retry = 0; retry <= 1; retry++) {
    snprintf_P(fname, sizeof(fname), PSTR(FILE_LOG_NAME), log_file_idx +1);  // /log01
    file = FileLogFs()->open(fname, "a");  // Append to existing log file
    if (!file) { 
      file = FileLogFs()->open(fname, "w");  // Make new log file
      if (!file) { 
        Settings->filelog_level = 0;     // [FileLog] disable
        AddLog(LOG_LEVEL_INFO, PSTR("FLG: Logging disabled. Save failed"));
        return;                          // Failed to make file
      }
    }

    bool fs_full = (FileLogFree() < FILE_LOG_FREE);
    if (!fs_full && (file.size() < (FILE_LOG_SIZE * 1000))) { break; }

    file.close();

    if (1 == retry) {
      Settings->filelog_level = 0;       // [FileLog] disable
      AddLog(LOG_LEVEL_INFO, PSTR("FLG: Logging disabled. No free space"));
      return;
    }

    // Rotate log file(s) as size is over FILE_LOG_SIZE or free space is less than FILE_LOG_FREE
    uint32_t last_log_file_idx = log_file_idx;  // 0..15
    log_file_idx++;

    if ((1 == filelog_option) &&
        (fs_full || (log_file_idx == FILE_LOG_COUNT))) {  // Rotate until free space is less than FILE_LOG_FREE
      Settings->filelog_level = 0;       // [FileLog] disable
      AddLog(LOG_LEVEL_INFO, PSTR("FLG: Logging disabled. Max rotates"));
      return;
    }

    if (log_file_idx >= FILE_LOG_COUNT) { log_file_idx = 0; }  // Rotate max 16 log files
    snprintf_P(fname, sizeof(fname), PSTR(FILE_LOG_NAME), log_file_idx +1);
    AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("FLG: Rotate file %s"), fname +1);  // Skip leading slash
    Settings->mbflag2.log_file_idx = log_file_idx;  // Save for restart or power on

    if (0 == filelog_option) {           // Remove oldest log file(s)
      // Remove log file(s) taking into account non-sequential file names and different file sizes
      uint32_t idx = log_file_idx;       // Next log file index
      do {                               // Need free space around FILE_LOG_SIZE so find oldest log file(s) and remove it
        snprintf_P(fname, sizeof(fname), PSTR(FILE_LOG_NAME), idx +1);
        if (FileLogFs()->remove(fname)) {  // Remove oldest (non-)sequential log file(s)
          AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("FLG: Delete file %s"), fname +1);  // Skip leading slash
        }
        idx++;
        if (idx >= FILE_LOG_COUNT) { idx = 0; }
      } while ((FileLogFree() < FILE_LOG_FREE) && (idx != last_log_file_idx));
    }
  }

#ifdef USE_WEBCAM
  WcInterrupt(0);                        // Stop stream if active to fix TG1WDT_SYS_RESET
#endif
  char* line;
  size_t len;
  while (GetLog(filelog_level, &index, &line, &len)) {
    // This will timeout on ESP32-webcam
    // But now solved with WcInterrupt(0) in support_esp.ino
    file.write((uint8_t*)line, len -1);  // Write up to LOG_BUFFER_SIZE log data
    snprintf_P(fname, sizeof(fname), PSTR("\r\n"));
    file.write((uint8_t*)fname, 2);
  }
#ifdef USE_WEBCAM
  WcInterrupt(1);
#endif

  file.close();
}

void FileLoggingDelete(void) {
  if (!ffs_type) { return; }             // No filesystem

  char fname[14];
  for (uint32_t idx = 0; idx < FILE_LOG_COUNT; idx++) {
    snprintf_P(fname, sizeof(fname), PSTR(FILE_LOG_NAME), idx +1);
    FileLogFs()->remove(fname);          // Remove all log file(s)
  }
  Settings->mbflag2.log_file_idx = 0;
  AddLog(LOG_LEVEL_INFO, PSTR("FLG: Log files deleted"));
}

/*********************************************************************************************\
 * File command execute support
\*********************************************************************************************/

bool UfsExecuteCommandFileReady(void) {
  return (UfsData.run_file_pos < 0);   // Check file ready to disable concurrency
}

void UfsExecuteCommandFileLoop(void) {
  if (UfsExecuteCommandFileReady() || !ffs_type) { return; }

  if (BACKLOG_EMPTY && strlen(UfsData.run_file) && !UfsData.run_file_mutex) {
    File file = ffsp->open(UfsData.run_file, "r");
    if (!file || !file.seek(UfsData.run_file_pos)) {
      UfsData.run_file_pos = -1;       // Signal file ready
      return;
    }

    UfsData.run_file_mutex = true;

    char cmd_line[512];
    cmd_line[0] = '\0';                // Clear in case of re-entry
    while (file.available()) {
      uint16_t index = 0;
      bool comment = false;
      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
        }
//        else if (index && !comment && (buf[0] == ';')) {
//          break;                       // End of command on multi command line
//        }
        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
        }
        else if (!comment && (index < sizeof(cmd_line) - 2)) {
          cmd_line[index++] = buf[0];  // Build command
        }
      }
      if ((index > 0) && (index < sizeof(cmd_line) - 1)) {
        cmd_line[index] = '\0';        // Valid command received
        break;
      }
    }
    UfsData.run_file_pos = (file.available()) ? file.position() : -1;
    file.close();
    if (strlen(cmd_line)) {
      ExecuteCommand(cmd_line, SRC_FILE);
    }

    UfsData.run_file_mutex = false;
  }
}

bool UfsExecuteCommandFile(const char *fname) {
  // Check for non-concurrency and file existance
  if (UfsExecuteCommandFileReady() && TfsFileExists(fname)) {
    snprintf(UfsData.run_file, sizeof(UfsData.run_file), fname);
    UfsData.run_file_pos = 0;          // Signal start of file
    return true;
  }
  return false;
}

/*********************************************************************************************\
 * File JSON settings support using file /.drvset000
 * 
 * {"UserSet1":{"Param1":123,"Param2":"Text1"},"UserSet2":{"Param1":123,"Param2":"Text2"},"UserSet3":{"Param1":123,"Param2":"Text3"}}
\*********************************************************************************************/

bool _UfsJsonSettingsUpdate(const char* data) {
  // Delete: Input UserSet2
  // Append: Input {"UserSet2":{"Param1":123,"Param2":"Text2"}}

  char filename[14];
  snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), 0);  // /.drvset000
  if (!TfsFileExists(filename)) { return false; }  // Error - File not found

  char bfname[14];
  strcpy_P(bfname, PSTR("/settmp"));
  File ofile = ffsp->open(bfname, "w");
  if (!ofile) { return false; }        // Error - unable to open temporary file
  File ifile = ffsp->open(filename, "r");
  if (!ifile) { 
    ofile.close();
    ffsp->remove(bfname);
    return false;                      // Error - unable to open settings file
  }

  bool append = false;
  char* key = (char*)data;
  char key_pos[32];                    // Max key length
  char *p = strchr(data, '"');
  if (p) {
    append = true;
    char *q = strchr(++p, '"');
    if (!q) { return false; }          // Error - No valid key provided in data to append
    uint32_t len = (uint32_t)q - (uint32_t)p;
    memcpy(key_pos, p, len);
    key_pos[len] = '\0';               // key = UserSet2
    key = key_pos;
  }

  char buffer[32];                     // Max key length
  uint8_t buf[1];
  uint32_t index = 0;
  uint32_t bracket_count = 0;
  int entries = 0;
  bool quote = false;
  bool mine = false;
  bool deleted = false;
  while (ifile.available()) {          // Process file
    ifile.read(buf, 1);
    if (bracket_count > 1) {           // Copy or skip old data
      if (!mine) {
        ofile.write(buf, 1);           // Copy data
      }
      if (buf[0] == '}') {
        bracket_count--;
      }
      else if (buf[0] == '{') {        // Next bracket
        bracket_count++;
      }
    } else {
      if (buf[0] == '}') {             // Last bracket
        break;                         // End of file
      }
      else if (buf[0] == '{') {
        bracket_count++;
        if (bracket_count > 1) {       // Skip first bracket
          entries++;
        }
      }
      else if (buf[0] == '"') {
        quote ^= 1;
        if (quote) {
          index = 0;
        } else {
          buffer[index] = '\0';        // End of key name
          mine = (!strcasecmp(buffer, key));
          if (mine) {
            entries--;                 // Skip old data
            deleted = true;
          } else {
            ofile.write((entries) ? (uint8_t*)",\"" : (uint8_t*)"{\"", 2);
            ofile.write((uint8_t*)buffer, strlen(buffer));
            ofile.write((uint8_t*)"\":{", 3);
          }
        }
      }
      else {
        buffer[index++] = buf[0];      // Add key name
        if (index > sizeof(buffer) -2) {
          break;                       // Key name too long
        }
      }
    }
  }
  ifile.close();
  if (append) {
    // Append new data
    ofile.write((entries) ? (uint8_t*)"," : (uint8_t*)"{", 1);
    ofile.write((uint8_t*)data +1, strlen(data) -1);
  } else {
    // Delete data
    if (entries) {
      ofile.write((uint8_t*)"}", 1);
    }
  }
  ofile.close();

  if (index > sizeof(buffer) -2) { 
    // No changes
    ffsp->remove(bfname);
    return false;                      // Error - Key name too long
  }
  ffsp->remove(filename);
  ffsp->rename(bfname, filename);
  if (!append) {
    // Delete data
    if (!entries) {
      ffsp->remove(filename);
    }
    return deleted;                    // State - 0 = Not found, 1 = Deleted
  }
  return true;                         // State - Append success
}

bool UfsJsonSettingsDelete(const char* key) {
  // Delete: Input UserSet2
  //         Output 0 = Not found, 1 = Deleted
  return _UfsJsonSettingsUpdate(key);  // State - 0 = Not found, 1 = Deleted
}

bool UfsJsonSettingsWrite(const char* data) {
  // Add new UserSet replacing present UserSet
  // Input {"UserSet2":{"Param1":123,"Param2":"Text2"}}
  // Output 0 = Error, 1 = Append success

  String json = data;
  JsonParser parser((char*)json.c_str());
  JsonParserObject root = parser.getRootObject();
  if (!root) { return false; }         // Error - invalid JSON

  char filename[14];
  snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), 0);  // /.drvset000
  if (!TfsFileExists(filename)) {
    return TfsSaveFile(filename, (uint8_t*)data, strlen(data));
  }
  return _UfsJsonSettingsUpdate(data); // State - 0 = Error, 1 = Append success
}

String UfsJsonSettingsRead(const char* key) {
  // Read: Input UserSet2
  //       Output "" = Error, {"Param1":123,"Param2":"Text2","Param3":[{"Param3a":1},{"Param3b":1}]} = Data
  String data = "";
  char filename[14];
  snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), 0);      // /.drvset000
  if (!TfsFileExists(filename)) { return data; }  // Error - File not found
  File file = ffsp->open(filename, "r");
  if (!file) { return data; }          // Error - unable to open settings file

  Trim((char*)key);
  char buffer[128];
  uint8_t buf[1] = { 0 };
  uint32_t index = 0;
  uint32_t bracket_count = 0;
  bool quote = false;
  bool mine = false;
  while (file.available()) {           // Process file
    file.read(buf, 1);
    if (bracket_count > 1) {           // Build JSON
      if (mine) {
        buffer[index++] = buf[0];      // Add key data
        if (index > sizeof(buffer) -2) {
          buffer[index] = '\0'; 
          data += buffer;              // Add buffer to result
          index = 0;
        }
      }
      if (buf[0] == '}') {
        bracket_count--;
        if (1 == bracket_count) {
          if (mine) {
            break;                     // End of key data
          } else {
            index = 0;                 // End of data which is not mine
          }
        }
      }
      else if (buf[0] == '{') {        // Next bracket
        bracket_count++;
      }
    } else {
      if (buf[0] == '}') {             // Last bracket
        index = 0;
        break;                         // End of file - key not found
      }
      else if (buf[0] == '{') {
        bracket_count++;
        if (bracket_count > 1) {       // Skip first bracket
          index = 0;
          buffer[index++] = buf[0];    // Start of key data
        }
      }
      else if (buf[0] == '"') {
        quote ^= 1;
        if (quote) {
          index = 0;
        } else {
          buffer[index] = '\0';        // End of key name
          mine = (!strcasecmp(buffer, key));
        }
      }
      else {
        buffer[index++] = buf[0];      // Add key name
        if (index > sizeof(buffer) -2) {
          index = 0;
          break;                       // Key name too long
        }
      }
    }
  }
  file.close();
  buffer[index] = '\0';
  data += buffer;
  return data;                         // State - "" = Error, {"Param1":123,"Param2":"Text2"} = Data
}

/*********************************************************************************************\
 * Commands
\*********************************************************************************************/

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;
}

const char kUFSCommands[] PROGMEM = "Ufs|"  // Prefix
  "|Type|Size|Free|Delete|Rename|Run|List"
#ifdef UFILESYS_STATIC_SERVING
  "|Serve"
#endif
#ifdef USE_FTP
  "|FTP"
#endif
  ;

void (* const kUFSCommand[])(void) PROGMEM = {
  &UFSInfo, &UFSType, &UFSSize, &UFSFree, &UFSDelete, &UFSRename, &UFSRun, &UFSList
#ifdef UFILESYS_STATIC_SERVING
  ,&UFSServe
#endif
#ifdef USE_FTP
  ,&Switch_FTP
#endif
  };

void UFSInfo(void) {
  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();
}

void UFSType(void) {
  if (ffs_type && (ffs_type != ufs_type)) {
    Response_P(PSTR("{\"%s\":[%d,%d]}"), XdrvMailbox.command, ufs_type, ffs_type);
  } else {
    ResponseCmndNumber(ufs_type);
  }
}

void UFSSize(void) {
  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));
  }
}

void UFSFree(void) {
  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));
  }
}

void UFSDelete(void) {
  // UfsDelete  sdcard or flashfs file if only one of them available
  // UfsDelete2 flashfs file if available
  if (XdrvMailbox.data_len > 0) {
    char fname[UFS_FILENAME_SIZE];
    UfsFilename(fname, XdrvMailbox.data);
    bool result = false;
    if (ffs_type && (ffs_type != ufs_type) && (2 == XdrvMailbox.index)) {
      result = TfsDeleteFile(fname);
    } else {
      result = (ufs_type && (ufsp->remove(fname) || ufsp->rmdir(fname)));
    }
    if (!result) {
      ResponseCmndFailed();
    } 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) {
      ResponseCmndFailed();
    } else {
      ResponseCmndDone();
    }
  }
}

bool UFSListDir(char *path, bool hide_dot) {
  bool update = false;

  File dir = dfsp->open(path, UFS_FILE_READ);
  if (dir) {
    dir.rewindDirectory();
    char *ep;
    while (true) {
      File entry = dir.openNextFile();
      if (!entry) {
        break;
      }
      // esp32 returns path here, shorten to filename
      ep = (char*)entry.name();
      if (*ep == '/') { ep++; }
      char *lcp = strrchr(ep,'/');
      if (lcp) {
        ep = lcp + 1;
      }
      if (hide_dot && (*ep == '.')) { continue; }

      // osx formatted disks contain a lot of stuff we dont want
      bool hiddable = UfsReject((char*)ep);
      if (!hiddable || !UfsIsSDC() ) {
        String tstr = "";
        if (!entry.isDirectory()) {   // ESP32 does not support isFile()
          uint32_t tm = entry.getLastWrite();
          tstr = GetDT(tm);
        }
        ResponseAppend_P(PSTR("%c[\"%s\",\"%s\",%d]"), (!update)?'[':',', EscapeJSONString(ep).c_str(), tstr.c_str(), entry.size());
        update = true;
        entry.close();

        yield(); // trigger watchdog reset
      }
    }
    dir.close();
  }
  return update;
}

void UFSList(void) {
  // UfsList       - List all non-dot files and directories in root directory
  // UfsList /     - List all non-dot files and directories in root directory
  // UfsList2      - List all files and directories in root directory
  // UfsList /dir1 - List all non-dot files and directories in directory dir1
  bool hide_dot = (XdrvMailbox.index != 2);
  strlcpy(ufs_path, "/", sizeof(ufs_path));
  if (XdrvMailbox.data_len > 0) {
    strlcpy(ufs_path, XdrvMailbox.data, sizeof(ufs_path));
  }
  ResponseCmnd();
  if (UFSListDir(ufs_path, hide_dot)) {
    ResponseAppend_P(PSTR("]}"));
  } else {
    ResponseCmndDone();
  }
}

#ifdef UFILESYS_STATIC_SERVING
/*
* Serves a filesystem folder at a web url.
* NOTE - this is expensive on flash -> +2.5kbytes.
* like "UFSServe <fs path>,<url>[,<noauth>]"
* e.g. "UFSServe /sd/,/mysdcard/,1" - will serve the /sd/ fs folder as https://<ip>/mysdcard/ with no auth required
* e.g. "UFSServe /www/,/" - will serve the /www/ fs folder as https://<ip>/ with auth required if TAS has a password setup
* <noauth> defaults to 0 - i.e. the default is to require auth if configured
* it WILL serve on / - so conflicting urls could occur.  I beleive native TAS urls will have priority.
* you can serve multiple folders, and they can each be auth or noauth
*
* by default, it also enables cors on the webserver - this allows you to have 
* a website external to TAS which can access the files, else the browser refuses.
*/
#include "detail/mimetable.h"

//#define SERVING_DEBUG

// Static file handler that requests auth when required.
// Derives from RequestHandler: including "detail/RequestHandlersImpl.h" (for StaticRequestHandler)
// a second time gives multiple definitions of RequestHandler::addMiddleware() and others at link time.
class StaticRequestHandlerAuth : public RequestHandler {
public:
    StaticRequestHandlerAuth(FS& fs, const char* path, const char* uri, bool requireAuth):
      _fs(fs), _uri(uri), _path(path), _requireAuth(requireAuth)
    {
      File f = fs.open(path);
      _isFile = (f && (!f.isDirectory()));
      _baseUriLength = _uri.length();
    }

    bool canHandle(HTTPMethod requestMethod, const String& requestUri) override {
      if (requestMethod != HTTP_GET) { return false; }
      if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri)) { return false; }
      return true;
    }

    bool canHandle(WebServer& server, HTTPMethod requestMethod, const String& requestUri) override {
      (void)server;
      return canHandle(requestMethod, requestUri);
    }

    // True if path contains a "." or ".." segment (slash-delimited)
    static bool containsDotSegment(const String& path) {
      int start = 0;
      int len = path.length();
      while (start <= len) {
        int slash = path.indexOf('/', start);
        int end = (slash < 0) ? len : slash;
        int seg = end - start;
        if ((1 == seg || 2 == seg) && ('.' == path[start]) && ((1 == seg) || ('.' == path[start + 1]))) { return true; }
        if (slash < 0) { break; }
        start = slash + 1;
      }
      return false;
    }

    static String getContentType(const String& path) {
      char buff[sizeof(mime::mimeTable[0].mimeType)];
      // Check all entries but last one for match, return if found
      for (size_t i = 0; i < mime::maxType - 1; i++) {
        strcpy_P(buff, mime::mimeTable[i].endsWith);
        if (path.endsWith(buff)) {
          strcpy_P(buff, mime::mimeTable[i].mimeType);
          return String(buff);
        }
      }
      // Fall-through and just return default type
      strcpy_P(buff, mime::mimeTable[mime::maxType - 1].mimeType);
      return String(buff);
    }

    FS& _fs;
    String _uri;
    String _path;
    size_t _baseUriLength;
    bool _isFile;
    bool _requireAuth;


    // we replace the handle method, 
    // and look for authentication only if we would serve the file.
    // if we check earlier, then we will reject as unauth even though a later
    // handler may be public, and so fail to serve public files.
    bool handle(WebServer& server, HTTPMethod requestMethod, const String& uri) override {
        if (!canHandle(server, requestMethod, uri))
            return false;
        String requestUri(uri);

        //log_v("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
#ifdef SERVING_DEBUG
        AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handle: request=%s _uri=%s"), requestUri.c_str(), _uri.c_str());
#endif
        String path(_path);

        if (!_isFile) {
            // Base URI doesn't point to a file.
            // If a directory is requested, look for index file.
            if (requestUri.endsWith("/")) 
              requestUri += "index.htm";

            // Append whatever follows this URI in request to get the file path.
            // Reject "." and ".." segments (also percent-encoded) so the request can't escape the served folder
            String relative = requestUri.substring(_baseUriLength);
            if (containsDotSegment(relative) || containsDotSegment(WebServer::urlDecode(relative))) {
              AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "reject path %s"), requestUri.c_str());
              return false;
            }
            path += relative;
        }
#ifdef SERVING_DEBUG
        AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handle: path=%s, isFile=%d"), path.c_str(), _isFile);
#endif
        String contentType = getContentType(path);

        // look for gz file, only if the original specified path is not a gz.  So part only works to send gzip via content encoding when a non compressed is asked for
        // if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
        if (!path.endsWith(FPSTR(mime::mimeTable[mime::gz].endsWith)) && !_fs.exists(path))  {
            String pathWithGz = path + FPSTR(mime::mimeTable[mime::gz].endsWith);
            if(_fs.exists(pathWithGz))
                path += FPSTR(mime::mimeTable[mime::gz].endsWith);
        }

        File f = _fs.open(path, "r");
        if (!f || !f.available()){
            AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handler missing file?"));
            return false;
        }
#ifdef SERVING_DEBUG
        AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handler file open %d"), f.available());
#endif
        if (_requireAuth && !WebAuthenticate()) {
#ifdef SERVING_DEBUG
          AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_UFS "serv of %s denied"), requestUri.c_str());
#endif          
          Webserver->requestAuthentication();   // TasmotaWebServer Basic auth, avoids linking Digest auth
          return true;
        }

#ifdef SERVING_DEBUG
        AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handler sending"));
#endif
        uint8_t buff[512];
        uint32_t bread;
        uint32_t flen = f.available();
        WiFiClient download_Client = server.client();
        server.setContentLength(flen);   
        server.send(200, contentType, "");

        // transfer is about 150kb/s
        uint32_t cnt = 0;
        while (f.available()) {
          bread = f.read(buff, sizeof(buff));
#ifdef SERVING_DEBUG
          //AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handler sending %d/%d"), cnt, flen);
#endif          
          if (!WSClientWrite(download_Client, buff, bread)) { break; }
          cnt += bread;
          yield();
        }
#ifdef SERVING_DEBUG
        AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handler sent %d/%d"), cnt, flen);
#endif

        if (cnt != flen){
          AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_UFS "::handler incomplete file send: sent %d/%d"), cnt, flen);
        }

        // It does seem that on lesser ESP32, this causes a problem?  A lockup...
        //server.streamFile(f, contentType);

        f.close();
        download_Client.stop();

#ifdef SERVING_DEBUG
        AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "::handler done"));
#endif        
        return true;
    }
};

void UFSServe(void) {
  bool result = false;
  if (XdrvMailbox.data_len > 0) {
    char *fpath = strtok(XdrvMailbox.data, ",");
    char *url = strtok(nullptr, ",");
    char *noauth = strtok(nullptr, ",");
    if (fpath && url) {
      if (Webserver) { // fail if no Webserver yet.
        StaticRequestHandlerAuth *staticHandler;
        if (noauth && *noauth == '1'){
          staticHandler = new StaticRequestHandlerAuth(*ffsp, fpath, url, false);
        } else {
          staticHandler = new StaticRequestHandlerAuth(*ffsp, fpath, url, true);
        }
        if (staticHandler) {
          //Webserver->serveStatic(url, *ffsp, fpath);
          Webserver->addHandler(staticHandler);
          Webserver->enableCORS(true);
          result = true;
        }
      }
    }
  }
  if (!result) {
    ResponseCmndFailed();
  } else {
    ResponseCmndDone();
  }
}
#endif // UFILESYS_STATIC_SERVING

void UFSRun(void) {
  if (XdrvMailbox.data_len > 0) {
    char fname[UFS_FILENAME_SIZE];
    if (UfsExecuteCommandFile(UfsFilename(fname, XdrvMailbox.data))) {
      ResponseClear();
    } else {
      ResponseCmndFailed();
    }
  } else {
    bool not_active = UfsExecuteCommandFileReady();
    UfsData.run_file_pos = -1;
    ResponseCmndChar(not_active ? PSTR(D_JSON_DONE) : PSTR(D_JSON_ABORTED));
  }
}

/*********************************************************************************************\
 * Web support
\*********************************************************************************************/

#ifdef USE_WEBSERVER

const char UFS_WEB_DIR[] PROGMEM =
  "<p></p><form action='ufsd' method='get'><input type='hidden' name='download' value='%s' /> <button>%s</button></form>";

const char UFS_CURRDIR[] PROGMEM = 
  "<p>%s: %s</p>";

#ifndef D_CURR_DIR
  #define D_CURR_DIR "Folder"
#endif

const char UFS_FORM_FILE_UPGc[] PROGMEM =
  "<div style='text-align:left;color:#%06x;'>" D_FS_SIZE " %s MB - " D_FS_FREE " %s MB";

const char UFS_FORM_FILE_UPGc1[] PROGMEM =
    " &nbsp;&nbsp;<a href='/ufsd?dir=%d'>%s</a>";

const char UFS_FORM_FILE_UPGc2[] PROGMEM =
  "</div>";

const char UFS_FORM_FILE_UPG[] PROGMEM =
  "<form method='post' action='ufsu?fsz=' enctype='multipart/form-data'>"
  "<br><input type='file' name='ufsu'><br>"
  "<br><button type='submit' "
  "onclick='eb(\"f1\").style.display=\"none\";eb(\"but6\").style.display=\"none\";eb(\"f2\").style.display=\"block\";this.form.action+=this.form[\"ufsu\"].files[0].size;this.form.submit();'"
  ">" D_UPLOAD "</button></form>"
  "<br><hr>";
const char UFS_FORM_SDC_DIRa[] PROGMEM =
  "<div style='text-align:left;overflow:auto;height:250px;'>";
const char UFS_FORM_SDC_DIRc[] PROGMEM =
  "</div>";
const char UFS_FORM_FILE_UPGb[] PROGMEM =
  "<input type='hidden' name='file' value='%s/" D_NEW_FILE "'>"
  "<button type='submit'>" D_CREATE_NEW_FILE "</button></form>";
const char UFS_FORM_FILE_UPGb1[] PROGMEM =
  "<label><input type='checkbox' id='shf' onclick='sf(eb(\"shf\").checked);' name='shf'>" D_SHOW_HIDDEN_FILES "</label>";

const char UFS_FORM_FILE_UPGb2[] PROGMEM =
  "</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'";
const char UFS_FORM_SDC_DIRd[] PROGMEM =
  "<pre><a href='%s' file='%s'>%s</a></pre>";
const char UFS_FORM_SDC_DIRb[] PROGMEM =
  "<pre%s><a href='%s' file='%s'>%s</a> %19s %8d %s %s</pre>";
const char UFS_FORM_SDC_HREF[] PROGMEM =
  "ufsd?download=%s/%s";

#ifdef GUI_TRASH_FILE
const char UFS_FORM_SDC_HREFdel[] PROGMEM =
  //"<a href=ufsd?delete=%s/%s>&#128465;</a>"; // 🗑️
  "<a href='ufsd?delete=%s/%s&download=%s' onclick=\"return confirm('" D_CONFIRM_FILE_DEL "')\">&#128293;</a>"; // 🔥
#endif // GUI_TRASH_FILE

#ifdef GUI_EDIT_FILE

#define FILE_BUFFER_SIZE  1024

const char UFS_FORM_SDC_HREFedit[] PROGMEM =
  "<a href='ufse?file=%s/%s'>&#x1F4DD;</a>"; // 📝

const char HTTP_EDITOR_FORM_START[] PROGMEM =
  "<form>"
  "<label for='name'>" D_FILE ":</label><input type='text' id='name' name='name' value='%s'><br><hr width='98%%'>"
  "<textarea id='content' name='content' wrap='off' rows='8' cols='80' style='font-size: 12pt'>";

const char HTTP_EDITOR_FORM_END[] PROGMEM =
  "</textarea>"
  "<button name='save' type='submit' formmethod='post' formenctype='multipart/form-data' formaction='/ufse' class='button bgrn'>" D_SAVE "</button>"
  "</form></fieldset>";

#endif  // #ifdef GUI_EDIT_FILE

// Wrapper around HandleUploadLoop() for /ufsu file uploads.
// HandleUploadLoop() is shared between OTA firmware updates (/u2) and filesystem
// uploads (/ufsu), and uses Web.upload_file_type to distinguish them.
// When uploading via the web UI, the browser first does a GET which calls
// UfsDirectory() and sets upload_file_type = UPL_UFSFILE. But direct POST
// requests (e.g. curl -F "file=@..." /ufsu) skip the GET, leaving
// upload_file_type unset and causing HandleUploadLoop() to treat the file
// as a firmware image, which fails.
// This wrapper ensures upload_file_type is always set before entering the
// shared upload handler.
void HandleUploadUFSLoop(void) {
  if (!WebAuthenticate()) { return; }
  if (!HttpCheckPriviledgedAccess(false)) { return; }

  Web.upload_file_type = UPL_UFSFILE;
  HandleUploadLoop();
}

void HandleUploadUFSDone(void) {
  if (!HttpCheckPriviledgedAccess()) { return; }

  Web.upload_file_type = UPL_NONE;

  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_UPLOAD_DONE));

  WifiConfigCounter();
  UploadServices(1);

  WSContentStart_P(PSTR(D_INFORMATION));

  WSContentSendStyle();
  WSContentSend_P(PSTR("<div style='text-align:center;'><b>" D_UPLOAD " <font color='#"));
  if (Web.upload_error) {
    WSContentSend_P(PSTR("%06x'>" D_FAILED "</font></b><br><br>"), WebColor(COL_TEXT_WARNING));
    char error[100];
    if (Web.upload_error < 10) {
      GetTextIndexed(error, sizeof(error), Web.upload_error -1, kUploadErrors);
    } else {
      snprintf_P(error, sizeof(error), PSTR(D_UPLOAD_ERROR_CODE " %d"), Web.upload_error);
    }
    WSContentSend_P(error);
    Web.upload_error = 0;
  } else {
    WSContentSend_P(PSTR("%06x'>" D_SUCCESSFUL "</font></b><br>"), WebColor(COL_TEXT_SUCCESS));
  }
  WSContentSend_P(PSTR("</div><br>"));

  WSContentSend_PD(UFS_WEB_DIR, "/", PSTR(D_MANAGE_FILE_SYSTEM));

  WSContentStop();
}

void UfsDirectory(void) {
  if (!HttpCheckPriviledgedAccess()) { return; }

  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_MANAGE_FILE_SYSTEM));

  uint8_t depth = 0;
  uint8_t isdir = 0;

  strlcpy(ufs_path, "/", sizeof(ufs_path));

  if (Webserver->hasArg(F("dir"))) {
    String stmp = Webserver->arg(F("dir"));
    ufs_dir = atoi(stmp.c_str());
  }

  if (ufs_dir == 1) {
    dfsp = ufsp;
  } else {
    if (ffsp) {
      dfsp = ffsp;
    }
  }

  if (Webserver->hasArg(F("delete"))) {
    String stmp = Webserver->arg(F("delete"));
    char *cp = (char*)stmp.c_str();
    File download_file = dfsp->open(cp, UFS_FILE_READ);
    if (download_file) {
      if (download_file.isDirectory()) {
        download_file.close();
        dfsp->rmdir(cp);
      } else {
        download_file.close();
        dfsp->remove(cp);
      }
    }
  }

  if (Webserver->hasArg(F("download"))) {
    String stmp = Webserver->arg(F("download"));
    char *cp = (char*)stmp.c_str();
    if (UfsDownloadFile(cp)) {
      // is directory
      strlcpy(ufs_path, cp, sizeof(ufs_path));
      isdir = 1;
    } else {
      return;
    }
  }

  WSContentStart_P(PSTR(D_MANAGE_FILE_SYSTEM));
  WSContentSendStyle();
  WSContentSend_P(HTTP_DIV_F1_BLOCK);
  WSContentSend_P(HTTP_FIELDSET_LEGEND, PSTR(D_MANAGE_FILE_SYSTEM));

  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);

  if (ufs_dir) {
    WSContentSend_P(UFS_FORM_FILE_UPGc1, (ufs_dir == 1)?2:1, (ufs_dir == 1)?PSTR("SDCard"):PSTR("FlashFS"));
  }
  WSContentSend_P(UFS_FORM_FILE_UPGc2);

  WSContentSend_P(UFS_FORM_FILE_UPG);

  if (isdir){
    // if a folder, show 'folder: xxx' if not '/'
    if (ufs_path[0] != '/' || strlen(ufs_path) != 1){
      WSContentSend_P(UFS_CURRDIR, PSTR(D_CURR_DIR), ufs_path);
    }
  }

  WSContentSend_P(UFS_FORM_SDC_DIRa);
  if (ufs_type) {
    UfsListDir(ufs_path, depth);
  }
  WSContentSend_P(UFS_FORM_SDC_DIRc);
#ifdef GUI_EDIT_FILE
  WSContentSend_P(HTTP_FORM_GET_ACTION, PSTR("ufse"));
  WSContentSend_P(UFS_FORM_FILE_UPGb, ufs_path);
#endif
  if (!UfsIsSDC()) {
    WSContentSend_P(UFS_FORM_FILE_UPGb1);
  }
  WSContentSend_P(UFS_FORM_FILE_UPGb2);

  WSContentSpaceButton(BUTTON_MANAGEMENT);
  WSContentStop();

  Web.upload_file_type = UPL_UFSFILE;
}

void UfsListDir(char *path, uint8_t depth) {
  char name[UFS_FILENAME_SIZE];
  char npath[128];
  char format[12];
  sprintf(format, PSTR("%%-%ds"), 24 - depth);

  File dir = dfsp->open(path, UFS_FILE_READ);
  if (dir) {
    dir.rewindDirectory();
    if (strlen(path)>1) {
      ext_snprintf_P(npath, sizeof(npath), PSTR("ufsd?download=%s"), path);
      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;
          }
          break;
        }
      }
      WSContentSend_P(UFS_FORM_SDC_DIRd, npath, path, PSTR(".."));
    }
    char *ep;
    while (true) {
      WiFiClient client = Webserver->client();
      // abort if the client disconnected
      // if there is a huge folder, then this gives a way out by refresh of browser
      if (!client.connected()){
        break;
      }

      File entry = dir.openNextFile();
      if (!entry) {
        break;
      }
      // esp32 returns path here, shorten to filename
      ep = (char*)entry.name();
      if (*ep == '/') { ep++; }
      char *lcp = strrchr(ep,'/');
      if (lcp) {
        ep = lcp + 1;
      }

      uint32_t tm = entry.getLastWrite();
      String tstr = GetDT(tm);

      char *pp = path;
      if (!*(pp + 1)) { pp++; }
      char *cp = name;
      // osx formatted disks contain a lot of stuff we dont want
      bool hiddable = UfsReject((char*)ep);

      if (!hiddable || !UfsIsSDC() ) {

        for (uint8_t cnt = 0; cnt<depth; cnt++) {
          *cp++ = '-';
        }
        snprintf(cp, sizeof(name) - (cp - name), format, ep);

        String pp_escaped_string = UrlEscape(pp);
        String ep_escaped_string = UrlEscape(ep);
        const char* ppe = pp_escaped_string.c_str();    // this can't be merged on a single line otherwise the String object can be freed
        const char* epe = ep_escaped_string.c_str();
#ifdef GUI_TRASH_FILE
        char delpath[128+UFS_FILENAME_SIZE+(2*strlen(ppe))];
        ext_snprintf_P(delpath, sizeof(delpath), UFS_FORM_SDC_HREFdel, ppe, epe, ppe[0]?ppe:"/");
#else
        char delpath[2] = " ";
#endif // GUI_TRASH_FILE
        if (entry.isDirectory()) {
          ext_snprintf_P(npath, sizeof(npath), UFS_FORM_SDC_HREF, ppe, epe);

          WSContentSend_P(UFS_FORM_SDC_DIRb, hiddable ? UFS_FORM_SDC_DIR_HIDDABLE : UFS_FORM_SDC_DIR_NORMAL, npath, epe,
                          HtmlEscape(name).c_str(), "", 0, delpath, " ");
          //WSContentSend_P(UFS_FORM_SDC_DIRd, npath, ep, name);
#ifdef UFILESYS_RECURSEFOLDERS_GUI
          uint8_t plen = strlen(path);
          if (plen > 1) {
            strlcat(path, "/", UFS_FILENAME_SIZE);
          }
          strlcat(path, ep, UFS_FILENAME_SIZE);
          UfsListDir(path, depth + 4);
          path[plen] = 0;
#endif          
        } else {
  #ifdef GUI_EDIT_FILE
          char editpath[128];
          ext_snprintf_P(editpath, sizeof(editpath), UFS_FORM_SDC_HREFedit, ppe, epe);
  #else
          char editpath[2];
          editpath[0]=0;
  #endif // GUI_TRASH_FILE
          ext_snprintf_P(npath, sizeof(npath), UFS_FORM_SDC_HREF, ppe, epe);
          WSContentSend_P(UFS_FORM_SDC_DIRb, hiddable ? UFS_FORM_SDC_DIR_HIDDABLE : UFS_FORM_SDC_DIR_NORMAL, npath, epe,
                          HtmlEscape(name).c_str(), tstr.c_str(), entry.size(), delpath, editpath);
        }
        entry.close();

        yield(); // trigger watchdog reset
      }
    }
    dir.close();
  }
}

#ifdef ESP32
// this actually does not work reliably, as the 
// webserver can close the connection before the download task completes
//#define ESP32_DOWNLOAD_TASK
#endif // ESP32

uint8_t UfsDownloadFile(char *file) {
  File download_file;

  AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "File '%s' download"), file);

  if (!dfsp->exists(file)) {
    AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "File '%s' not found"), file);
    return 0;
  }

  download_file = dfsp->open(file, UFS_FILE_READ);
  if (!download_file) {
    AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "Could not open file '%s'"), file);
    return 0;
  }

  if (download_file.isDirectory()) {
    AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "File '%s' to download is directory"), file);
    download_file.close();
    return 1;
  }

#ifndef ESP32_DOWNLOAD_TASK
  WiFiClient download_Client;
  uint32_t flen = download_file.size();

  download_Client = Webserver->client();
  Webserver->setContentLength(flen);

  char attachment[100];
  char *cp = fileOnly(file);
  snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=%s"), cp);
  Webserver->sendHeader(F("Content-Disposition"), attachment);
  WSSend(200, CT_APP_STREAM, "");

  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));
    if (!WSClientWrite(download_Client, buff, bread)) { break; }
    cnt++;
    if (cnt > 7) {
      cnt = 0;
      //if (glob_script_mem.script_loglevel & 0x80) {
        // this indeed multitasks, but is slower 50 kB/s
      //  loop();
      //}
    }
    delay(0);
    OsWatchLoop();
  }
  download_file.close();
  download_Client.stop();
#endif // ESP32_DOWNLOAD_TASK



// to make this work I thing you wouold need to duplicate the client 
// BEFORE starting the task, so that the webserver does not close it's
// copy of the client.
#ifdef ESP32_DOWNLOAD_TASK
  download_file.close();

  if (UfsData.download_busy == true) {
    AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "Download is busy"));
    return 0;
  }

  UfsData.download_busy = true;
  char *path = (char*)malloc(128);
  strlcpy(path, file, 128);
  BaseType_t ret = xTaskCreatePinnedToCore(download_task, "DT", 6000, (void*)path, 3, nullptr, 1);
  if (ret != pdPASS)
    AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "Download task failed with %d"), ret);
  yield();
#endif // ESP32_DOWNLOAD_TASK

  return 0;
}


#ifdef ESP32_DOWNLOAD_TASK
#ifndef DOWNLOAD_SIZE
#define DOWNLOAD_SIZE 4096
#endif // DOWNLOAD_SIZE
void download_task(void *path) {
  File download_file;
  WiFiClient download_Client;
  char *file = (char*) path;

  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "ESP32 File '%s' to download"), file);

  download_file = dfsp->open(file, UFS_FILE_READ);
  uint32_t flen = download_file.size();

  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "len %d to download"), flen);

  download_Client = Webserver->client();
  Webserver->setContentLength(flen);

  char attachment[100];
  char *cp = fileOnly(file);
  //snprintf_P(attachment, sizeof(attachment), PSTR("download file '%s' as '%s'"), file, cp);
  //Webserver->sendHeader(F("X-Tasmota-Debug"), attachment);
  snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=%s"), cp);
  Webserver->sendHeader(F("Content-Disposition"), attachment);
  WSSend(200, CT_APP_STREAM, "");

  uint8_t *buff = (uint8_t*)malloc(DOWNLOAD_SIZE);
  if (buff) {
    uint32_t bread;
    while (download_file.available()) {
      bread = download_file.read(buff, DOWNLOAD_SIZE);
      if (!WSClientWrite(download_Client, buff, bread)) { break; }
    }
    free(buff);
  }
  download_file.close();
  download_Client.stop();
  UfsData.download_busy = false;
  vTaskDelete( NULL );
  free(path);
  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "esp32 sent file"));
}
#endif //  ESP32_DOWNLOAD_TASK


bool UfsUploadFileOpen(const char* upload_filename) {
  char npath[UFS_FILENAME_SIZE];
  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);
  } else {
    return false;
  }
  return true;
}

void UfsUploadFileClose(void) {
  ufs_upload_file.close();
}

//******************************************************************************************
//  File Editor
//******************************************************************************************

#ifdef GUI_EDIT_FILE

void UfsEditor(void) {
  if (!HttpCheckPriviledgedAccess()) { return; }

  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "UfsEditor GET"));

  char fname_input[UFS_FILENAME_SIZE];
  if (Webserver->hasArg(F("file"))) {
    WebGetArg(PSTR("file"), fname_input, sizeof(fname_input));
  } else {
    snprintf_P(fname_input, sizeof(fname_input), PSTR(D_NEW_FILE));
  }
  char fname[UFS_FILENAME_SIZE];
  UfsFilename(fname, fname_input);                  // Trim spaces and add slash

  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "UfsEditor: file=%s, ffs_type=%d, TfsFileExist=%d"), fname, ffs_type, dfsp->exists(fname));

  WSContentStart_P(PSTR(D_EDIT_FILE));
  WSContentSendStyle();
  WSContentSend_P(HTTP_FIELDSET_LEGEND, PSTR(D_EDIT_FILE));
  char *bfname = fname +1;
  WSContentSend_P(HTTP_EDITOR_FORM_START, bfname);  // Skip leading slash

  if (ffs_type && dfsp->exists(fname)) {
    File fp = dfsp->open(fname, "r");
    if (!fp) {
      AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "UfsEditor: file open failed"));
      WSContentSend_P(D_NEW_FILE);
    } else {
      uint8_t *buf = (uint8_t*)malloc(FILE_BUFFER_SIZE+1);
      size_t filelen = fp.size();
      AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "UfsEditor: file len=%d"), filelen);
      while (filelen > 0) {
        size_t l = fp.read(buf, FILE_BUFFER_SIZE);
        AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_UFS "UfsEditor: read=%d"), l);
        if (l < 0) { break; }
        buf[l] = '\0';
        WSContentSendRaw_P( HtmlEscape((char*)buf).c_str());
        filelen -= l;
      }
      fp.close();
      free(buf);
      AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "UfsEditor: read done"));
    }
  } else {
    WSContentSend_P(D_NEW_FILE);
  }

  WSContentSend_P(HTTP_EDITOR_FORM_END);
  folderOnly(fname);
  WSContentSend_P(UFS_WEB_DIR, fname, PSTR(D_MANAGE_FILE_SYSTEM));
  WSContentStop();
}

void UfsEditorUpload(void) {
  AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_UFS "UfsEditor: file upload"));

  if (!HttpCheckPriviledgedAccess()) { return; }

  if (!Webserver->hasArg("name")) {
    AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_UFS "UfsEditor: file upload - no filename"));
    WSSend(400, CT_PLAIN, F("400: Bad request - no filename"));
    return;
  }

  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(D_LOG_UFS "UfsEditor: file '%s'"), fname);

  if (!Webserver->hasArg("content")) {
    AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_UFS "UfsEditor: file upload - no content"));
    WSSend(400, CT_PLAIN, F("400: Bad request - no content"));
    return;
  }
  String content = Webserver->arg("content");

  if (!dfsp) {
    Web.upload_error = 1;
    AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_UFS "UfsEditor: 507: no storage available"));
    WSSend(507, CT_PLAIN, F("507: no storage available"));
    return;
  }

  // recursively create folder(s)
  char tmp[UFS_FILENAME_SIZE];
  char folder[UFS_FILENAME_SIZE] = "";
  strlcpy(tmp, fname, sizeof(tmp));
  // zap file name off the end
  folderOnly(tmp);
  char *tf = strtok(tmp, "/");
  while(tf){
    if (*tf){
      strlcat(folder, "/", sizeof(folder));
      strlcat(folder, tf, sizeof(folder));
    }
    // we don;t care if it fails - it may already exist.
    dfsp->mkdir(folder);
    tf = strtok(nullptr, "/");
  }

  File fp = dfsp->open(fname, "w");
  if (!fp) {
    Web.upload_error = 1;
    AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_UFS "UfsEditor: 400: invalid file name '%s'"), fname);
    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)) {
    AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_UFS "UfsEditor: write error on '%s'"), fname);
  }

  fp.close();

  // zap file name off the end
  folderOnly(fname);
  char t[20+UFS_FILENAME_SIZE] = "/ufsu?download=";
  strlcat(t, fname, sizeof(t));
  Webserver->sendHeader(F("Location"), t);
  Webserver->send(303);
}

#endif  // GUI_EDIT_FILE

#endif  // USE_WEBSERVER


#ifdef USE_FTP
#include <ESPFtpServer.h>
FtpServer *ftpSrv;

void FTP_Server(uint32_t mode) {
  if (mode > 0) {
    if (ftpSrv) {
      delete ftpSrv;
    }
    ftpSrv = new FtpServer;
    if (mode == 1) {
      ftpSrv->begin(USER_FTP,PW_FTP, ufsp);
    } else {
      ftpSrv->begin(USER_FTP,PW_FTP, ffsp);
    }
    AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UFS "FTP Server started in mode: '%d'"), mode);
  } else {
    if (ftpSrv) {
      delete ftpSrv;
      ftpSrv = nullptr;
    }
  }
}

void Switch_FTP(void) {
  if (XdrvMailbox.data_len > 0) {
    if (XdrvMailbox.payload >= 0 && XdrvMailbox.payload <= 2) {
      FTP_Server(XdrvMailbox.payload);
      Settings->mbflag2.FTP_Mode = XdrvMailbox.payload;
    }
  }
  ResponseCmndNumber(Settings->mbflag2.FTP_Mode);
}
#endif // USE_FTP

/*********************************************************************************************\
 * Interface
\*********************************************************************************************/

bool Xdrv50(uint32_t function) {
  bool result = false;

  switch (function) {
    case FUNC_LOOP:
      UfsExecuteCommandFileLoop();

#ifdef USE_FTP
      if (ftpSrv) {
        ftpSrv->handleFTP();
      }
#endif

      break;
    
    case FUNC_NETWORK_UP:
#ifdef USE_FTP
      if (Settings->mbflag2.FTP_Mode && !ftpSrv) {
        FTP_Server(Settings->mbflag2.FTP_Mode);
      }
#endif
      break;

/*
// Moved to support_tasmota.ino for earlier init to be used by scripter
#ifdef USE_SDCARD
    case FUNC_PRE_INIT:
      UfsCheckSDCardInit();
      break;
#endif // USE_SDCARD
*/
    case FUNC_MQTT_INIT:
      if (!TasmotaGlobal.no_autoexec) {
        UfsExecuteCommandFile(TASM_FILE_AUTOEXEC);
      }
      break;
    case FUNC_COMMAND:
      result = DecodeCommand(kUFSCommands, kUFSCommand);
      break;
#ifdef USE_WEBSERVER
    case FUNC_WEB_ADD_MANAGEMENT_BUTTON:
      if (ufs_type) {
        if (XdrvMailbox.index) {
          XdrvMailbox.index++;
        } else {
          WSContentSend_PD(UFS_WEB_DIR, "/", PSTR(D_MANAGE_FILE_SYSTEM));
        }
      }
      break;
    case FUNC_WEB_ADD_HANDLER:
      Webserver->on("/ufsd", UfsDirectory);
      Webserver->on("/ufsu", HTTP_GET, UfsDirectory);
      Webserver->on("/ufsu", HTTP_POST, HandleUploadUFSDone, HandleUploadUFSLoop);
#ifdef GUI_EDIT_FILE
      Webserver->on("/ufse", HTTP_GET, UfsEditor);
      Webserver->on("/ufse", HTTP_POST, UfsEditorUpload);
#endif
      break;
#endif // USE_WEBSERVER
    case FUNC_ACTIVE:
      result = true;
      break;
  }
  return result;
}
#endif  // USE_UFILESYS
