'; } + AssetCache::clear_cache(); ob_flush(); self::get_latest_version(true); } diff --git a/src/Module/Util/AssetCache.php b/src/Module/Util/AssetCache.php new file mode 100644 index 0000000000..70937ca9a1 --- /dev/null +++ b/src/Module/Util/AssetCache.php @@ -0,0 +1,100 @@ +. + * + */ + +declare(strict_types=0); + +namespace Ampache\Module\Util; + +use Ampache\Config\AmpConfig; +use Ampache\Module\System\Core; + +/** + * A collection of methods related to cache-busting assets + */ +class AssetCache +{ + private static string $cacheDir = '/cached_assets/'; + private static string $cacheDirURL; + private static string $cacheDirPath; + + private static function set_cache_dir() + { + self::$cacheDirURL = AmpConfig::get('web_path') . self::$cacheDir; + self::$cacheDirPath = Core::get_server('DOCUMENT_ROOT') . self::$cacheDir; + } + + public static function get_url(string $path) + { + self::set_cache_dir(); + + $originalFileURL = $path; + $originalFilePath = Core::get_server('DOCUMENT_ROOT') . str_replace(AmpConfig::get('web_path'), '', $path); + $originalFilePathArray = pathinfo($path); + + $cachedFileURL = self::$cacheDirURL . $originalFilePathArray['filename'] . '-' . md5_file($originalFilePath) . '.' . $originalFilePathArray['extension']; + $cachedFilePath = self::$cacheDirPath . $originalFilePathArray['filename'] . '-' . md5_file($originalFilePath) . '.' . $originalFilePathArray['extension']; + + if (!file_exists($cachedFilePath)) { + self::copy_file($originalFilePath); + } + + if (file_exists($cachedFilePath)) { + return $cachedFileURL; + } + + // if all else fails return original file + return $originalFileURL; + } + + private static function copy_file(string $path) + { + self::create_cache_dir(); + + $filePath = str_replace(AmpConfig::get('web_path'), Core::get_server('DOCUMENT_ROOT'), $path); + $filepathArray = pathinfo($path); + + if (file_exists($filePath)) { + $cachedVersion = self::$cacheDirPath . $filepathArray['filename'] . '-' . md5_file($filePath) . '.' . $filepathArray['extension']; + copy($filePath, $cachedVersion); + } + } + + private static function create_cache_dir() + { + if (!file_exists(self::$cacheDirPath)) { + mkdir(self::$cacheDirPath, 0755); + } + } + + public static function clear_cache() + { + self::set_cache_dir(); + + $files = glob(self::$cacheDirPath . '*'); + + foreach ($files as $file) { + if (is_file($file)) { + unlink($file); + } + } + } +}
'; } + AssetCache::clear_cache(); ob_flush(); self::get_latest_version(true); } diff --git a/src/Module/Util/AssetCache.php b/src/Module/Util/AssetCache.php new file mode 100644 index 0000000000..70937ca9a1 --- /dev/null +++ b/src/Module/Util/AssetCache.php @@ -0,0 +1,100 @@ +. + * + */ + +declare(strict_types=0); + +namespace Ampache\Module\Util; + +use Ampache\Config\AmpConfig; +use Ampache\Module\System\Core; + +/** + * A collection of methods related to cache-busting assets + */ +class AssetCache +{ + private static string $cacheDir = '/cached_assets/'; + private static string $cacheDirURL; + private static string $cacheDirPath; + + private static function set_cache_dir() + { + self::$cacheDirURL = AmpConfig::get('web_path') . self::$cacheDir; + self::$cacheDirPath = Core::get_server('DOCUMENT_ROOT') . self::$cacheDir; + } + + public static function get_url(string $path) + { + self::set_cache_dir(); + + $originalFileURL = $path; + $originalFilePath = Core::get_server('DOCUMENT_ROOT') . str_replace(AmpConfig::get('web_path'), '', $path); + $originalFilePathArray = pathinfo($path); + + $cachedFileURL = self::$cacheDirURL . $originalFilePathArray['filename'] . '-' . md5_file($originalFilePath) . '.' . $originalFilePathArray['extension']; + $cachedFilePath = self::$cacheDirPath . $originalFilePathArray['filename'] . '-' . md5_file($originalFilePath) . '.' . $originalFilePathArray['extension']; + + if (!file_exists($cachedFilePath)) { + self::copy_file($originalFilePath); + } + + if (file_exists($cachedFilePath)) { + return $cachedFileURL; + } + + // if all else fails return original file + return $originalFileURL; + } + + private static function copy_file(string $path) + { + self::create_cache_dir(); + + $filePath = str_replace(AmpConfig::get('web_path'), Core::get_server('DOCUMENT_ROOT'), $path); + $filepathArray = pathinfo($path); + + if (file_exists($filePath)) { + $cachedVersion = self::$cacheDirPath . $filepathArray['filename'] . '-' . md5_file($filePath) . '.' . $filepathArray['extension']; + copy($filePath, $cachedVersion); + } + } + + private static function create_cache_dir() + { + if (!file_exists(self::$cacheDirPath)) { + mkdir(self::$cacheDirPath, 0755); + } + } + + public static function clear_cache() + { + self::set_cache_dir(); + + $files = glob(self::$cacheDirPath . '*'); + + foreach ($files as $file) { + if (is_file($file)) { + unlink($file); + } + } + } +}