Initial
This commit is contained in:
143
src/wrapper/base64_wrapper.cpp
Normal file
143
src/wrapper/base64_wrapper.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
Copyright (C) 2016 Arturo Guadalupi. All right reserved.
|
||||
|
||||
This library 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 library 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.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "base64_wrapper.h"
|
||||
#if (defined(__AVR__))
|
||||
#include <avr/pgmspace.h>
|
||||
#else
|
||||
#include <pgmspace.h>
|
||||
#endif
|
||||
|
||||
const char PROGMEM _Base64AlphabetTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
int Base64Class::encode(char *output, char *input, int inputLength) {
|
||||
int i = 0, j = 0;
|
||||
int encodedLength = 0;
|
||||
unsigned char A3[3];
|
||||
unsigned char A4[4];
|
||||
|
||||
while(inputLength--) {
|
||||
A3[i++] = *(input++);
|
||||
if(i == 3) {
|
||||
fromA3ToA4(A4, A3);
|
||||
|
||||
for(i = 0; i < 4; i++) {
|
||||
output[encodedLength++] = pgm_read_byte(&_Base64AlphabetTable[A4[i]]);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(i) {
|
||||
for(j = i; j < 3; j++) {
|
||||
A3[j] = '\0';
|
||||
}
|
||||
|
||||
fromA3ToA4(A4, A3);
|
||||
|
||||
for(j = 0; j < i + 1; j++) {
|
||||
output[encodedLength++] = pgm_read_byte(&_Base64AlphabetTable[A4[j]]);
|
||||
}
|
||||
|
||||
while((i++ < 3)) {
|
||||
output[encodedLength++] = '=';
|
||||
}
|
||||
}
|
||||
output[encodedLength] = '\0';
|
||||
return encodedLength;
|
||||
}
|
||||
|
||||
int Base64Class::decode(char * output, char * input, int inputLength) {
|
||||
int i = 0, j = 0;
|
||||
int decodedLength = 0;
|
||||
unsigned char A3[3];
|
||||
unsigned char A4[4];
|
||||
|
||||
|
||||
while (inputLength--) {
|
||||
if(*input == '=') {
|
||||
break;
|
||||
}
|
||||
|
||||
A4[i++] = *(input++);
|
||||
if (i == 4) {
|
||||
for (i = 0; i <4; i++) {
|
||||
A4[i] = lookupTable(A4[i]);
|
||||
}
|
||||
|
||||
fromA4ToA3(A3,A4);
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
output[decodedLength++] = A3[i];
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 4; j++) {
|
||||
A4[j] = '\0';
|
||||
}
|
||||
|
||||
for (j = 0; j <4; j++) {
|
||||
A4[j] = lookupTable(A4[j]);
|
||||
}
|
||||
|
||||
fromA4ToA3(A3,A4);
|
||||
|
||||
for (j = 0; j < i - 1; j++) {
|
||||
output[decodedLength++] = A3[j];
|
||||
}
|
||||
}
|
||||
output[decodedLength] = '\0';
|
||||
return decodedLength;
|
||||
}
|
||||
|
||||
int Base64Class::encodedLength(int plainLength) {
|
||||
int n = plainLength;
|
||||
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
|
||||
}
|
||||
|
||||
int Base64Class::decodedLength(char * input, int inputLength) {
|
||||
int i = 0;
|
||||
int numEq = 0;
|
||||
for(i = inputLength - 1; input[i] == '='; i--) {
|
||||
numEq++;
|
||||
}
|
||||
|
||||
return ((6 * inputLength) / 8) - numEq;
|
||||
}
|
||||
|
||||
//Private utility functions
|
||||
inline void Base64Class::fromA3ToA4(unsigned char * A4, unsigned char * A3) {
|
||||
A4[0] = (A3[0] & 0xfc) >> 2;
|
||||
A4[1] = ((A3[0] & 0x03) << 4) + ((A3[1] & 0xf0) >> 4);
|
||||
A4[2] = ((A3[1] & 0x0f) << 2) + ((A3[2] & 0xc0) >> 6);
|
||||
A4[3] = (A3[2] & 0x3f);
|
||||
}
|
||||
|
||||
inline void Base64Class::fromA4ToA3(unsigned char * A3, unsigned char * A4) {
|
||||
A3[0] = (A4[0] << 2) + ((A4[1] & 0x30) >> 4);
|
||||
A3[1] = ((A4[1] & 0xf) << 4) + ((A4[2] & 0x3c) >> 2);
|
||||
A3[2] = ((A4[2] & 0x3) << 6) + A4[3];
|
||||
}
|
||||
|
||||
inline unsigned char Base64Class::lookupTable(char c) {
|
||||
if(c >='A' && c <='Z') return c - 'A';
|
||||
if(c >='a' && c <='z') return c - 71;
|
||||
if(c >='0' && c <='9') return c + 4;
|
||||
if(c == '+') return 62;
|
||||
if(c == '/') return 63;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Base64Class Base64;
|
||||
217
src/wrapper/littlefs_wrapper.cpp
Normal file
217
src/wrapper/littlefs_wrapper.cpp
Normal file
@@ -0,0 +1,217 @@
|
||||
|
||||
// ****************************************************************
|
||||
// Sketch Esp8266 Filesystem Manager spezifisch sortiert Modular(Tab)
|
||||
// created: Jens Fleischer, 2020-06-08
|
||||
// last mod: Jens Fleischer, 2020-12-19
|
||||
// For more information visit: https://fipsok.de
|
||||
// ****************************************************************
|
||||
// Hardware: Esp8266
|
||||
// Software: Esp8266 Arduino Core 2.7.0 - 3.0.2
|
||||
// Geprueft: von 1MB bis 2MB Flash
|
||||
// Getestet auf: Nodemcu
|
||||
/******************************************************************
|
||||
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 <ESP8266WebServer.h> muessen im Haupttab aufgerufen werden
|
||||
// Die Funktionalitaet des ESP8266 Webservers ist erforderlich.
|
||||
// "webserver.onNotFound()" darf nicht im Setup des ESP8266 Webserver stehen.
|
||||
// Die Funktion "setup_filesystem();" muss im Setup aufgerufen werden.
|
||||
/**************************************************************************************/
|
||||
#include <Arduino.h>
|
||||
#include "littlefs_wrapper.h"
|
||||
#include "LittleFS.h"
|
||||
#include <list>
|
||||
#include <tuple>
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
extern ESP8266WebServer webserver;
|
||||
|
||||
const char WARNING[] PROGMEM = R"(<h2>Der Sketch wurde mit "FS:none" kompiliert!)";
|
||||
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 fs.html hoch.)";
|
||||
|
||||
void setup_filesystem()
|
||||
{ // Funktionsaufruf "setup_filesystem();" muss im Setup eingebunden werden
|
||||
LittleFS.begin();
|
||||
webserver.on("/format", format_filesystem);
|
||||
webserver.on("/upload", HTTP_POST, send_response, handle_upload);
|
||||
webserver.onNotFound([]()
|
||||
{
|
||||
if (!handle_file(webserver.urlDecode(webserver.uri())))
|
||||
webserver.send(404, "text/plain", "FileNotFound"); });
|
||||
}
|
||||
|
||||
bool handle_list()
|
||||
{ // Senden aller Daten an den Client
|
||||
FSInfo fs_info;
|
||||
LittleFS.info(fs_info); // Fuellt FSInfo Struktur mit Informationen ueber das Dateisystem
|
||||
Dir dir = LittleFS.openDir("/");
|
||||
using namespace std;
|
||||
using records = tuple<String, String, int>;
|
||||
list<records> dirList;
|
||||
while (dir.next()) // Ordner und Dateien zur Liste hinzufuegen
|
||||
{
|
||||
if (dir.isDirectory())
|
||||
{
|
||||
uint8_t ran{0};
|
||||
Dir fold = LittleFS.openDir(dir.fileName());
|
||||
while (fold.next())
|
||||
{
|
||||
ran++;
|
||||
dirList.emplace_back(dir.fileName(), fold.fileName(), fold.fileSize());
|
||||
}
|
||||
if (!ran)
|
||||
dirList.emplace_back(dir.fileName(), "", 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
dirList.emplace_back("", dir.fileName(), dir.fileSize());
|
||||
}
|
||||
}
|
||||
dirList.sort([](const records &f, const records &l) { // Dateien sortieren
|
||||
if (webserver.arg(0) == "1")
|
||||
{
|
||||
return get<2>(f) > get<2>(l);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (uint8_t i = 0; i < 31; i++)
|
||||
{
|
||||
if (tolower(get<1>(f)[i]) < tolower(get<1>(l)[i]))
|
||||
return true;
|
||||
else if (tolower(get<1>(f)[i]) > tolower(get<1>(l)[i]))
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
dirList.sort([](const records &f, const records &l) { // Ordner sortieren
|
||||
if (get<0>(f)[0] != 0x00 || get<0>(l)[0] != 0x00)
|
||||
{
|
||||
for (uint8_t i = 0; i < 31; i++)
|
||||
{
|
||||
if (tolower(get<0>(f)[i]) < tolower(get<0>(l)[i]))
|
||||
return true;
|
||||
else if (tolower(get<0>(f)[i]) > tolower(get<0>(l)[i]))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
String temp = "[";
|
||||
for (auto &t : dirList)
|
||||
{
|
||||
if (temp != "[")
|
||||
temp += ',';
|
||||
temp += "{\"folder\":\"" + get<0>(t) + "\",\"name\":\"" + get<1>(t) + "\",\"size\":\"" + format_bytes(get<2>(t)) + "\"}";
|
||||
}
|
||||
temp += ",{\"usedBytes\":\"" + format_bytes(fs_info.usedBytes) + // Berechnet den verwendeten Speicherplatz
|
||||
"\",\"totalBytes\":\"" + format_bytes(fs_info.totalBytes) + // Zeigt die Groeße des Speichers
|
||||
"\",\"freeBytes\":\"" + (fs_info.totalBytes - fs_info.usedBytes) + "\"}]"; // Berechnet den freien Speicherplatz
|
||||
webserver.send(200, "application/json", temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
void delete_recursive(const String &path)
|
||||
{
|
||||
if (LittleFS.remove(path))
|
||||
{
|
||||
LittleFS.open(path.substring(0, path.lastIndexOf('/')) + "/", "w");
|
||||
return;
|
||||
}
|
||||
Dir dir = LittleFS.openDir(path);
|
||||
while (dir.next())
|
||||
{
|
||||
delete_recursive(path + '/' + dir.fileName());
|
||||
}
|
||||
LittleFS.rmdir(path);
|
||||
}
|
||||
|
||||
bool handle_file(String &&path)
|
||||
{
|
||||
if (webserver.hasArg("new"))
|
||||
{
|
||||
String folderName{webserver.arg("new")};
|
||||
for (auto &c : {34, 37, 38, 47, 58, 59, 92})
|
||||
for (auto &e : folderName)
|
||||
if (e == c)
|
||||
{
|
||||
e = 95; // Ersetzen der nicht erlaubten Zeichen
|
||||
}
|
||||
LittleFS.mkdir(folderName);
|
||||
}
|
||||
if (webserver.hasArg("sort"))
|
||||
{
|
||||
return handle_list();
|
||||
}
|
||||
if (webserver.hasArg("delete"))
|
||||
{
|
||||
delete_recursive(webserver.arg("delete"));
|
||||
send_response();
|
||||
return true;
|
||||
}
|
||||
if (!LittleFS.exists("fs.html"))
|
||||
webserver.send(200, "text/html", LittleFS.begin() ? HELPER : WARNING); // ermoeglicht das hochladen der fs.html
|
||||
if (path.endsWith("/"))
|
||||
{
|
||||
path += "index.html";
|
||||
}
|
||||
if (path == "/spiffs.html")
|
||||
{
|
||||
send_response(); // Vorruebergehend fuer den Admin Tab
|
||||
}
|
||||
return LittleFS.exists(path) ? ({File f = LittleFS.open(path, "r"); webserver.streamFile(f, mime::getContentType(path)); f.close(); true; }) : false;
|
||||
}
|
||||
|
||||
void handle_upload() // Dateien ins Filesystem schreiben
|
||||
{
|
||||
static File fsUploadFile;
|
||||
HTTPUpload &upload = webserver.upload();
|
||||
if (upload.status == UPLOAD_FILE_START)
|
||||
{
|
||||
if (upload.filename.length() > 31)
|
||||
{ // Dateinamen kuerzen
|
||||
upload.filename = upload.filename.substring(upload.filename.length() - 31, upload.filename.length());
|
||||
}
|
||||
printf(PSTR("handleFileUpload Name: /%s\n"), upload.filename.c_str());
|
||||
fsUploadFile = LittleFS.open(webserver.arg(0) + "/" + webserver.urlDecode(upload.filename), "w");
|
||||
}
|
||||
else if (upload.status == UPLOAD_FILE_WRITE)
|
||||
{
|
||||
printf(PSTR("handleFileUpload Data: %u\n"), upload.currentSize);
|
||||
fsUploadFile.write(upload.buf, upload.currentSize);
|
||||
}
|
||||
else if (upload.status == UPLOAD_FILE_END)
|
||||
{
|
||||
printf(PSTR("handleFileUpload Size: %u\n"), upload.totalSize);
|
||||
fsUploadFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
void format_filesystem() // Formatiert das Filesystem
|
||||
{
|
||||
LittleFS.format();
|
||||
send_response();
|
||||
}
|
||||
|
||||
void send_response()
|
||||
{
|
||||
webserver.sendHeader("Location", "fs.html");
|
||||
webserver.send(303, "message/http");
|
||||
}
|
||||
|
||||
const String format_bytes(size_t const &bytes)
|
||||
{ // lesbare Anzeige der Speichergroeß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";
|
||||
}
|
||||
Reference in New Issue
Block a user