@@ -78,7 +78,11 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
7878 if (_username != emptyString && _password != emptyString)
7979 if ( !request->authenticate (_username.c_str (), _password.c_str ()))
8080 return request->requestAuthentication ();
81+ #ifdef ESP32
82+ AsyncWebServerResponse* response = request->beginResponse (200 , " text/html" , serverIndex, sizeof (serverIndex));
83+ #else
8184 AsyncWebServerResponse* response = request->beginResponse_P (200 , " text/html" , serverIndex, sizeof (serverIndex));
85+ #endif
8286 response->addHeader (" Content-Encoding" , " gzip" );
8387 request->send (response); });
8488
@@ -105,29 +109,37 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
105109 if (!_authenticated)
106110 return request->requestAuthentication ();
107111
108- if (Update. hasError () )
112+ if (_updateResult != UpdateResult::UPDATE_OK )
109113 {
110- AsyncWebServerResponse *response = request->beginResponse (200 , F (" text/html" ), String (F (" Update error: " )) + _updaterError);
114+ AsyncWebServerResponse *response = request->beginResponse (200 , F (" text/html" ), Update. hasError () ? String (F (" Update error: " )) + _updaterError : " Update aborted by server. " );
111115 response->addHeader (" Access-Control-Allow-Headers" , " *" );
112116 response->addHeader (" Access-Control-Allow-Origin" , " *" );
113117 response->addHeader (" Connection" , " close" );
114118 request->send (response);
115119 }
116120 else
117121 {
122+ #ifdef ESP32
123+ request->send (200 , PSTR (" text/html" ), successResponse);
124+ #else
118125 request->send_P (200 , PSTR (" text/html" ), successResponse);
119- restartTimer.once_ms (1000 , ESP.restart );
126+ #endif
127+ Log (" Rebooting...\n " );
128+ restartTimer.once_ms (1000 ,[]{ ESP.restart (); });
120129 } },
121130 [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final )
122131 {
123132 // handler for the file upload, gets the sketch bytes, and writes
124133 // them through the Update object
125134
126- String inputName = request->getParam (" name" )->value ();
135+ _updateType = request->getParam (" name" )->value () == " filesystem" ?
136+ UpdateType::FILE_SYSTEM :
137+ UpdateType::FIRMWARE;
127138
128139 if (!index)
129140 {
130141 _updaterError.clear ();
142+
131143#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
132144 ESPASYNCHTTPUPDATESERVER_SerialOutput.setDebugOutput (true );
133145#endif
@@ -137,13 +149,27 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
137149 Log (" Unauthenticated Update\n " );
138150 return ;
139151 }
152+
153+ if (onUpdateBegin)
154+ {
155+ _updateResult = UpdateResult::UPDATE_OK;
156+ onUpdateBegin (_updateType, _updateResult);
157+ if (_updateResult != UpdateResult::UPDATE_OK)
158+ {
159+ Log (" Update aborted by server: %d\n " , _updateResult);
160+ if (onUpdateEnd)
161+ onUpdateEnd (_updateType, _updateResult);
162+ return ;
163+ }
164+ }
165+
140166 Log (" Update: %s\n " , filename.c_str ());
141167#ifdef ESP8266
142168 Update.runAsync (true );
143169#endif
144- if (inputName == " filesystem " )
170+ if (_updateType == UpdateType::FILE_SYSTEM )
145171 {
146- Log (" updating filesystem" );
172+ Log (" updating filesystem\n " );
147173#ifdef ESP8266
148174 int command = U_FS;
149175 size_t fsSize = ((size_t )FS_end - (size_t )FS_start);
@@ -165,25 +191,28 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
165191 }
166192 else
167193 {
168- Log (" updating flash" );
194+ Log (" updating flash\n " );
169195 uint32_t maxSketchSpace = (ESP.getFreeSketchSpace () - 0x1000 ) & 0xFFFFF000 ;
170196 if (!Update.begin (maxSketchSpace, U_FLASH)) // start with max available size
171197 _setUpdaterError ();
172198 }
173199 }
174200
175- if (_authenticated && len && !_updaterError. length () )
201+ if (_authenticated && len && _updateResult == UpdateResult::UPDATE_OK )
176202 {
177203 Log (" ." );
178204 if (Update.write (data, len) != len)
179205 _setUpdaterError ();
180206 }
181207
182- if (_authenticated && final && !_updaterError. length () )
208+ if (_authenticated && final && _updateResult == UpdateResult::UPDATE_OK )
183209 {
184210 if (Update.end (true ))
185211 { // true to set the size to the current progress
186- Log (" Update Success: \n Rebooting...\n " );
212+ Log (" Update Success.\n " );
213+ _updateResult = UpdateResult::UPDATE_OK;
214+ if (onUpdateEnd)
215+ onUpdateEnd (_updateType, _updateResult);
187216 }
188217 else
189218 _setUpdaterError ();
@@ -204,4 +233,8 @@ void ESPAsyncHTTPUpdateServer::_setUpdaterError()
204233 StreamString str;
205234 Update.printError (str);
206235 _updaterError = str.c_str ();
236+
237+ _updateResult = UpdateResult::UPDATE_ERROR;
238+ if (onUpdateEnd)
239+ onUpdateEnd (_updateType, _updateResult);
207240}
0 commit comments