|
| 1 | +#include "HttpMethods.h" |
| 2 | + |
| 3 | +#include <ESP8266HTTPClient.h> |
| 4 | +#include <FS.h> |
| 5 | + |
| 6 | +#define DBG_OUTPUT_PORT Serial |
| 7 | + |
| 8 | +void listFiles() { |
| 9 | + Dir dir = SPIFFS.openDir("/"); |
| 10 | + while (dir.next()) { |
| 11 | + String fileName = dir.fileName(); |
| 12 | + size_t fileSize = dir.fileSize(); |
| 13 | + DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); |
| 14 | + } |
| 15 | + DBG_OUTPUT_PORT.printf("\n"); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +void httpRequest(char *url) { |
| 21 | + // Declare an object of class HTTPClient |
| 22 | + HTTPClient http; |
| 23 | + // Specify request destination |
| 24 | + http.begin(url); |
| 25 | + // Send the request |
| 26 | + int httpCode = http.GET(); |
| 27 | + // Check the returning code |
| 28 | + if (httpCode > 0) { |
| 29 | + // Get the request response payload |
| 30 | + String payload = http.getString(); |
| 31 | + } |
| 32 | + // Close connection |
| 33 | + http.end(); |
| 34 | +} |
| 35 | + |
| 36 | +void httpPost(char *url, char *msg) { |
| 37 | + HTTPClient http; //Declare object of class HTTPClient |
| 38 | + |
| 39 | + http.begin(url); //Specify request destination |
| 40 | + http.addHeader("Content-Type", "text/plain"); //Specify content-type header |
| 41 | + |
| 42 | + int httpCode = http.POST(msg); //Send the request |
| 43 | + String payload = http.getString(); //Get the response payload |
| 44 | + |
| 45 | + Serial.println(httpCode); //Print HTTP return code |
| 46 | + Serial.println(payload); //Print request response payload |
| 47 | + |
| 48 | + http.end(); //Close connection |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +//format bytes |
| 53 | +String formatBytes(size_t bytes) { |
| 54 | + if (bytes < 1024) { |
| 55 | + return String(bytes) + "B"; |
| 56 | + } else if (bytes < (1024 * 1024)) { |
| 57 | + return String(bytes / 1024.0) + "KB"; |
| 58 | + } else if (bytes < (1024 * 1024 * 1024)) { |
| 59 | + return String(bytes / 1024.0 / 1024.0) + "MB"; |
| 60 | + } else { |
| 61 | + return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +String getContentType(String filename) { |
| 66 | + if (server.hasArg("download")) { |
| 67 | + return "application/octet-stream"; |
| 68 | + } else if (filename.endsWith(".htm")) { |
| 69 | + return "text/html"; |
| 70 | + } else if (filename.endsWith(".html")) { |
| 71 | + return "text/html"; |
| 72 | + } else if (filename.endsWith(".css")) { |
| 73 | + return "text/css"; |
| 74 | + } else if (filename.endsWith(".js")) { |
| 75 | + return "application/javascript"; |
| 76 | + } else if (filename.endsWith(".png")) { |
| 77 | + return "image/png"; |
| 78 | + } else if (filename.endsWith(".gif")) { |
| 79 | + return "image/gif"; |
| 80 | + } else if (filename.endsWith(".jpg")) { |
| 81 | + return "image/jpeg"; |
| 82 | + } else if (filename.endsWith(".ico")) { |
| 83 | + return "image/x-icon"; |
| 84 | + } else if (filename.endsWith(".xml")) { |
| 85 | + return "text/xml"; |
| 86 | + } else if (filename.endsWith(".pdf")) { |
| 87 | + return "application/x-pdf"; |
| 88 | + } else if (filename.endsWith(".zip")) { |
| 89 | + return "application/x-zip"; |
| 90 | + } else if (filename.endsWith(".gz")) { |
| 91 | + return "application/x-gzip"; |
| 92 | + } |
| 93 | + return "text/plain"; |
| 94 | +} |
| 95 | + |
| 96 | +bool handleFileRead(String path) { |
| 97 | + DBG_OUTPUT_PORT.println("handleFileRead: " + path); |
| 98 | + if (path.endsWith("/")) { |
| 99 | + path += "index.htm"; |
| 100 | + } |
| 101 | + String contentType = getContentType(path); |
| 102 | + String pathWithGz = path + ".gz"; |
| 103 | + if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { |
| 104 | + if (SPIFFS.exists(pathWithGz)) { |
| 105 | + path += ".gz"; |
| 106 | + } |
| 107 | + File file = SPIFFS.open(path, "r"); |
| 108 | + server.streamFile(file, contentType); |
| 109 | + file.close(); |
| 110 | + return true; |
| 111 | + } |
| 112 | + return false; |
| 113 | +} |
| 114 | + |
| 115 | +//holds the current upload |
| 116 | +File fsUploadFile; |
| 117 | + |
| 118 | +void handleFileUpload() { |
| 119 | + if (server.uri() != "/edit") { |
| 120 | + return; |
| 121 | + } |
| 122 | + HTTPUpload& upload = server.upload(); |
| 123 | + if (upload.status == UPLOAD_FILE_START) { |
| 124 | + String filename = upload.filename; |
| 125 | + if (!filename.startsWith("/")) { |
| 126 | + filename = "/" + filename; |
| 127 | + } |
| 128 | + DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename); |
| 129 | + fsUploadFile = SPIFFS.open(filename, "w"); |
| 130 | + filename = String(); |
| 131 | + } else if (upload.status == UPLOAD_FILE_WRITE) { |
| 132 | + //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize); |
| 133 | + if (fsUploadFile) { |
| 134 | + fsUploadFile.write(upload.buf, upload.currentSize); |
| 135 | + } |
| 136 | + } else if (upload.status == UPLOAD_FILE_END) { |
| 137 | + if (fsUploadFile) { |
| 138 | + fsUploadFile.close(); |
| 139 | + } |
| 140 | + DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +void handleFileDelete() { |
| 145 | + if (server.args() == 0) { |
| 146 | + return server.send(500, "text/plain", "BAD ARGS"); |
| 147 | + } |
| 148 | + String path = server.arg(0); |
| 149 | + DBG_OUTPUT_PORT.println("handleFileDelete: " + path); |
| 150 | + if (path == "/") { |
| 151 | + return server.send(500, "text/plain", "BAD PATH"); |
| 152 | + } |
| 153 | + if (!SPIFFS.exists(path)) { |
| 154 | + return server.send(404, "text/plain", "FileNotFound"); |
| 155 | + } |
| 156 | + SPIFFS.remove(path); |
| 157 | + server.send(200, "text/plain", ""); |
| 158 | + path = String(); |
| 159 | +} |
| 160 | + |
| 161 | +void handleFileCreate() { |
| 162 | + if (server.args() == 0) { |
| 163 | + return server.send(500, "text/plain", "BAD ARGS"); |
| 164 | + } |
| 165 | + String path = server.arg(0); |
| 166 | + DBG_OUTPUT_PORT.println("handleFileCreate: " + path); |
| 167 | + if (path == "/") { |
| 168 | + return server.send(500, "text/plain", "BAD PATH"); |
| 169 | + } |
| 170 | + if (SPIFFS.exists(path)) { |
| 171 | + return server.send(500, "text/plain", "FILE EXISTS"); |
| 172 | + } |
| 173 | + File file = SPIFFS.open(path, "w"); |
| 174 | + if (file) { |
| 175 | + file.close(); |
| 176 | + } else { |
| 177 | + return server.send(500, "text/plain", "CREATE FAILED"); |
| 178 | + } |
| 179 | + server.send(200, "text/plain", ""); |
| 180 | + path = String(); |
| 181 | +} |
| 182 | + |
| 183 | +void handleFileList() { |
| 184 | + if (!server.hasArg("dir")) { |
| 185 | + server.send(500, "text/plain", "BAD ARGS"); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + String path = server.arg("dir"); |
| 190 | + DBG_OUTPUT_PORT.println("handleFileList: " + path); |
| 191 | + Dir dir = SPIFFS.openDir(path); |
| 192 | + path = String(); |
| 193 | + |
| 194 | + String output = "["; |
| 195 | + while (dir.next()) { |
| 196 | + File entry = dir.openFile("r"); |
| 197 | + if (output != "[") { |
| 198 | + output += ','; |
| 199 | + } |
| 200 | + bool isDir = false; |
| 201 | + output += "{\"type\":\""; |
| 202 | + output += (isDir) ? "dir" : "file"; |
| 203 | + output += "\",\"name\":\""; |
| 204 | + output += String(entry.name()).substring(1); |
| 205 | + output += "\"}"; |
| 206 | + entry.close(); |
| 207 | + } |
| 208 | + |
| 209 | + output += "]"; |
| 210 | + server.send(200, "text/json", output); |
| 211 | +} |
| 212 | + |
| 213 | + |
0 commit comments