forked from HerrRiebmann/Caravan_Leveler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebFile.ino
87 lines (76 loc) · 2.42 KB
/
WebFile.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
String getContentType(String filename) { // convert the file extension to the MIME type
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(".ico"))
return "image/x-icon";
else if (filename.endsWith(".png"))
return "image/png";
else if (filename.endsWith(".jpg"))
return "image/jpg";
else if (filename.endsWith(".gz"))
return "application/x-gzip";
return "text/plain";
}
void handleFileRead() {
String path = webServer.uri();
Serial.println("Handle FileRead: " + path);
// If a folder is requested, send index.html
if (path.endsWith("/"))
path.concat("index.html");
// If request is captive request, followed with a GUID
if(path.startsWith("/generate_204")){
redirect();
return;
}
if (SPIFFS.exists(path)) {
File file = SPIFFS.open(path, "r");
String fileSize = String(file.size());
//Check File "Version" (Size) is still the same, otherwise sumbit it
if (ProcessETag(fileSize.c_str())) {
file.close();
return;
}
size_t sent = webServer.streamFile(file, getContentType(path));
file.close();
Serial.print(String("\tSent file: ") + path);
Serial.println(" " + String(sent));
return;
}
handleNotFound();
Serial.println(String("\tFile Not Found: ") + path);
}
File fsUploadFile;
void handle_fileupload() {
HTTPUpload& upload = webServer.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/"))
filename = "/" + filename;
Serial.print("handleFileUpload: ");
Serial.println(filename);
if (SPIFFS.exists(filename)) {
Serial.println(F("\tFile Deleted"));
SPIFFS.remove(filename);
}
fsUploadFile = SPIFFS.open(filename, "w");
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile) {
fsUploadFile.close();
Serial.print("handleFileUpload Size: ");
Serial.println(upload.totalSize);
// Redirect the client to the root page
webServer.sendHeader("Location", "/");
webServer.send(303);
} else {
webServer.send(500, "text/plain", "500: couldn't create file");
}
}
}