-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileStream.cpp
80 lines (72 loc) · 1.94 KB
/
FileStream.cpp
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
#include "FileStream.h"
/*
* @brief Class Constructor creating a new object
*/
FileStream::FileStream() {
this->server = new AsyncWebServer(80);
}
/*
* @brief Setup the FileStream
*/
bool FileStream::setup() {
if (SPIFFS.begin(true)) {
DEBUG_FS(F("SPIFFS Started"));
return true;
} else {
DEBUG_FS(F("SPIFFS Fail"));
return false;
}
} // END Function
/*
* @brief check if the file exists
* @param path: to the file
*/
bool FileStream::isFile(String path) {
if (SPIFFS.exists(path)) {
//DEBUG_FS(F("File Found"));
return true;
} else {
DEBUG_FS(F("File Missing"));
return false;
}
} // END Function
/*
* @brief Stream a file depending on it's extension
* @param path: to file gzip: compacted?
*/
bool FileStream::stream(AsyncWebServerRequest* request, String path, bool gzip) {
if (isFile(path)) {
File file = SPIFFS.open(path, "r");
} else {
DEBUG_FS(F("Fail to Server File"));
return false;
}
AsyncWebServerResponse* response;
if (path.endsWith(".js")) {
response = request->beginResponse(SPIFFS, path, "application/javascript");
} else if (path.endsWith(".css")) {
response = request->beginResponse(SPIFFS, path, "text/css");
} else if (path.endsWith(".html")) {
response = request->beginResponse(SPIFFS, path, "text/html");
} else if (path.endsWith(".png")) {
response = request->beginResponse(SPIFFS, path, "img/png");
} else if (path.endsWith(".jpg")) {
response = request->beginResponse(SPIFFS, path, "img/jpeg");
} else if (path.endsWith(".json")) {
response = request->beginResponse(SPIFFS, path, "application/json");
}
if (gzip)
response->addHeader("Content-Encoding", "gzip");
request->send(response);
return true;
} // END Function
/*
* @brief Debug template
*/
template <typename Generic>
void FileStream::DEBUG_FS(Generic text) {
if (_debug) {
Serial.print("[FileStream] : ");
Serial.println(text);
}
} // template DEBUG_FS