LittleFS_ESP32.ino



// ****************************************************************
// Sketch Esp32 Datei Manager spezifisch sortiert Modular(Tab)
// created: Jens Fleischer, 2020-03-26
// last mod: Jens Fleischer, 2020-09-05
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp32
// Software: Esp32 Arduino Core 1.0.0 - 1.0.4
// Geprüft: mit 4MB Flash
// Getestet auf: ESP32 NodeMCU-32s
/******************************************************************
  Copyright (c) 2020 Jens Fleischer. All rights reserved.

  This file is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This file 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
  Lesser General Public License for more details.
*******************************************************************/
// Diese Version von LittleFS sollte als Tab eingebunden werden.
// #include <LittleFS.h> #include <WebServer.h> müssen im Haupttab aufgerufen werden
// Die Funktionalität des ESP32 Webservers ist erforderlich.
// "server.onNotFound()" darf nicht im Setup des ESP32 Webserver stehen.
// Die Funktion "littlefs();" muss im Setup aufgerufen werden.
/**************************************************************************************/

#include <detail/RequestHandlersImpl.h>
#include <list>

const char HELPER[] PROGMEM = R"(<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="[]" multiple><button>Upload</button></form>Lade die littlefs.html hoch.)";

void littlefs() {                                                                             // Funktionsaufruf "littlefs();" muss im Setup eingebunden werden
  LittleFS.begin(true);
  server.on("/list", handleList);
  server.on("/format", formatLittleFS);
  server.on("/upload", HTTP_POST, sendResponce, handleFileUpload);
  server.onNotFound([]() {
    if (!handleFile(server.urlDecode(server.uri())))
      server.send(404, "text/plain", "FileNotFound");
  });
}

void handleList() {                                                                         // Senden aller Daten an den Client
  File root = LittleFS.open("/");
  typedef std::pair<String, int> prop;
  std::list<prop> dirList;                                                                  // Liste anlegen
  while (File f = root.openNextFile()) dirList.emplace_back(f.name(), f.size());            // Liste füllen
  dirList.sort([](const prop & f, const prop & l) {                                         // Liste sortieren
    if (server.arg(0) == "1") {
      return f.second > l.second;
    } else {
      for (uint8_t i = 0; i < 31; i++) {
        if (tolower(f.first[i]) < tolower(l.first[i])) return true;
        else if (tolower(f.first[i]) > tolower(l.first[i])) return false;
      }
      return false;
    }
  });
  String temp = "[";
  for (auto& p : dirList) {
    if (temp != "[") temp += ',';
    temp += "{\"name\":\"" + p.first + "\",\"size\":\"" + formatBytes(p.second) + "\"}";  
  }
  temp += R"(,{"usedBytes":")" + formatBytes(LittleFS.usedBytes() * 1.05) + R"(",)" +         // Berechnet den verwendeten Speicherplatz + 5% Sicherheitsaufschlag
          R"("totalBytes":")" + formatBytes(LittleFS.totalBytes()) + R"(","freeBytes":")" +   // Zeigt die Größe des Speichers
          (LittleFS.totalBytes() - (LittleFS.usedBytes() * 1.05)) + R"("}])";                   // Berechnet den freien Speicherplatz + 5% Sicherheitsaufschlag
  server.send(200, "application/json", temp);
}

bool handleFile(String&& path) {
  if (server.hasArg("delete")) {
    LittleFS.remove(server.arg("delete"));                                                    // Datei löschen
    sendResponce();
    return true;
  }
  if (!LittleFS.exists("/littlefs.html"))server.send(200, "text/html", HELPER);                 // ermöglicht das hochladen der littlefs.html
  if (path.endsWith("/")) path += "index.html";
  return LittleFS.exists(path) ? ({File f = LittleFS.open(path); server.streamFile(f, StaticRequestHandler::getContentType(path)); f.close(); true;}) : false;
}

