@@ -82,13 +82,14 @@ void Config::init() {
8282 BOOTLOG (" SPIFFS mounted" );
8383 emptyFS = _isFSempty ();
8484 if (emptyFS) {
85- #ifndef FILESURL
85+ #ifndef UPDATEURL
8686 BOOTLOG (" SPIFFS is empty!" );
8787 #else
88- BOOTLOG (" SPIFFS is empty. Will attempt to get files from online..." );
89- File markerFile = SPIFFS .open (ONLINEUPDATE_MARKERFILE , " w" );
90- if (markerFile) markerFile.close ();
91- display.putRequest (NEWMODE , UPDATING );
88+ BOOTLOG (" SPIFFS is missing files. Will attempt to get files from online..." );
89+ config.deleteMainDatawwwFile ();
90+ getRequiredFiles ();
91+ delay (200 );
92+ ESP .restart ();
9293 #endif
9394 }
9495 ssidsCount = 0 ;
@@ -1169,6 +1170,23 @@ void Config::sleepForAfter(uint16_t sf, uint16_t sa){
11691170 else doSleep ();
11701171}
11711172
1173+ void Config::deleteMainDatawwwFile () {
1174+ if (requiredFilesCount > 0 ) {
1175+ const char * lastFile = requiredFiles[requiredFilesCount - 1 ];
1176+ char mainfile[64 ];
1177+ snprintf (mainfile, sizeof (mainfile), " www/%s" , lastFile);
1178+ if (SPIFFS .exists (mainfile)) {
1179+ SPIFFS .remove (mainfile);
1180+ Serial.printf (" [Config] Deleted main www file: %s\n " , mainfile);
1181+ }
1182+ snprintf (mainfile, sizeof (mainfile), " www/%s.gz" , lastFile);
1183+ if (SPIFFS .exists (mainfile)) {
1184+ SPIFFS .remove (mainfile);
1185+ Serial.printf (" [Config] Deleted main www file: %s\n " , mainfile);
1186+ }
1187+ }
1188+ }
1189+
11721190void cleanStaleSearchResults () {
11731191 const char * metaPath = " /data/searchresults.json.meta" ;
11741192 if (SPIFFS .exists (metaPath)) {
@@ -1206,41 +1224,45 @@ void fixPlaylistFileEnding() {
12061224 playlistfile.close ();
12071225}
12081226
1209- void updateFile (void * param, const char * localFile, const char * onlineFile, const char * updatePeriod, const char * simpleName) {
1210- char startMsg[128 ];
1211- snprintf (startMsg, sizeof (startMsg), " [ESPFileUpdater: %s] Started update." , simpleName);
1212- Serial.println (startMsg);
1213- ESPFileUpdater* updater = (ESPFileUpdater*)param;
1214- ESPFileUpdater::UpdateStatus result = updater->checkAndUpdate (
1215- localFile,
1216- onlineFile,
1217- updatePeriod,
1218- ESPFILEUPDATER_VERBOSE
1219- );
1220- if (result == ESPFileUpdater::UPDATED ) {
1221- Serial.printf (" [ESPFileUpdater: %s] Update completed.\n " , simpleName);
1222- } else if (result == ESPFileUpdater::NOT_MODIFIED ||result == ESPFileUpdater::MAX_AGE_NOT_REACHED ) {
1223- Serial.printf (" [ESPFileUpdater: %s] No update needed.\n " , simpleName);
1224- } else {
1225- Serial.printf (" [ESPFileUpdater: %s] Update failed.\n " , simpleName);
1226- }
1227- }
1228-
12291227#ifdef UPDATEURL
1230- void getRequiredFiles (void * param) {
1228+ void Config::getRequiredFiles () {
1229+ player.sendCommand ({PR_STOP , 0 });
1230+ display.putRequest (NEWMODE , UPDATING );
1231+ char localFileGz[64 ];
1232+ char localFile[64 ];
1233+ char tryFile[64 ];
1234+ char tryUrl[128 ];
12311235 for (size_t i = 0 ; i < requiredFilesCount; i++) {
1232- player.sendCommand ({PR_STOP , 0 });
1233- display.putRequest (NEWMODE , UPDATING );
12341236 const char * fname = requiredFiles[i];
1235- char localPath[64 ];
1236- char remoteUrl[128 ];
1237- snprintf (localPath, sizeof (localPath), " /www/%s" , fname);
1238- if (strlen (fname) > 3 && strcmp (fname + strlen (fname) - 3 , " .gz" ) == 0 ) {
1239- snprintf (remoteUrl, sizeof (remoteUrl), " %s%s" , UPDATEURL , fname);
1240- } else {
1241- snprintf (remoteUrl, sizeof (remoteUrl), " %s%s.gz" , UPDATEURL , fname);
1237+ snprintf (localFileGz, sizeof (localFileGz), " /www/%s.gz" , fname);
1238+ snprintf (localFile, sizeof (localFile), " /www/%s" , fname);
1239+ if (SPIFFS .exists (localFileGz)) SPIFFS .remove (localFileGz);
1240+ if (SPIFFS .exists (localFile)) SPIFFS .remove (localFile);
1241+ for (size_t j = 0 ; j < 2 ; j++) {
1242+ if (j == 0 ) { // Try compressed first
1243+ snprintf (tryFile, sizeof (tryFile), " %s" , localFileGz);
1244+ snprintf (tryUrl, sizeof (tryUrl), " %s%s.gz" , UPDATEURL , fname);
1245+ } else { // Fallback to uncompressed
1246+ snprintf (tryFile, sizeof (tryFile), " %s" , localFile);
1247+ snprintf (tryUrl, sizeof (tryUrl), " %s%s" , UPDATEURL , fname);
1248+ }
1249+ Serial.printf (" [ESPFileUpdater: %s] Updating required file.\n " , tryFile);
1250+ ESPFileUpdater updater (SPIFFS );
1251+ updater.setTimeout (5000 );
1252+ ESPFileUpdater::UpdateStatus result = updater.checkAndUpdate (
1253+ tryFile,
1254+ tryUrl,
1255+ " " ,
1256+ ESPFILEUPDATER_VERBOSE
1257+ );
1258+ if (result == ESPFileUpdater::UPDATED ) {
1259+ Serial.printf (" [ESPFileUpdater: %s] Download completed.\n " , tryFile);
1260+ break ; // Exit inner loop on success
1261+ } else {
1262+ if (j == 0 ) Serial.printf (" [ESPFileUpdater: %s] Download failed. Will retry for uncompressed file.\n " , tryFile);
1263+ if (j == 1 ) Serial.printf (" [ESPFileUpdater: %s] Download failed. Moving onto next file anyways.\n " , tryFile);
1264+ }
12421265 }
1243- updateFile (param, localPath, remoteUrl, " " , fname);
12441266 }
12451267 // Delete any files in /www that are not in the requiredFiles list
12461268 File root = SPIFFS .open (" /www" );
@@ -1254,7 +1276,10 @@ void updateFile(void* param, const char* localFile, const char* onlineFile, cons
12541276 if (slash) name = slash + 1 ;
12551277 bool found = false ;
12561278 for (size_t j = 0 ; j < requiredFilesCount; j++) {
1257- if (strcmp (name, requiredFiles[j]) == 0 ) {
1279+ // Check against both compressed and uncompressed names
1280+ char requiredNameGz[64 ];
1281+ snprintf (requiredNameGz, sizeof (requiredNameGz), " %s.gz" , requiredFiles[j]);
1282+ if (strcmp (name, requiredFiles[j]) == 0 || strcmp (name, requiredNameGz) == 0 ) {
12581283 found = true ;
12591284 break ;
12601285 }
@@ -1269,19 +1294,30 @@ void updateFile(void* param, const char* localFile, const char* onlineFile, cons
12691294 }
12701295#endif // #ifdef UPDATEURL
12711296
1297+ void Config::updateFile (void * param, const char * localFile, const char * onlineFile, const char * updatePeriod, const char * simpleName) {
1298+ char startMsg[128 ];
1299+ snprintf (startMsg, sizeof (startMsg), " [ESPFileUpdater: %s] Started update." , simpleName);
1300+ Serial.println (startMsg);
1301+ ESPFileUpdater* updater = (ESPFileUpdater*)param;
1302+ ESPFileUpdater::UpdateStatus result = updater->checkAndUpdate (
1303+ localFile,
1304+ onlineFile,
1305+ updatePeriod,
1306+ ESPFILEUPDATER_VERBOSE
1307+ );
1308+ if (result == ESPFileUpdater::UPDATED ) {
1309+ Serial.printf (" [ESPFileUpdater: %s] Update completed.\n " , simpleName);
1310+ } else if (result == ESPFileUpdater::NOT_MODIFIED ||result == ESPFileUpdater::MAX_AGE_NOT_REACHED ) {
1311+ Serial.printf (" [ESPFileUpdater: %s] No update needed.\n " , simpleName);
1312+ } else {
1313+ Serial.printf (" [ESPFileUpdater: %s] Update failed.\n " , simpleName);
1314+ }
1315+ }
1316+
12721317void startAsyncServices (void * param){
12731318 fixPlaylistFileEnding ();
1274- // if the OTA marker file exists, fetch all web assets immediately, clean up, restart
1275- #ifdef UPDATEURL
1276- if (SPIFFS .exists (ONLINEUPDATE_MARKERFILE )) {
1277- getRequiredFiles (param);
1278- SPIFFS .remove (ONLINEUPDATE_MARKERFILE );
1279- delay (200 );
1280- ESP .restart ();
1281- }
1282- #endif
1283- updateFile (param, " /www/timezones.json.gz" , TIMEZONES_JSON_URL , " 1 week" , " Timezones database file" );
1284- updateFile (param, " /www/rb_srvrs.json" , RADIO_BROWSER_SERVERS_URL , " 4 weeks" , " Radio Browser Servers list" );
1319+ config.updateFile (param, " /www/timezones.json.gz" , TIMEZONES_JSON_URL , " 1 week" , " Timezones database file" );
1320+ config.updateFile (param, " /www/rb_srvrs.json" , RADIO_BROWSER_SERVERS_URL , " 4 weeks" , " Radio Browser Servers list" );
12851321 cleanStaleSearchResults ();
12861322 vTaskDelete (NULL );
12871323}
0 commit comments