|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\DevToolkit\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\ORM\DB; |
|
7
|
|
|
use SilverStripe\Assets\File; |
|
8
|
|
|
use SilverStripe\Dev\BuildTask; |
|
9
|
|
|
use SilverStripe\Control\Director; |
|
10
|
|
|
use LeKoala\DevToolkit\BuildTaskTools; |
|
11
|
|
|
use LeKoala\Base\Subsite\SubsiteHelper; |
|
|
|
|
|
|
12
|
|
|
use SilverStripe\AssetAdmin\Controller\AssetAdmin; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Generates FileHash and moves files to .protected folder |
|
16
|
|
|
* Re-generates CMS thumbs on the second run and publish files |
|
17
|
|
|
* |
|
18
|
|
|
* @link https://gist.github.com/thezenmonkey/1b7846a01255e94c02906da6d121385a |
|
19
|
|
|
* @link https://forum.silverstripe.org/t/upgrade-from-3-to-4-1-assets-dont-work/184/7 |
|
20
|
|
|
* @link https://docs.silverstripe.org/en/4/developer_guides/files/file_migration/ |
|
21
|
|
|
*/ |
|
22
|
|
|
class PublishAllFilesTask extends BuildTask |
|
23
|
|
|
{ |
|
24
|
|
|
use BuildTaskTools; |
|
25
|
|
|
|
|
26
|
|
|
protected $title = "Publish All Files"; |
|
27
|
|
|
protected $description = 'Publish all assets that are in draft'; |
|
28
|
|
|
private static $segment = 'PublishAllFilesTask'; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return AssetAdmin |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function getAssetAdmin() |
|
34
|
|
|
{ |
|
35
|
|
|
return AssetAdmin::singleton(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function run($request) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->request = $request; |
|
41
|
|
|
|
|
42
|
|
|
set_time_limit(0); |
|
43
|
|
|
SubsiteHelper::disableFilter(); |
|
44
|
|
|
$admin = self::getAssetAdmin(); |
|
45
|
|
|
|
|
46
|
|
|
$originalDir = BASE_PATH . '/' . Director::publicDir() . '/assets/'; |
|
47
|
|
|
|
|
48
|
|
|
$files = File::get(); |
|
49
|
|
|
|
|
50
|
|
|
$this->message("Processing {$files->count()} files"); |
|
51
|
|
|
|
|
52
|
|
|
$i = 0; |
|
53
|
|
|
foreach ($files as $file) { |
|
54
|
|
|
$i++; |
|
55
|
|
|
$name = $file->getFilename(); |
|
56
|
|
|
|
|
57
|
|
|
if (!$name) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$originalName = $originalDir . $name; |
|
62
|
|
|
|
|
63
|
|
|
// Generate a file hash if not set |
|
64
|
|
|
if (!$file->getField('FileHash') && is_file($originalName)) { |
|
65
|
|
|
$hash = sha1_file($originalName); |
|
66
|
|
|
DB::query('UPDATE "File" SET "FileHash" = \'' . $hash . '\' WHERE "ID" = \'' . $file->ID . '\' LIMIT 1;'); |
|
67
|
|
|
$targetDir = str_replace('./', '', BASE_PATH . '/' . Director::publicDir() . '/assets/.protected/' . dirname($name) |
|
68
|
|
|
. '/' . substr($hash, 0, 10) . '/'); |
|
69
|
|
|
if (!file_exists($targetDir)) { |
|
70
|
|
|
mkdir($targetDir, 0755, true); |
|
71
|
|
|
} |
|
72
|
|
|
rename($originalDir . $name, $targetDir . basename($name)); |
|
73
|
|
|
echo '<b style="color:red">' . $originalDir . $name . ' > ' . $targetDir . basename($name) . '</b><br>'; |
|
74
|
|
|
} else { |
|
75
|
|
|
// Will only apply to images |
|
76
|
|
|
$admin->generateThumbnails($file); |
|
77
|
|
|
// Publish |
|
78
|
|
|
try { |
|
79
|
|
|
$file->copyVersionToStage('Stage', 'Live'); |
|
80
|
|
|
$this->message("Published $name", "created"); |
|
81
|
|
|
} catch (Exception $ex) { |
|
82
|
|
|
$this->message($ex->getMessage(), "error"); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$file->destroy(); |
|
87
|
|
|
} |
|
88
|
|
|
$this->message("Processed $i files"); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function isEnabled() |
|
92
|
|
|
{ |
|
93
|
|
|
return Director::isDev(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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