|
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
|
|
|
public function run(array $oneFileInfoArray, ?string $mode = null) |
|
22
|
|
|
{ |
|
23
|
|
|
if ($mode !== 'add' && $mode !== 'remove' && $mode !== null) { |
|
24
|
|
|
user_error('Mode must be either "add" or "remove" or not set at all', E_USER_ERROR); |
|
25
|
|
|
} |
|
26
|
|
|
$pathFromAssetsFolder = $oneFileInfoArray['PathFromAssetsFolder']; |
|
27
|
|
|
$localPath = $oneFileInfoArray['Path']; |
|
28
|
|
|
if ($oneFileInfoArray['IsDir']) { |
|
29
|
|
|
DB::alteration_message('Skipping ' . $pathFromAssetsFolder . ' as this is a folder', ''); |
|
30
|
|
|
} elseif (! empty($oneFileInfoArray['IsResizedImage'])) { |
|
31
|
|
|
if (file_exists($localPath)) { |
|
32
|
|
|
DB::alteration_message('Deleting ' . $pathFromAssetsFolder, 'deleted'); |
|
33
|
|
|
//unlink($localPath); |
|
34
|
|
|
} |
|
35
|
|
|
} elseif ($oneFileInfoArray['ErrorDBNotPresent'] && $mode !== 'remove') { |
|
36
|
|
|
DB::alteration_message('Adding file to database ' . $pathFromAssetsFolder, 'created'); |
|
37
|
|
|
$this->addFileToDb($oneFileInfoArray); |
|
38
|
|
|
} elseif ($oneFileInfoArray['ErrorIsInFileSystem'] && $mode !== 'add') { |
|
39
|
|
|
DB::alteration_message('Removing from database ' . $pathFromAssetsFolder, 'deleted'); |
|
40
|
|
|
$this->removeFileFromDb($oneFileInfoArray); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function removeFileFromDb(array $oneFileInfoArray) |
|
45
|
|
|
{ |
|
46
|
|
|
$file = File::get()->byID($oneFileInfoArray['DBID']); |
|
47
|
|
|
if ($file) { |
|
48
|
|
|
$file->DeleteFromStage(Versioned::LIVE); |
|
49
|
|
|
$file->DeleteFromStage(Versioned::DRAFT); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function addFileToDb(array $oneFileInfoArray) |
|
54
|
|
|
{ |
|
55
|
|
|
$localPath = $oneFileInfoArray['Path']; |
|
56
|
|
|
$pathFromAssetsFolder = $oneFileInfoArray['PathFromAssetsFolder']; |
|
57
|
|
|
$extension = File::get_file_extension($localPath); |
|
58
|
|
|
$newClass = File::get_class_for_file_extension($extension); |
|
59
|
|
|
$newFile = Injector::inst()->create($newClass); |
|
60
|
|
|
$newFile->setFromLocalFile($localPath, $pathFromAssetsFolder); |
|
61
|
|
|
$newFile->write(); |
|
62
|
|
|
|
|
63
|
|
|
// If file is an image, generate thumbnails |
|
64
|
|
|
if (is_a($newFile, Image::class)) { |
|
65
|
|
|
$admin = AssetAdmin::create(); |
|
66
|
|
|
$admin->generateThumbnails($newFile, true); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($this->Config()->publish_recursive) { |
|
70
|
|
|
$newFile->publishRecursive(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
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