|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\PerfectCmsImages\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Filesystem; |
|
6
|
|
|
use ReflectionMethod; |
|
7
|
|
|
use SilverStripe\Assets\Flysystem\FlysystemAssetStore; |
|
8
|
|
|
use SilverStripe\Assets\Image; |
|
9
|
|
|
use SilverStripe\Assets\Storage\AssetStore; |
|
10
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
11
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
12
|
|
|
use SilverStripe\Dev\BuildTask; |
|
13
|
|
|
use SilverStripe\ORM\DataObject; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* SOURCE: https://gist.github.com/blueo/6598bc349b406cf678f9a8f009587a95 |
|
17
|
|
|
* Class DeleteGeneratedImagesTask. |
|
18
|
|
|
* |
|
19
|
|
|
* Hack to allow removing manipulated images |
|
20
|
|
|
* This is needed occasionally when manipulation functions change |
|
21
|
|
|
* It isn't directly possible with core so this is a workaround |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://github.com/silverstripe/silverstripe-assets/issues/109 |
|
24
|
|
|
* @codeCoverageIgnore |
|
25
|
|
|
*/ |
|
26
|
|
|
class DeleteGeneratedImagesTask extends BuildTask |
|
27
|
|
|
{ |
|
28
|
|
|
public function getDescription(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return 'Regenerate Images for an asset'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create test jobs for the purposes of testing. |
|
35
|
|
|
* |
|
36
|
|
|
* @param HTTPRequest $request |
|
37
|
|
|
*/ |
|
38
|
|
|
public function run($request) // phpcs:ignore |
|
39
|
|
|
{ |
|
40
|
|
|
$Id = $request->getVar('ID'); |
|
41
|
|
|
if (! $Id) { |
|
42
|
|
|
echo 'No ID provided, make sure to supply an ID to the URL eg ?ID=2'; |
|
43
|
|
|
|
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$image = DataObject::get_by_id(Image::class, $Id); |
|
48
|
|
|
|
|
49
|
|
|
if (! $image) { |
|
|
|
|
|
|
50
|
|
|
echo 'No Image found with that ID'; |
|
51
|
|
|
|
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$asetValues = $image->File->getValue(); |
|
|
|
|
|
|
56
|
|
|
$store = Injector::inst()->get(AssetStore::class); |
|
57
|
|
|
|
|
58
|
|
|
// warning - super hacky as accessing private methods |
|
59
|
|
|
$getID = new ReflectionMethod(FlysystemAssetStore::class, 'getFileID'); |
|
60
|
|
|
$getID->setAccessible(true); |
|
61
|
|
|
|
|
62
|
|
|
$flyID = $getID->invoke($store, $asetValues['Filename'], $asetValues['Hash']); |
|
63
|
|
|
$getFileSystem = new ReflectionMethod(FlysystemAssetStore::class, 'getFilesystemFor'); |
|
64
|
|
|
|
|
65
|
|
|
$getFileSystem->setAccessible(true); |
|
66
|
|
|
/** @var Filesystem $system */ |
|
67
|
|
|
$system = $getFileSystem->invoke($store, $flyID); |
|
68
|
|
|
|
|
69
|
|
|
$findVariants = new ReflectionMethod(FlysystemAssetStore::class, 'findVariants'); |
|
70
|
|
|
$findVariants->setAccessible(true); |
|
71
|
|
|
foreach ($findVariants->invoke($store, $flyID, $system) as $variant) { |
|
72
|
|
|
$isGenerated = strpos($variant, '__'); |
|
73
|
|
|
if (! $isGenerated) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$system->delete($variant); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
echo "Deleted generated images for {$image->Name}"; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|