-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebHandlers.cpp
More file actions
53 lines (43 loc) · 1.44 KB
/
WebHandlers.cpp
File metadata and controls
53 lines (43 loc) · 1.44 KB
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
#include "WebHandlers.h"
String getPhotoList() {
String photoList;
File root = SD_MMC.open("/");
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
// Skip directories
} else {
photoList += "<li><a href='/view?photo=" + String(file.name()) + "'>" + String(file.name()) + "</a></li>";
}
file = root.openNextFile();
}
return photoList;
}
void handlePhotoRequest(AsyncWebServerRequest *request) {
if (request->hasParam("photo")) {
String photoName = request->getParam("photo")->value();
if (!photoName.isEmpty()) {
String photoPath = "/" + photoName;
if (SD_MMC.exists(photoPath)) {
request->send(SD_MMC, photoPath, "image/jpeg", false);
} else {
request->send(404, "text/plain", "File not found");
}
} else {
request->send(400, "text/plain", "Empty 'photo' parameter");
}
} else {
String photoList = getPhotoList();
String html = "<html><body><h1>Photo List</h1><ul>" + photoList + "</ul></body></html>";
request->send(200, "text/html", html);
}
}
void handleStreamRequest(AsyncWebServerRequest *request) {
AsyncJpegStreamResponse *response = new AsyncJpegStreamResponse();
if (!response) {
request->send(501);
return;
}
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
}