BUG Fix GeneratedAssetHandler crashing on expired resources

This commit is contained in:
Damian Mooyman 2015-10-23 11:46:58 +13:00
parent f9892c628c
commit fe3d23f0d4
2 changed files with 29 additions and 10 deletions

View File

@ -105,7 +105,10 @@ class CacheGeneratedAssetHandler implements GeneratedAssetHandler, Flushable {
$data = $cache->load($cacheID);
if($data) {
$result = unserialize($data);
return $this->validateResult($result, $filename);
$valid = $this->validateResult($result, $filename);
if($valid) {
return $result;
}
}
// Regenerate
@ -127,8 +130,14 @@ class CacheGeneratedAssetHandler implements GeneratedAssetHandler, Flushable {
if($result) {
$cache->save(serialize($result), $cacheID);
}
return $this->validateResult($result, $filename);
// Ensure this result is successfully saved
$valid = $this->validateResult($result, $filename);
if($valid) {
return $result;
}
throw new Exception("Error regenerating file \"{$filename}\"");
}
/**
@ -151,17 +160,16 @@ class CacheGeneratedAssetHandler implements GeneratedAssetHandler, Flushable {
*
* @param mixed $result
* @param string $filename
* @return array The result
* @throws Exception
* @return bool True if this $result is valid
*/
protected function validateResult($result, $filename) {
if(!$result) {
return false;
}
// Retrieve URL from tuple
$store = $this->getAssetStore();
if($result && $store->exists($result['Filename'], $result['Hash'], $result['Variant'])) {
return $result;
}
throw new Exception("Error regenerating file \"{$filename}\"");
return $store->exists($result['Filename'], $result['Hash'], $result['Variant']);
}
}

View File

@ -7,6 +7,7 @@ use SilverStripe\Filesystem\Flysystem\FlysystemAssetStore;
use SilverStripe\Filesystem\Flysystem\FlysystemUrlPlugin;
use SilverStripe\Filesystem\Storage\AssetContainer;
use SilverStripe\Filesystem\Storage\AssetStore;
use SilverStripe\Filesystem\Storage\CacheGeneratedAssetHandler;
class AssetStoreTest extends SapphireTest {
@ -450,6 +451,10 @@ class AssetStoreTest_SpyStore extends FlysystemAssetStore {
* Reset defaults for this store
*/
public static function reset() {
// Need flushing since it won't have any files left
CacheGeneratedAssetHandler::flush();
// Remove all files in this store
if(self::$basedir) {
$path = self::base_path();
if(file_exists($path)) {
@ -469,6 +474,12 @@ class AssetStoreTest_SpyStore extends FlysystemAssetStore {
if($asset instanceof Folder) {
return self::base_path() . '/' . $asset->getFilename();
}
if($asset instanceof File) {
$asset = $asset->File;
}
if($asset instanceof DBFile) {
return BASE_PATH . $asset->getSourceURL();
}
return BASE_PATH . $asset->getUrl();
}