-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ino
116 lines (100 loc) · 3.06 KB
/
utils.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
#include "headers.h"
void setPinState(byte pin, byte state) {
digitalWrite(pin, !state);
}
byte getPinState(byte pin) {
return digitalRead(pin);
}
void SPIFFSInformation() {
Serial.println();
Dir dir = filesystem->openDir("/");
Serial.println("SPIFFS information:");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Serial.printf("%s [%s]\n", fileName.c_str(), formatBytes(fileSize).c_str());
fileName = String();
}
}
void ConnectionInformation() {
Serial.println();
Serial.print("Connected to '");
Serial.print(WiFi.SSID());
Serial.println("'");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin(HOSTNAME, WiFi.localIP())) {
Serial.println("mDNS responder started");
Serial.print("Open http://");
Serial.print(HOSTNAME);
Serial.println(".local in your browser to see it working");
} else {
Serial.println("Error setting up MDNS responder!");
}
}
void ServerInformation() {
Serial.println();
Serial.print("HTTP server started on ");
Serial.print(HTTP_PORT);
Serial.println(" port");
Serial.printf("Update server ready! Open http://%s.local%s in your browser.\n",HOSTNAME, UPDATE_PATH);
Serial.printf("Use username '%s' and password '%s' for auth\n", UPDATE_USERNAME, UPDATE_PASSWORD);
}
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
String getContentType(String filename) {
if (httpServer.hasArg("download")) {
return "application/octet-stream";
} 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(".svg")) {
return "image/svg+xml";
} 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";
}
return "text/plain";
}
bool handleFileRead(String path) {
if (path.endsWith("/")) {
path += "index.html";
}
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (filesystem->exists(pathWithGz) || filesystem->exists(path)) {
if (filesystem->exists(pathWithGz)) {
path += ".gz";
}
File file = filesystem->open(path, "r");
httpServer.streamFile(file, contentType);
file.close();
return true;
}
return false;
}