| 45 | } |
| 46 | |
| 47 | void WebDAVHandler::raw(WebServer& server, const String& uri, HTTPRaw& raw) { |
| 48 | (void)uri; |
| 49 | if (raw.status == RAW_START) { |
| 50 | _putPath = getRequestPath(server); |
| 51 | if (isProtectedPath(_putPath)) { |
| 52 | _putOk = false; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Ensure parent directory exists |
| 57 | int lastSlash = _putPath.lastIndexOf('/'); |
| 58 | if (lastSlash > 0) { |
| 59 | String parentPath = _putPath.substring(0, lastSlash); |
| 60 | if (!Storage.exists(parentPath.c_str())) { |
| 61 | _putOk = false; |
| 62 | return; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (_putFile) _putFile.close(); |
| 67 | _putExisted = Storage.exists(_putPath.c_str()); |
| 68 | |
| 69 | if (_putExisted) { |
| 70 | HalFile existing = Storage.open(_putPath.c_str()); |
| 71 | if (existing && existing.isDirectory()) { |
| 72 | existing.close(); |
| 73 | _putOk = false; |
| 74 | return; |
| 75 | } |
| 76 | if (existing) existing.close(); |
| 77 | } |
| 78 | |
| 79 | // Write to a temp file to avoid destroying the original on failed upload |
| 80 | String tempPath = _putPath + ".davtmp"; |
| 81 | Storage.remove(tempPath.c_str()); |
| 82 | _putOk = Storage.openFileForWrite("DAV", tempPath, _putFile); |
| 83 | LOG_DBG("DAV", "PUT START: %s", _putPath.c_str()); |
| 84 | |
| 85 | } else if (raw.status == RAW_WRITE) { |
| 86 | if (_putFile && _putOk) { |
| 87 | esp_task_wdt_reset(); |
| 88 | size_t written = _putFile.write(raw.buf, raw.currentSize); |
| 89 | if (written != raw.currentSize) { |
| 90 | _putOk = false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } else if (raw.status == RAW_END) { |
| 95 | if (_putFile) _putFile.close(); |
| 96 | if (_putOk) { |
| 97 | String tempPath = _putPath + ".davtmp"; |
| 98 | if (_putExisted) Storage.remove(_putPath.c_str()); |
| 99 | HalFile tmp = Storage.open(tempPath.c_str()); |
| 100 | if (tmp) { |
| 101 | _putOk = tmp.rename(_putPath.c_str()); |
| 102 | tmp.close(); |
| 103 | } else { |
| 104 | _putOk = false; |
nothing calls this directly
no test coverage detected