void handleFileUpload() {                                  // Dateien vom Rechnenknecht oder Klingelkasten ins LittleFS schreiben
  static File fsUploadFile;
  HTTPUpload& upload = server.upload();
  if (upload.status == UPLOAD_FILE_START) {
    if (upload.filename.length() > 30) {
      upload.filename = upload.filename.substring(upload.filename.length() - 30, upload.filename.length());  // Dateinamen auf 30 Zeichen kürzen
    }
    DEBUG_F("handleFileUpload Name: /%s\n", upload.filename.c_str());
    fsUploadFile = LittleFS.open("/" + server.urlDecode(upload.filename), "w");
  } else if (upload.status == UPLOAD_FILE_WRITE) {
    DEBUG_F("handleFileUpload Data: %u\n", upload.currentSize);
    if (fsUploadFile)
      fsUploadFile.write(upload.buf, upload.currentSize);
  } else if (upload.status == UPLOAD_FILE_END) {
    if (fsUploadFile)
      fsUploadFile.close();
    DEBUG_F("handleFileUpload Size: %u\n", upload.totalSize);
  }
}

void formatLittleFS() {       //Formatiert den Speicher
  LittleFS.format();
  sendResponce();
}

void sendResponce() {
  server.sendHeader("Location", "littlefs.html");
  server.send(303, "message/http");
}

const String formatBytes(size_t const& bytes) {            // lesbare Anzeige der Speichergrößen
  return bytes < 1024 ? static_cast<String>(bytes) + " Byte" : bytes < 1048576 ? static_cast<String>(bytes / 1024.0) + " KB" : static_cast<String>(bytes / 1048576.0) + " MB";
}

bool freeSpace(uint16_t const& printsize) {               // Funktion um beim speichern in Logdateien zu prüfen ob noch genügend freier Platz verfügbar ist.
  DEBUG_F("Funktion: %s meldet in Zeile: %d FreeSpace: %s\n", __PRETTY_FUNCTION__, __LINE__, formatBytes(LittleFS.totalBytes() - (LittleFS.usedBytes() * 1.05)).c_str());
  return (LittleFS.totalBytes() - (LittleFS.usedBytes() * 1.05) > printsize) ? true : false;
}

/*
Sketchname: F:\Arduino\ESP32\ESP_LittleFS\ESP_LittleFS.ino
Build: Wed Nov 09 07:50:57 2022    IDE: 1.8.19

./components/esp_littlefs/src/littlefs/lfs.c:1229:error: Corrupted dir pair at {0x0, 0x1}
E (35) esp_littlefs: mount failed,  (-84)
E (39) esp_littlefs: Failed to initialize LittleFS
.
Verbunden mit: FRITZ!Box 7590 UN
Esp32 IP: 192.168.178.22

HTTP Server gestartet


handleFileUpload Name: /littlefs.html
handleFileUpload Data: 1436
handleFileUpload Data: 1355
handleFileUpload Size: 2791
handleFileUpload Name: /style32.css
handleFileUpload Data: 1436
handleFileUpload Data: 1
handleFileUpload Size: 1437
handleFileUpload Name: /admin.html
handleFileUpload Data: 1436
handleFileUpload Data: 1436
handleFileUpload Data: 1436
handleFileUpload Data: 1436
handleFileUpload Data: 1013
handleFileUpload Size: 6757
handleFileUpload Name: /favicon.ico
handleFileUpload Data: 1436
handleFileUpload Data: 317
handleFileUpload Size: 1753
handleFileUpload Name: /onboardled.html
handleFileUpload Data: 998
handleFileUpload Size: 998
LED ist an
LED ist aus
handleFileUpload Name: /admin.html
handleFileUpload Data: 1436
handleFileUpload Data: 1436
handleFileUpload Data: 1436
handleFileUpload Data: 1436
handleFileUpload Data: 1014
handleFileUpload Size: 6758

*/