From ce7e57977f63bb9d413d5fd97931eb07683f13d7 Mon Sep 17 00:00:00 2001 From: Yury Pikhtarev Date: Fri, 20 Jun 2025 03:47:43 +0400 Subject: [PATCH] fix(cache): auto-create cache directories when using FileStorage The UnifiedCacheSystem was constructing directory paths for Nette FileStorage but not creating them, causing "Directory not found" errors when accessing caches like 'bb_login_err'. FileStorage expects directories to already exist. Changes: - Add automatic directory creation using bb_mkdir() before FileStorage init - Handle both regular cache directories and SQLite parent directories - Apply to both _buildStorage() and _buildDatastoreStorage() methods - Add proper error handling with RuntimeException for failed creation - Maintain consistency with TorrentPier's directory creation patterns This ensures cache directories are created automatically when first accessed, eliminating the need for manual directory creation during deployment. Fixes: Cache initialization failures with missing directories --- src/Cache/UnifiedCacheSystem.php | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Cache/UnifiedCacheSystem.php b/src/Cache/UnifiedCacheSystem.php index 594910afc..07a617388 100644 --- a/src/Cache/UnifiedCacheSystem.php +++ b/src/Cache/UnifiedCacheSystem.php @@ -165,10 +165,23 @@ class UnifiedCacheSystem case 'redis': // Some deprecated cache types will fall back to file storage $dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/' . $cache_name . '/'; + + // Create directory automatically using TorrentPier's bb_mkdir function + if (!is_dir($dir) && !bb_mkdir($dir)) { + throw new \RuntimeException("Failed to create cache directory: $dir"); + } + return new FileStorage($dir); case 'sqlite': $dbFile = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/' . $cache_name . '.db'; + + // Create parent directory for SQLite file + $dbDir = dirname($dbFile); + if (!is_dir($dbDir) && !bb_mkdir($dbDir)) { + throw new \RuntimeException("Failed to create cache directory for SQLite: $dbDir"); + } + return new SQLiteStorage($dbFile); case 'memory': @@ -183,6 +196,12 @@ class UnifiedCacheSystem default: // Fallback to file storage $dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/' . $cache_name . '/'; + + // Create directory automatically using TorrentPier's bb_mkdir function + if (!is_dir($dir) && !bb_mkdir($dir)) { + throw new \RuntimeException("Failed to create cache directory: $dir"); + } + return new FileStorage($dir); } } @@ -218,10 +237,23 @@ class UnifiedCacheSystem case 'redis': // Some deprecated cache types will fall back to file storage $dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/datastore/'; + + // Create directory automatically using TorrentPier's bb_mkdir function + if (!is_dir($dir) && !bb_mkdir($dir)) { + throw new \RuntimeException("Failed to create datastore directory: $dir"); + } + return new FileStorage($dir); case 'sqlite': $dbFile = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/datastore.db'; + + // Create parent directory for SQLite file + $dbDir = dirname($dbFile); + if (!is_dir($dbDir) && !bb_mkdir($dbDir)) { + throw new \RuntimeException("Failed to create datastore directory for SQLite: $dbDir"); + } + return new SQLiteStorage($dbFile); case 'memory': @@ -236,6 +268,12 @@ class UnifiedCacheSystem default: // Fallback to file storage $dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/datastore/'; + + // Create directory automatically using TorrentPier's bb_mkdir function + if (!is_dir($dir) && !bb_mkdir($dir)) { + throw new \RuntimeException("Failed to create datastore directory: $dir"); + } + return new FileStorage($dir); } }