123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- void prettyPrintRequest(AsyncWebServerRequest * request) {
- if (request->method() == HTTP_GET)
- Serial.printf("GET");
- else if (request->method() == HTTP_POST)
- Serial.printf("POST");
- else if (request->method() == HTTP_DELETE)
- Serial.printf("DELETE");
- else if (request->method() == HTTP_PUT)
- Serial.printf("PUT");
- else if (request->method() == HTTP_PATCH)
- Serial.printf("PATCH");
- else if (request->method() == HTTP_HEAD)
- Serial.printf("HEAD");
- else if (request->method() == HTTP_OPTIONS)
- Serial.printf("OPTIONS");
- else
- Serial.printf("UNKNOWN");
- Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
- if (request->contentLength()) {
- Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
- Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength());
- }
- int headers = request->headers();
- int i;
- for (i = 0; i < headers; i++) {
- AsyncWebHeader* h = request->getHeader(i);
- Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
- }
- int params = request->params();
- for (i = 0; i < params; i++) {
- AsyncWebParameter* p = request->getParam(i);
- if (p->isFile()) {
- Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
- } else if (p->isPost()) {
- Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
- } else {
- Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
- }
- }
- }
- bool IsDir(const String& path) {
- File file = SD.open(path);
- if (file && !file.isDirectory()) {
- file.close();
- return false;
- }
- file.close();
- return true;
- }
- String GeneratedRandomHex() {
- String hexString = "";
- for (int i = 0; i < 8; i++) {
- byte randomValue = random(256);
- hexString += String(randomValue, HEX);
- }
- return hexString;
- }
- unsigned long getTime() {
- unsigned long now = timeClient.getEpochTime();
- return now;
- }
- String getUTCTimeString(time_t unixTimestamp) {
- char utcString[30];
- const char* weekdayNames[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
- const char* monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- tm* timeInfo = gmtime(&unixTimestamp);
- sprintf(utcString, "%s, %02d %s %04d %02d:%02d:%02d GMT",
- weekdayNames[timeInfo->tm_wday], timeInfo->tm_mday, monthNames[timeInfo->tm_mon],
- timeInfo->tm_year + 1900, timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
- return String(utcString);
- }
- String GetCookieValueByKey(AsyncWebServerRequest *request, const String& key) {
-
- if (!request->hasHeader("Cookie")) {
- return "";
- }
-
- String targetValue = "";
- AsyncWebHeader* h = request->getHeader("Cookie");
- String cookieHeader = String(h->value().c_str());
-
- int startIndex = cookieHeader.indexOf(key + "=");
- if (startIndex != -1) {
- startIndex += 9;
- int endIndex = cookieHeader.indexOf(';', startIndex);
- if (endIndex == -1) {
- endIndex = cookieHeader.length();
- }
-
- targetValue = cookieHeader.substring(startIndex, endIndex);
- }
- return targetValue;
- }
- String basename(const String& filePath) {
- int lastSlashIndex = filePath.lastIndexOf('/');
-
- if (lastSlashIndex == -1) {
- return filePath;
- }
-
- return filePath.substring(lastSlashIndex + 1);
- }
- bool recursiveDirRemove(const String& path) {
- Serial.println(path);
- File directory = SD.open(path);
- if (!directory) {
- Serial.println("Error opening directory!");
- return false;
- }
-
- directory.rewindDirectory();
- while (true) {
- File entry = directory.openNextFile();
- if (!entry) {
-
- break;
- }
- String filename = String(entry.name());
- if (entry.isDirectory()) {
-
- recursiveDirRemove(path + filename);
- } else {
-
- entry.close();
- Serial.println("Removing " + path + filename);
- SD.remove(path + filename);
- }
- }
-
- directory.close();
-
- if (!SD.rmdir(path)) {
- Serial.println("Error deleting directory!");
- return false;
- }
- return true;
- }
- void analyzeDirectory(const String& path, uint32_t& totalSize, uint16_t& fileCount, uint16_t& folderCount) {
- File directory = SD.open(path);
- if (!directory) {
- Serial.println("Error opening directory!");
- return;
- }
-
- directory.rewindDirectory();
- while (true) {
- File entry = directory.openNextFile();
- if (!entry) {
-
- break;
- }
- if (entry.isDirectory()) {
-
- folderCount++;
- analyzeDirectory(entry.name(), totalSize, fileCount, folderCount);
- } else {
-
- fileCount++;
- totalSize += entry.size();
- }
- entry.close();
- }
-
- directory.close();
- }
- void scanSDCardForKeyword(const String& directoryPath, const String& keyword, int *matchCounter, AsyncResponseStream *response) {
- File directory = SD.open(directoryPath);
- if (!directory) {
- Serial.println("Error opening directory " + directoryPath);
- return;
- }
-
- directory.rewindDirectory();
- while (true) {
- File entry = directory.openNextFile();
- if (!entry) {
-
- break;
- }
- if (entry.isDirectory()) {
-
- scanSDCardForKeyword((directoryPath + entry.name() + "/"), keyword, matchCounter, response);
- } else {
-
- String filename = basename(entry.name());
- if (filename.indexOf(keyword) != -1) {
- if ((*matchCounter) > 0){
-
- response->print(",");
- }
-
-
- response->print("\"" + directoryPath.substring(4) + entry.name() + "\"");
- (*matchCounter)++;
- }
- }
- entry.close();
- }
-
- directory.close();
- }
|