123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- void handleFileUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
-
- if (IsUserAuthed(request)) {
- String logmessage = "";
-
-
-
- filename = trimFilename(filename);
-
- String dirToStore = GetPara(request, "dir");
- if (!dirToStore.startsWith("/")) {
- dirToStore = "/" + dirToStore;
- }
- if (!dirToStore.endsWith("/")) {
- dirToStore = dirToStore + "/";
- }
- dirToStore = "/www" + dirToStore;
- if (!index) {
- Serial.println("Selected Upload Dir: " + dirToStore);
- logmessage = "Upload Start: " + String(filename) + " by " + request->client()->remoteIP().toString();
-
- if (!SD.exists(dirToStore)) {
- SD.mkdir(dirToStore);
- }
-
- if (SD.exists(dirToStore + filename)) {
- SD.remove(dirToStore + filename);
- }
- request->_tempFile = SD.open(dirToStore + filename, FILE_WRITE);
- Serial.println(logmessage);
- }
- if (len) {
-
- request->_tempFile.write(data, len);
-
-
- }
- if (final) {
- logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
-
- request->_tempFile.close();
- Serial.println(logmessage);
-
- if (!SD.exists(String(dirToStore + filename))) {
-
- SendErrorResp(request, "Write failed for " + String(filename) + ". Try a shorter name!");
- return;
- }
- request->send(200, "application/json", "ok");
- }
- } else {
- Serial.println("Auth: Failed");
- SendErrorResp(request, "unauthorized");
- }
- }
- uint8_t getUtf8CharLength(const uint8_t firstByte) {
- if ((firstByte & 0x80) == 0) {
-
- return 1;
- } else if ((firstByte & 0xE0) == 0xC0) {
-
- return 2;
- } else if ((firstByte & 0xF0) == 0xE0) {
-
- return 3;
- } else if ((firstByte & 0xF8) == 0xF0) {
-
- return 4;
- } else {
-
- return 0;
- }
- }
- String filterBrokenUtf8(const String& input) {
- String result;
- size_t inputLength = input.length();
- size_t i = 0;
- while (i < inputLength) {
- uint8_t firstByte = input[i];
- uint8_t charLength = getUtf8CharLength(firstByte);
- if (charLength == 0){
-
- break;
- }
-
-
- if (i + charLength <= inputLength) {
-
- for (size_t j = 0; j < charLength; j++) {
- result += input[i];
- i++;
- }
- }else{
-
- break;
- }
- }
- return result;
- }
- String trimFilename(String& filename) {
-
- filename.replace("#","");
- filename.replace("?","");
- filename.replace("&","");
-
-
- int dotIndex = filename.lastIndexOf('.');
-
- if (dotIndex > 0 && dotIndex < filename.length() - 1) {
-
- int maxLength = 32 - (filename.length() - dotIndex - 1);
-
- if (filename.length() > maxLength) {
- String trimmedFilename = filterBrokenUtf8(filename.substring(0, maxLength)) + filename.substring(dotIndex);
- return trimmedFilename;
- }
- }
-
- return filename;
- }
|