1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\AssetsOverview\Api; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Controller\AssetAdmin; |
|
|
|
|
6
|
|
|
use SilverStripe\Assets\File; |
7
|
|
|
use SilverStripe\Assets\Image; |
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
11
|
|
|
use SilverStripe\ORM\DB; |
12
|
|
|
use SilverStripe\Versioned\Versioned; |
13
|
|
|
|
14
|
|
|
class AddAndRemoveFromDb |
15
|
|
|
{ |
16
|
|
|
use Injectable; |
17
|
|
|
use Configurable; |
18
|
|
|
|
19
|
|
|
private static $publish_recursive = true; |
20
|
|
|
|
21
|
|
|
protected $dryRun = true; |
22
|
|
|
|
23
|
|
|
public function __construct(?bool $dryRun = true) |
24
|
|
|
{ |
25
|
|
|
$this->dryRun = $dryRun; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setIsDryRun(bool $b): static |
29
|
|
|
{ |
30
|
|
|
$this->dryRun = $b; |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function run(array $oneFileInfoArray, ?string $mode = null) |
37
|
|
|
{ |
38
|
|
|
if ($mode !== 'add' && $mode !== 'remove' && $mode !== null) { |
39
|
|
|
user_error('Mode must be either "add" or "remove" or not set at all', E_USER_ERROR); |
40
|
|
|
} |
41
|
|
|
$pathFromAssetsFolder = $oneFileInfoArray['Path']; |
42
|
|
|
$absolutePath = $oneFileInfoArray['AbsolutePath']; |
43
|
|
|
|
44
|
|
|
// Usage |
45
|
|
|
if ($oneFileInfoArray['IsDir']) { |
46
|
|
|
$this->logMessage('Skipping [FOLDER]', $pathFromAssetsFolder); |
47
|
|
|
} elseif (isset($oneFileInfoArray['IsResizedImage']) && $oneFileInfoArray['IsResizedImage'] === true) { |
48
|
|
|
if (file_exists($absolutePath)) { |
49
|
|
|
$this->logMessage('Deleting', $pathFromAssetsFolder, 'deleted'); |
50
|
|
|
if ($this->dryRun === false) { |
51
|
|
|
unlink($absolutePath); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
$this->logMessage('Skipping [RESIZED IMAGE]', $pathFromAssetsFolder); |
55
|
|
|
} elseif ($oneFileInfoArray['ErrorDBNotPresent'] === true && $mode !== 'remove') { |
56
|
|
|
|
57
|
|
|
$this->logMessage('+++ Adding to DB', $pathFromAssetsFolder, 'created'); |
58
|
|
|
if ($this->dryRun === false) { |
59
|
|
|
$this->addFileToDb($oneFileInfoArray); |
60
|
|
|
} |
61
|
|
|
} elseif ($oneFileInfoArray['ErrorIsInFileSystem'] === true && $mode !== 'add') { |
62
|
|
|
|
63
|
|
|
$this->logMessage('--- Removing from DB', $pathFromAssetsFolder, 'deleted'); |
64
|
|
|
if ($this->dryRun === false) { |
65
|
|
|
$this->removeFileFromDb($oneFileInfoArray); |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
$this->logMessage('Skipping [ALL OK]', $pathFromAssetsFolder); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function logMessage(string $action, string $path, string $type = ''): void |
73
|
|
|
{ |
74
|
|
|
|
75
|
|
|
$action = strtoupper($action); |
76
|
|
|
$reasonPadding = 15; // Adjust padding as needed for alignment |
77
|
|
|
$formattedAction = str_pad($action, $reasonPadding); |
78
|
|
|
$path .= ($this->dryRun ? ' (DRY RUN)' : ' (FOR REAL)'); |
79
|
|
|
DB::alteration_message($formattedAction . ': ' . $path, $type); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function removeFileFromDb(array $oneFileInfoArray) |
84
|
|
|
{ |
85
|
|
|
$file = File::get()->byID($oneFileInfoArray['DBID']); |
86
|
|
|
if ($file) { |
87
|
|
|
$file->DeleteFromStage(Versioned::LIVE); |
88
|
|
|
$file->DeleteFromStage(Versioned::DRAFT); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function addFileToDb(array $oneFileInfoArray) |
93
|
|
|
{ |
94
|
|
|
$absolutePath = $oneFileInfoArray['AbsolutePath']; |
95
|
|
|
$pathFromAssetsFolder = $oneFileInfoArray['Path']; |
96
|
|
|
$extension = File::get_file_extension($absolutePath); |
97
|
|
|
$newClass = File::get_class_for_file_extension($extension); |
98
|
|
|
$newFile = Injector::inst()->create($newClass); |
99
|
|
|
if (file_exists($absolutePath)) { |
100
|
|
|
$newFile->setFromLocalFile($absolutePath, $pathFromAssetsFolder); |
101
|
|
|
try { |
102
|
|
|
$newFile->write(); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
DB::alteration_message('Could not write file ' . $pathFromAssetsFolder . ' because ' . $e->getMessage(), 'deleted'); |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// If file is an image, generate thumbnails |
112
|
|
|
if (is_a($newFile, Image::class)) { |
113
|
|
|
$admin = AssetAdmin::create(); |
114
|
|
|
$admin->generateThumbnails($newFile, true); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($this->Config()->publish_recursive) { |
118
|
|
|
$newFile->publishRecursive(); |
119
|
|
|
} |
120
|
|
|
$newFile->publishSingle(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths