File upload for Web update
| 899 | |
| 900 | //File upload for Web update |
| 901 | void WebUI_Server::WebUpdateUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final) { |
| 902 | static size_t last_upload_update; |
| 903 | static uint32_t maxSketchSpace = UINT32_MAX; |
| 904 | |
| 905 | //only admin can update FW |
| 906 | if (is_authenticated() != AuthenticationLevel::LEVEL_ADMIN) { |
| 907 | _upload_status = UploadStatus::FAILED; |
| 908 | log_info("Upload rejected"); |
| 909 | sendAuthFailed(request); |
| 910 | //pushError(request, ESP_ERROR_AUTHENTICATION, "Upload rejected", 401); |
| 911 | } else { |
| 912 | //Upload start |
| 913 | //************** |
| 914 | if (!index) { //upload.status == UPLOAD_FILE_START) { |
| 915 | log_info("Update Firmware"); |
| 916 | _upload_status = UploadStatus::ONGOING; |
| 917 | std::string sizeargname(filename.c_str()); |
| 918 | sizeargname += "S"; |
| 919 | if (request->hasParam(sizeargname.c_str())) |
| 920 | maxSketchSpace = request->getParam(sizeargname.c_str())->value().toInt(); |
| 921 | else if (request->hasHeader("Content-Length")) |
| 922 | maxSketchSpace = request->getHeader("Content-Length")->value().toInt(); |
| 923 | //check space |
| 924 | size_t flashsize = 0; |
| 925 | if (esp_ota_get_running_partition()) { |
| 926 | const esp_partition_t* partition = esp_ota_get_next_update_partition(NULL); |
| 927 | if (partition) { |
| 928 | flashsize = partition->size; |
| 929 | } |
| 930 | } |
| 931 | if (flashsize < maxSketchSpace) { |
| 932 | String msg = String("Upload rejected, not enough space (needs " + String(maxSketchSpace) + ", has " + String(flashsize)); |
| 933 | pushError(request, ESP_ERROR_NOT_ENOUGH_SPACE, msg.c_str()); |
| 934 | _upload_status = UploadStatus::FAILED; |
| 935 | log_info("Update cancelled"); |
| 936 | } |
| 937 | if (_upload_status != UploadStatus::FAILED) { |
| 938 | last_upload_update = 0; |
| 939 | if (!Update.begin()) { //start with max available size |
| 940 | _upload_status = UploadStatus::FAILED; |
| 941 | log_info("Update cancelled"); |
| 942 | pushError(request, ESP_ERROR_NOT_ENOUGH_SPACE, "Upload rejected, not enough space"); |
| 943 | } else { |
| 944 | log_info("Update 0%"); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | //Upload write |
| 949 | //************** |
| 950 | //check if no error |
| 951 | if (_upload_status == UploadStatus::ONGOING) { |
| 952 | if (((100 * index) / maxSketchSpace) != last_upload_update) { |
| 953 | if (maxSketchSpace > 0) { |
| 954 | last_upload_update = (100 * index) / maxSketchSpace; |
| 955 | } else { |
| 956 | last_upload_update = index; |
| 957 | } |
| 958 |