A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: Destroy CSS Cache file when invalid (#1018)

## Summary

* Destroy CSS Cache file when invalid

## Additional Context

* Fixes issue where it would attempt to rebuild every book open

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? No

authored by

Dave Allie and committed by
GitHub
22b96ec2 c9faf2a8

+61 -49
+52 -49
lib/Epub/Epub.cpp
··· 268 268 LOG_DBG("EBP", "No CSS files to parse, but CssParser created for inline styles"); 269 269 } 270 270 271 + LOG_DBG("EBP", "CSS files to parse: %zu", cssFiles.size()); 272 + 271 273 // See if we have a cached version of the CSS rules 272 - if (!cssParser->hasCache()) { 273 - // No cache yet - parse CSS files 274 - for (const auto& cssPath : cssFiles) { 275 - LOG_DBG("EBP", "Parsing CSS file: %s", cssPath.c_str()); 274 + if (cssParser->hasCache()) { 275 + LOG_DBG("EBP", "CSS cache exists, skipping parseCssFiles"); 276 + return; 277 + } 276 278 277 - // Check heap before parsing - CSS parsing allocates heavily 278 - const uint32_t freeHeap = ESP.getFreeHeap(); 279 - if (freeHeap < MIN_HEAP_FOR_CSS_PARSING) { 280 - LOG_ERR("EBP", "Insufficient heap for CSS parsing (%u bytes free, need %zu), skipping: %s", freeHeap, 281 - MIN_HEAP_FOR_CSS_PARSING, cssPath.c_str()); 282 - continue; 283 - } 279 + // No cache yet - parse CSS files 280 + for (const auto& cssPath : cssFiles) { 281 + LOG_DBG("EBP", "Parsing CSS file: %s", cssPath.c_str()); 284 282 285 - // Check CSS file size before decompressing - skip files that are too large 286 - size_t cssFileSize = 0; 287 - if (getItemSize(cssPath, &cssFileSize)) { 288 - if (cssFileSize > MAX_CSS_FILE_SIZE) { 289 - LOG_ERR("EBP", "CSS file too large (%zu bytes > %zu max), skipping: %s", cssFileSize, MAX_CSS_FILE_SIZE, 290 - cssPath.c_str()); 291 - continue; 292 - } 293 - } 283 + // Check heap before parsing - CSS parsing allocates heavily 284 + const uint32_t freeHeap = ESP.getFreeHeap(); 285 + if (freeHeap < MIN_HEAP_FOR_CSS_PARSING) { 286 + LOG_ERR("EBP", "Insufficient heap for CSS parsing (%u bytes free, need %zu), skipping: %s", freeHeap, 287 + MIN_HEAP_FOR_CSS_PARSING, cssPath.c_str()); 288 + continue; 289 + } 294 290 295 - // Extract CSS file to temp location 296 - const auto tmpCssPath = getCachePath() + "/.tmp.css"; 297 - FsFile tempCssFile; 298 - if (!Storage.openFileForWrite("EBP", tmpCssPath, tempCssFile)) { 299 - LOG_ERR("EBP", "Could not create temp CSS file"); 291 + // Check CSS file size before decompressing - skip files that are too large 292 + size_t cssFileSize = 0; 293 + if (getItemSize(cssPath, &cssFileSize)) { 294 + if (cssFileSize > MAX_CSS_FILE_SIZE) { 295 + LOG_ERR("EBP", "CSS file too large (%zu bytes > %zu max), skipping: %s", cssFileSize, MAX_CSS_FILE_SIZE, 296 + cssPath.c_str()); 300 297 continue; 301 298 } 302 - if (!readItemContentsToStream(cssPath, tempCssFile, 1024)) { 303 - LOG_ERR("EBP", "Could not read CSS file: %s", cssPath.c_str()); 304 - tempCssFile.close(); 305 - Storage.remove(tmpCssPath.c_str()); 306 - continue; 307 - } 308 - tempCssFile.close(); 299 + } 309 300 310 - // Parse the CSS file 311 - if (!Storage.openFileForRead("EBP", tmpCssPath, tempCssFile)) { 312 - LOG_ERR("EBP", "Could not open temp CSS file for reading"); 313 - Storage.remove(tmpCssPath.c_str()); 314 - continue; 315 - } 316 - cssParser->loadFromStream(tempCssFile); 301 + // Extract CSS file to temp location 302 + const auto tmpCssPath = getCachePath() + "/.tmp.css"; 303 + FsFile tempCssFile; 304 + if (!Storage.openFileForWrite("EBP", tmpCssPath, tempCssFile)) { 305 + LOG_ERR("EBP", "Could not create temp CSS file"); 306 + continue; 307 + } 308 + if (!readItemContentsToStream(cssPath, tempCssFile, 1024)) { 309 + LOG_ERR("EBP", "Could not read CSS file: %s", cssPath.c_str()); 317 310 tempCssFile.close(); 318 311 Storage.remove(tmpCssPath.c_str()); 312 + continue; 319 313 } 314 + tempCssFile.close(); 320 315 321 - // Save to cache for next time 322 - if (!cssParser->saveToCache()) { 323 - LOG_ERR("EBP", "Failed to save CSS rules to cache"); 316 + // Parse the CSS file 317 + if (!Storage.openFileForRead("EBP", tmpCssPath, tempCssFile)) { 318 + LOG_ERR("EBP", "Could not open temp CSS file for reading"); 319 + Storage.remove(tmpCssPath.c_str()); 320 + continue; 324 321 } 325 - cssParser->clear(); 322 + cssParser->loadFromStream(tempCssFile); 323 + tempCssFile.close(); 324 + Storage.remove(tmpCssPath.c_str()); 325 + } 326 326 327 - LOG_DBG("EBP", "Loaded %zu CSS style rules from %zu files", cssParser->ruleCount(), cssFiles.size()); 327 + // Save to cache for next time 328 + if (!cssParser->saveToCache()) { 329 + LOG_ERR("EBP", "Failed to save CSS rules to cache"); 328 330 } 331 + cssParser->clear(); 332 + 333 + LOG_DBG("EBP", "Loaded %zu CSS style rules from %zu files", cssParser->ruleCount(), cssFiles.size()); 329 334 } 330 335 331 336 // load in the meta data for the epub file ··· 341 346 if (bookMetadataCache->load()) { 342 347 if (!skipLoadingCss) { 343 348 // Rebuild CSS cache when missing or when cache version changed (loadFromCache removes stale file) 344 - bool needCssRebuild = !cssParser->hasCache(); 345 - if (cssParser->hasCache() && !cssParser->loadFromCache()) { 346 - needCssRebuild = true; 347 - } 348 - if (needCssRebuild) { 349 + if (!cssParser->hasCache() || !cssParser->loadFromCache()) { 349 350 LOG_DBG("EBP", "CSS rules cache missing or stale, attempting to parse CSS files"); 351 + cssParser->deleteCache(); 352 + 350 353 if (!parseContentOpf(bookMetadataCache->coreMetadata)) { 351 354 LOG_ERR("EBP", "Could not parse content.opf from cached bookMetadata for CSS files"); 352 355 // continue anyway - book will work without CSS and we'll still load any inline style CSS
+4
lib/Epub/Epub/css/CssParser.cpp
··· 640 640 641 641 bool CssParser::hasCache() const { return Storage.exists((cachePath + rulesCache).c_str()); } 642 642 643 + void CssParser::deleteCache() const { 644 + if (hasCache()) Storage.remove((cachePath + rulesCache).c_str()); 645 + } 646 + 643 647 bool CssParser::saveToCache() const { 644 648 if (cachePath.empty()) { 645 649 return false;
+5
lib/Epub/Epub/css/CssParser.h
··· 86 86 bool hasCache() const; 87 87 88 88 /** 89 + * Delete CSS rules cache file exists 90 + */ 91 + void deleteCache() const; 92 + 93 + /** 89 94 * Save parsed CSS rules to a cache file. 90 95 * @return true if cache was written successfully 91 96 */