-
Notifications
You must be signed in to change notification settings - Fork 7
/
FSBrowser.ino
411 lines (378 loc) · 8.78 KB
/
FSBrowser.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
String unsupportedFiles = String();
// Utils to return HTTP codes, and determine content-type
void replyOK()
{
server.send(200, FPSTR("text/plain"), "");
}
void replyResponse(const char *msg)
{
server.send(200, FPSTR("text/plain"), msg);
}
void replyOKWithMsg(String msg)
{
server.send(200, FPSTR("text/plain"), msg);
}
void replyReboot(const char *msg)
{
server.send(205, FPSTR("text/plain"), msg);
}
void replyNotFound(String msg)
{
server.send(404, FPSTR("text/plain"), msg);
}
void replyBadRequest(String msg)
{
server.send(400, FPSTR("text/plain"), msg + "\r\n");
}
void replyServerError(String msg)
{
server.send(500, FPSTR("text/plain"), msg + "\r\n");
}
String getContentType(const String &filename)
{
if (server.hasArg("download"))
{
return "application/octet-stream";
}
else if (filename.endsWith(".htm"))
{
return "text/html";
}
else if (filename.endsWith(".html"))
{
return "text/html";
}
else if (filename.endsWith(".css"))
{
return "text/css";
}
else if (filename.endsWith(".js"))
{
return "application/javascript";
}
else if (filename.endsWith(".png"))
{
return "image/png";
}
else if (filename.endsWith(".gif"))
{
return "image/gif";
}
else if (filename.endsWith(".jpg"))
{
return "image/jpeg";
}
else if (filename.endsWith(".ico"))
{
return "image/x-icon";
}
else if (filename.endsWith(".xml"))
{
return "text/xml";
}
else if (filename.endsWith(".pdf"))
{
return "application/x-pdf";
}
else if (filename.endsWith(".zip"))
{
return "application/x-zip";
}
else if (filename.endsWith(".gz"))
{
return "application/x-gzip";
}
else if (filename.endsWith(".json"))
{
return "text/plain";
}
else if (filename.endsWith(".mp3"))
{
return "audio/mpeg3";
}
return "text/plain";
}
void handleStatus()
{
String json;
json.reserve(128);
json = "{\"type\":\"Filesystem\", \"isOk\":";
json += PSTR("\"true\", \"totalBytes\":\"");
#ifdef ESP32
json += LittleFS.totalBytes();
#elif ESP8266
FSInfo fs_info;
LittleFS.info(fs_info);
json += fs_info.totalBytes;
#endif
json += PSTR("\", \"usedBytes\":\"");
#ifdef ESP32
json += LittleFS.usedBytes();
#elif ESP8266
json += fs_info.usedBytes;
#endif
json += "\"";
json += PSTR(",\"unsupportedFiles\":\"\"}");
server.send(200, "application/json", json);
}
bool exists(String path)
{
File file = LittleFS.open(path, "r");
if (!file.isDirectory())
return true;
file.close();
return false;
}
#ifdef ESP32
void handleFileList()
{
if (!server.hasArg("dir"))
{
server.send(500, "text/plain", "BAD ARGS");
return;
}
String path = server.arg("dir");
File root = LittleFS.open(path);
path.clear();
String output = "[";
if (root.isDirectory())
{
File file = root.openNextFile();
while (file)
{
if (output != "[")
{
output += ',';
}
output += "{\"type\":\"";
if (file.isDirectory())
output += "dir";
else
{
output += F("file\",\"size\":\"");
output += file.size();
}
output += "\",\"name\":\"";
if (file.name()[0] == '/')
output += &(file.name()[1]);
else
output += file.name();
output += "\"}";
file = root.openNextFile();
}
}
output += "]";
server.send(200, "text/json", output);
}
#elif ESP8266
void handleFileList()
{
if (!server.hasArg("dir"))
{
return replyBadRequest(F("DIR ARG MISSING"));
}
String path = server.arg("dir");
if (path != "/" && LittleFS.mkdir(path)) // mkdir workarround für exists on directory
{
return replyBadRequest("BAD PATH");
}
Dir dir = LittleFS.openDir(path);
path.clear();
if (!server.chunkedResponseModeStart(200, "text/json"))
{
server.send(505, F("text/html"), F("HTTP1.1 required"));
return;
}
String output;
output.reserve(64);
while (dir.next())
{
if (output.length())
{
server.sendContent(output);
output = ',';
}
else
{
output = '[';
}
output += "{\"type\":\"";
if (dir.isDirectory())
{
output += "dir";
}
else
{
output += F("file\",\"size\":\"");
output += dir.fileSize();
}
output += F("\",\"name\":\"");
if (dir.fileName()[0] == '/')
{
output += &(dir.fileName()[1]);
}
else
{
output += dir.fileName();
}
output += "\"}";
}
output += "]";
server.sendContent(output);
server.chunkedResponseFinalize();
}
#endif
// Read the given file from the filesystem and stream it back to the client
bool handleFileRead(String path)
{
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (exists(pathWithGz) || exists(path))
{
if (exists(pathWithGz))
{
path += ".gz";
}
File file = LittleFS.open(path, "r");
server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
// As some FS (e.g. LittleFS) delete the parent folder when the last child has been removed, return the path of the closest parent still existing
String lastExistingParent(String path)
{
while (!path.isEmpty() && !LittleFS.exists(path))
{
if (path.lastIndexOf('/') > 0)
path = path.substring(0, path.lastIndexOf('/'));
else
path = String(); // No slash => the top folder does not exist
}
return path;
}
/*
Handle the creation/rename of a new file
Operation | req.responseText
---------------+--------------------------------------------------------------
Create file | parent of created file
Create folder | parent of created folder
Rename file | parent of source file
Move file | parent of source file, or remaining ancestor
Rename folder | parent of source folder
Move folder | parent of source folder, or remaining ancestor
*/
void handleFileCreate()
{
String path = server.arg("path");
if (path.isEmpty())
return replyBadRequest(F("PATH ARG MISSING"));
if (path == "/")
return replyBadRequest("BAD PATH");
if (LittleFS.exists(path))
return replyBadRequest(F("PATH FILE EXISTS"));
String src = server.arg("src");
if (src.isEmpty())
{
// No source specified: creation
if (path.endsWith("/"))
{
// Create a folder
path.remove(path.length() - 1);
if (!LittleFS.mkdir(path))
return replyServerError(F("MKDIR FAILED"));
}
else
{
// Create a file
File file = LittleFS.open(path, "w");
if (file)
file.close();
else
return replyServerError(F("CREATE FAILED"));
}
if (path.lastIndexOf('/') > -1)
path = path.substring(0, path.lastIndexOf('/'));
replyOKWithMsg(path);
}
else
{
// Source specified: rename
if (src == "/")
return replyBadRequest("BAD SRC");
if (!LittleFS.exists(src))
return replyBadRequest(F("SRC FILE NOT FOUND"));
if (path.endsWith("/"))
path.remove(path.length() - 1);
if (src.endsWith("/"))
src.remove(src.length() - 1);
if (!LittleFS.rename(src, path))
return replyServerError(F("RENAME FAILED"));
replyOKWithMsg(lastExistingParent(src));
}
}
void deleteRecursive(String path)
{
File file = LittleFS.open(path, "r");
bool isDir = file.isDirectory();
file.close();
if (!isDir)
{
LittleFS.remove(path);
return;
}
LittleFS.rmdir(path);
}
/*
Handle a file deletion request
Operation | req.responseText
---------------+--------------------------------------------------------------
Delete file | parent of deleted file, or remaining ancestor
Delete folder | parent of deleted folder, or remaining ancestor
*/
void handleFileDelete()
{
String path = server.arg(0);
if (path.isEmpty() || path == "/")
return replyBadRequest("BAD PATH");
if (!LittleFS.exists(path))
return replyNotFound(FPSTR("FileNotFound"));
deleteRecursive(path);
replyOKWithMsg(lastExistingParent(path));
}
// Handle a file upload request
void handleFileUpload()
{
if (server.uri() != "/edit")
return;
HTTPUpload &upload = server.upload();
if (upload.status == UPLOAD_FILE_START)
{
String filename = upload.filename;
if (!filename.startsWith("/"))
filename = "/" + filename;
fsUploadFile = LittleFS.open(filename, "w");
if (!fsUploadFile)
return replyServerError(F("CREATE FAILED"));
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (fsUploadFile)
{
size_t bytesWritten = fsUploadFile.write(upload.buf, upload.currentSize);
if (bytesWritten != upload.currentSize)
return replyServerError(F("WRITE FAILED"));
}
}
else if (upload.status == UPLOAD_FILE_END)
{
if (fsUploadFile)
fsUploadFile.close();
}
}
void handleGetEdit()
{
server.sendHeader(PSTR("Content-Encoding"), "gzip");
server.send_P(200, "text/html", edit_htm_gz, edit_htm_gz_len);
}