forked from bracci/Qlockwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LittleFS.ino
629 lines (591 loc) · 18.6 KB
/
LittleFS.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include <list>
void setupFS() {
initFS();
webServer.on("/fs", handleFSExplorer);
webServer.on(F("/fsstyle.css"),handleFSExplorerCSS);
webServer.on(F("/sanduhr"), []() {handleContent(p_bluespinner,sizeof(p_bluespinner),IMAGE_GIF);});
webServer.on(F("/sunrise"), []() {handleContent(p_sunrise,sizeof(p_sunrise),IMAGE_PNG);});
webServer.on(F("/sunset"), []() {handleContent(p_sunset,sizeof(p_sunset),IMAGE_PNG);});
webServer.on(F("/format"), formatFS);
webServer.on(F("/upload"), HTTP_POST, sendResponce, handleUpload);
webServer.onNotFound([]() {
if (!handleFile(webServer.urlDecode(webServer.uri())))
webServer.send(404, TEXT_PLAIN, F("FileNotFound"));
});
}
void initFS() {
if ( !LittleFS.begin() )
{
Serial.println(F("LittleFS Mount fehlgeschlagen"));
if ( !LittleFS.format() )
{
Serial.println(F("Formatierung nicht möglich"));
}
else
{
Serial.println(F("Formatierung erfolgreich"));
if ( !LittleFS.begin() )
{
Serial.println(F("LittleFS Mount trotzdem fehlgeschlagen"));
}
else
{
Serial.println(F("LittleFS Dateisystems erfolgreich gemounted!"));
}
}
}
else
{
Serial.println(F("LittleFS erfolgreich gemounted!"));
}
if ( !LittleFS.exists("/web"))
{
if ( LittleFS.mkdir("/web") )
{
Serial.println(F("Verzeichnis /web erstellt"));
}
else
{
Serial.println(F("Verzeichnis /web konnte nicht erstellt werden"));
}
}
}
bool handleList() { // Senden aller Daten an den Client
FSInfo fs_info; LittleFS.info(fs_info); // Füllt FSInfo Struktur mit Informationen über das Dateisystem
Dir dir = LittleFS.openDir("/");
#ifdef DEBUG_WEB
Serial.printf("LittleFS.handleList: %i Codezeile: %u\n", ESP.getMaxFreeBlockSize(), __LINE__);
#endif
using namespace std;
using records = tuple<String, String, int>;
list<records> dirList;
while (dir.next()) { // Ordner und Dateien zur Liste hinzufügen
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());
}
}
#ifdef DEBUG_WEB
Serial.printf("LittleFS.handleList: %i Codezeile: %u\n", ESP.getMaxFreeBlockSize(), __LINE__);
#endif
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;
});
#ifdef DEBUG_WEB
Serial.printf("LittleFS: %i Codezeile: %u\n", ESP.getMaxFreeBlockSize(), __LINE__);
#endif
webServer.setContentLength(CONTENT_LENGTH_UNKNOWN);
uint8_t jsonoutteil = 0;
bool jsonoutfirst = true;
String temp = F("[");
for (auto& t : dirList) {
if ( ESP.getMaxFreeBlockSize() < 1000) delay(10);
if (temp != "[") temp += ',';
temp += F("{\"folder\":\"");
temp += get<0>(t);
temp += F("\",\"name\":\"");
temp += get<1>(t);
temp += F("\",\"size\":\"");
temp += formatBytes(get<2>(t));
temp += F("\"}");
jsonoutteil++;
if ( jsonoutteil > 2 )
{
jsonoutteil = 0;
if ( jsonoutfirst )
{
jsonoutfirst = false;
webServer.send(200, F("application/json"), temp);
}
else
{
webServer.sendContent(temp);
}
if ( ESP.getMaxFreeBlockSize() < minFreeBlockSize )
{
minFreeBlockSize = ESP.getMaxFreeBlockSize();
codeline = __LINE__;
codetab = __NAME__;
#ifdef DEBUG_WEB
Serial.printf("minFreeBlockSize: %i Tab: %s Codezeile: %u\n", minFreeBlockSize, codetab.c_str(), codeline);
#endif
}
#ifdef DEBUG_WEB
Serial.printf("LittleFS.handleList.MaxFreeBlockSize: %i Codezeile: %u\n", ESP.getMaxFreeBlockSize(), __LINE__);
Serial.println(temp);
#endif
temp = "";
delay(0);
}
}
temp += F(",{\"usedBytes\":\"");
temp += formatBytes(fs_info.usedBytes); // Berechnet den verwendeten Speicherplatz
temp += F("\",\"totalBytes\":\"");
temp += formatBytes(fs_info.totalBytes); // Zeigt die Größe des Speichers
temp += F("\",\"freeBytes\":\"");
temp += (fs_info.totalBytes - fs_info.usedBytes); // Berechnet den freien Speicherplatz
temp += F("\"}]");
webServer.sendContent(temp);
webServer.sendContent("");
#ifdef DEBUG_WEB
Serial.println(temp);
#endif
temp = "";
return true;
}
void deleteRecursive(const String &path) {
if (LittleFS.remove(path)) {
LittleFS.open(path.substring(0, path.lastIndexOf('/')) + "/", "w");
return;
}
Dir dir = LittleFS.openDir(path);
while (dir.next()) {
deleteRecursive(path + '/' + dir.fileName());
}
LittleFS.rmdir(path);
}
bool handleFile(String &&path) {
#ifdef DEBUG_WEB
Serial.print (F("LittleFS handleFile: "));
Serial.println(path);
#endif
if (path.endsWith("/")) path += F("index.html");
if (path.endsWith(F("player"))) path=F("/web/spieler.html");
if (path.endsWith(F("spieler"))) path=F("/web/spieler.html");
if (path.endsWith(F("favicon.ico"))) path=F("/web/favicon.ico");
if ( LittleFS.exists(path) )
{
File f = LittleFS.open(path, "r");
if(!f)
{
Serial.print(F("Fehler beim lesen des Files: "));
Serial.println(path);
return false;
}
else
{
#ifdef DEBUG_WEB
Serial.println(mime::getContentType(path));
#endif
webServer.streamFile(f, mime::getContentType(path));
f.close();
return true;
}
}
else
{
return false;
}
}
void handleUpload() { // Dateien ins Filesystem schreiben
static File fsUploadFile;
HTTPUpload& upload = webServer.upload();
if (upload.status == UPLOAD_FILE_START) {
if (upload.filename.length() > 31) { // Dateinamen kürzen
upload.filename = upload.filename.substring(upload.filename.length() - 31, upload.filename.length());
}
Serial.print(F("handleFileUpload Name: "));
Serial.println (upload.filename);
fsUploadFile = LittleFS.open(webServer.arg(0) + "/" + webServer.urlDecode(upload.filename), "w");
} else if (upload.status == UPLOAD_FILE_WRITE) {
Serial.print(F("handleFileUpload Data: "));
Serial.println (upload.currentSize);
fsUploadFile.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
Serial.print(F("handleFileUpload Size: "));
Serial.println (upload.totalSize);
fsUploadFile.close();
}
}
void formatFS() { // Formatiert das Filesystem
LittleFS.format();
initFS();
sendResponce();
}
void sendResponce() {
webServer.sendHeader("Location", "fs");
webServer.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";
}
// #####################################################################################################################
// FS Webseite
// Teil 1
const char fshtml1[] PROGMEM = R"=====(
document.addEventListener('DOMContentLoaded', () => {
list(JSON.parse(localStorage.getItem('sortBy')));
btn.addEventListener('click', () => {
if (!confirm("Wirklich formatieren? Alle Daten gehen verloren." + "\n" )) event.preventDefault();
});
});
function list(to){
let myList = document.querySelector('main'), noted = '';
fetch(`?sort=${to}`).then( (response) => {
return response.json();
}).then((json) => {
myList.innerHTML = '<nav><input type="radio" id="/" name="group" checked="checked"><label for="/"> 📁</label><span id="cr">+📁</nav></span><span id="si"></span>';
document.querySelector('form').setAttribute('action', '/upload?f=');
for (var i = 0; i < json.length - 1; i++) {
let dir = '', f = json[i].folder, n = json[i].name;
if (f != noted) {
noted = f;
dir = `<nav><input type="radio" id="${f}" name="group"><label for="${f}"></label> 📁 ${f} <a href="?delete=/${f}">🗑️</a></nav>`;
}
if (n != '') dir += `<li><a href="${f}/${n}">${n}</a><small> ${json[i].size}</small><a href="${f}/${n}"download="${n}"> Download</a> or<a href="?delete=${f}/${n}"> Delete</a>`;
myList.insertAdjacentHTML('beforeend', dir);
}
myList.insertAdjacentHTML('beforeend', `<li><b id="so">${to ? '▼' : '▲'} LittleFS</b><small> ` + "belegt" + `: ${json[i].usedBytes.replace(".00", "")}/` + "gesamt" + `: ${json[i].totalBytes.replace(".00", "")}</small>`);
var free = json[i].freeBytes;
cr.addEventListener('click', () => {
document.getElementById('no').classList.toggle('no');
});
so.addEventListener('click', () => {
list(to=++to%2);
localStorage.setItem('sortBy', JSON.stringify(to));
});
document.addEventListener('change', (e) => {
if (e.target.id == 'fs') {
for (var bytes = 0, i = 0; i < event.target.files.length; i++) bytes += event.target.files[i].size;
for (var output = `${bytes} Byte`, i = 0, circa = bytes / 1024; circa > 1; circa /= 1024) output = circa.toFixed(2) + ['KB', 'MB', 'GB'][i++];
if (bytes > free) {
si.innerHTML = `<li><b> ${output}</b><strong> ` + "Ungenügend Speicher frei" + `</strong></li>`;
up.setAttribute('disabled', 'disabled');
}
else {
si.innerHTML = `<li><b>` + "Dateigröße" + `:</b> ${output}</li>`;
up.removeAttribute('disabled');
}
}
document.querySelectorAll(`input[type=radio]`).forEach(el => { if (el.checked) document.querySelector('form').setAttribute('action', '/upload?f=' + el.id)});
});
document.querySelectorAll('[href^="?delete=/"]').forEach(node => {
node.addEventListener('click', () => {
if (!confirm("sicher?")) event.preventDefault();
});
});
});
}
</script>
</head>
<body>
)=====";
// Teil 2
const char fshtml2[] PROGMEM = R"=====(
<form method="post" enctype="multipart/form-data">
<input id="fs" type="file" name="up[]" multiple>
<button id="up" disabled>Upload</button>
</form>
<div class="sanduhrcontainer" id="sanduhrcontainer" style="display: none">
<img class="sanduhr" id="sanduhr" style="width: 150px" src="/sanduhr" alt="Sanduhr">
</div>
<script>
const upbut = document.getElementById("up");
upbut.addEventListener("click", function() {
document.getElementById("sanduhrcontainer").style.display = "block";
});
</script>
<form id="no" class="no" method="POST">
)=====";
//################################################################
// FS Webseite CSS
// Teil 1
const char fsstyle1[] PROGMEM = R"=====(
body {
font-family: sans-serif;
background-color: #aaaaaa;
display: flex;
max-width : 500px;
flex-flow: column;
align-items: center;
}
h1,h2 {
color: #e1e1e1;
text-shadow: 2px 2px 2px black;
}
li {
background-color: #feb1e2;
list-style-type: none;
margin-bottom: 10px;
padding: 2px 5px 1px 0;
box-shadow: 5px 5px 5px rgba(0,0,0,0.7);
}
li a:first-child, li b {
background-color: #8f05a5;
font-weight: bold;
color: white;
text-decoration:none;
padding: 2px 5px;
text-shadow: 2px 2px 1px black;
cursor:pointer;
}
li strong {
color: red;
}
input {
height:35px;
font-size:14px;
}
label + a {
text-decoration: none;
}
h1 + main {
display: flex;
}
aside {
display: flex;
flex-direction: column;
padding: 0.2em;
}
#left {
align-items:flex-end;
text-shadow: 0.5px 0.5px 1px #757474;
}
.note {
background-color: #fecdee;
padding: 0.5em;
margin-top: 1em;
text-align: center;
max-width: 320px;
border-radius: 0.5em;
}
[type=submit] {
height:40px;
font-size: 14px;
}
.buttons {
background-color: #353746;
text-align:center;
line-height: 22px;
color:#FFFFFF;
width:200px;
height: 32px;
padding:1px;
border:2px solid #FFFFFF;
font-size:14px;
border-radius:15px;
cursor: pointer;
}
form [title] {
background-color: #353746;
font-size: 14px;
width: 150px;
}
)=====";
// Teil 2
const char fsstyle2[] PROGMEM = R"=====(
nav {
display: flex;
align-items: baseline;
justify-content: space-between;
}
#left {
align-items:flex-end;
text-shadow: 0.5px 0.5px 1px #757474;
}
#cr {
font-weight: bold;
cursor:pointer;
font-size: 1.5em;
}
#up {
width: auto;
}
.note {
background-color: #fecdee;
padding: 0.5em;
margin-top: 1em;
text-align: center;
max-width: 320px;
border-radius: 0.5em;
}
.no {
display: none;
}
[value*=Format] {
margin-top: 1em;
box-shadow: 5px 5px 5px rgba(0,0,0,0.7);
}
[name="group"] {
display: none;
}
[name="group"] + label {
font-size: 1.5em;
margin-right: 5px;
}
[name="group"] + label::before {
content: "\002610";
}
[name="group"]:checked + label::before {
content: '\002611\0027A5';
}
.sanduhrcontainer {
position: absolute;
top: 130px;
left: 180px;
}
.sanduhr {
position: absolute;
margin: 0px auto;
}
)=====";
// #####################################################################################################################
void handleFSExplorer() {
String message;
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);
sendResponce();
}
else if (webServer.hasArg("sort"))
{
bool x=handleList();
}
else if (webServer.hasArg("delete"))
{
deleteRecursive(webServer.arg("delete"));
sendResponce();
}
else
{
webServer.setContentLength(CONTENT_LENGTH_UNKNOWN);
message = F("<!DOCTYPE HTML>");
message += F("<html lang=\"");
message += "de";
message += F("\">\n");
message += F("<head>");
message += F("<meta charset=\"UTF-8\">");
message += F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
message += F("<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"web/favicon.ico\">");
message += F("<link rel=\"stylesheet\" href=\"fsstyle.css\">");
message += F("<title>");
message += "Datei Manager";
message += F("</title>\n");
message += F("<script>");
message += F("const LANG_FORMATCONF = \"" "Wirklich formatieren? Alle Daten gehen verloren." "\";\n");
message += F("const LANG_NOSPACE = \"" "Ungenügend Speicher frei" "\";\n");
message += F("const LANG_FILESIZE= \"" "Dateigröße" "\";\n");
message += F("const LANG_SURE= \"" "sicher?" "\";\n");
message += F("const LANG_FSUSED= \"" "belegt" "\";\n");
message += F("const LANG_FSTOTAL= \"" "gesamt" "\";\n");
webServer.send(200, F(TEXT_HTML), message);
delay(0);
webServer.sendContent_P(fshtml1);
delay(0);
message = F("<h2>" "Datei Manager" "</h2>");
webServer.sendContent(message);
webServer.sendContent_P(fshtml2);
delay(0);
message = F("<input name=\"new\" placeholder=\"" "Ordner Name" "\"");
message += F(" pattern=\"[^\x22/%&\\:;]{1,31}\" title=\"");
message += F("Zeichen “ % & / : ; \\ sind nicht erlaubt.");
message += F("\" required=\"\">");
message += F("<button>Create</button>\n");
message += F("</form>");
message += F("<main></main>\n");
message += F("<form action=\"/format\" method=\"POST\">\n");
message += F("<button class=\"buttons\" title=\"" "zurück" "\" id=\"back\" type=\"button\" onclick=\"window.location.href=\'\\/\'\">🔙 " "zurück" "</button>\n");
message += F("<button class=\"buttons\" title=\"Format LittleFS\" id=\"btn\" type=\"submit\" value=\"Format LittleFS\">❌Format LittleFS</button>\n");
message += F("</form>\n");
message += F("</body>\n");
message += F("</html>\n");
webServer.sendContent(message);
message = "";
delay(0);
webServer.sendContent("");
delay(0);
}
}
void handleFSExplorerCSS() {
webServer.setContentLength(CONTENT_LENGTH_UNKNOWN);
delay(0);
webServer.send_P(200, TEXT_CSS, fsstyle1);
delay(0);
webServer.sendContent_P(fsstyle2);
delay(0);
webServer.sendContent("");
delay(0);
}
/*
void sendPcontent(const char * p_cont, size_t size, char* ctype)
{
const uint16_t maxbuff = 512;
uint16_t aktsize = 0;
uint16_t akt_index = 0;
uint16_t sz;
char tmp_buffer[maxbuff];
bool f = true;
webServer.setContentLength(size);
while ( akt_index < size)
{
for (sz = 0; sz < maxbuff; sz++)
{
if ( sz+akt_index < size)
{
tmp_buffer[sz] = pgm_read_byte(&p_cont[sz+akt_index]);
aktsize++;
}
}
akt_index = akt_index + maxbuff;
if ( f ) {
webServer.send(200, ctype, tmp_buffer,aktsize);
f = false;
}
else
{
webServer.sendContent(tmp_buffer,aktsize);
}
// Serial.printf("sendPcontent ESP.getMaxFreeBlockSize: %i Codezeile: %u\n", ESP.getMaxFreeBlockSize(), __LINE__);
delay(0);
aktsize = 0;
}
delay(0);
}
*/
void handleContent(const uint8_t * image, size_t size, const char * mime_type) {
uint8_t buffer[512];
size_t buffer_size = sizeof(buffer);
size_t sent_size = 0;
webServer.setContentLength(size);
webServer.send(200, mime_type, "");
// WiFiClient client = webServer.client();
while (sent_size < size) {
size_t chunk_size = min(buffer_size, size - sent_size);
memcpy_P(buffer, image + sent_size, chunk_size);
webServer.client().write(buffer, chunk_size);
sent_size += chunk_size;
delay(0);
#ifdef DEBUG_WEB
Serial.printf("sendContent: %i byte : %i byte of %i byte\n", chunk_size, sent_size,size );
#endif
}
webServer.sendContent("");
delay(0);
}