|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\MoveLargeFilesToAssets; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
|
6
|
|
|
use SilverStripe\Assets\Folder; |
|
7
|
|
|
use SilverStripe\Control\Controller; |
|
8
|
|
|
use SilverStripe\Control\Director; |
|
9
|
|
|
use SilverStripe\Dev\BuildTask; |
|
10
|
|
|
use SilverStripe\ORM\DB; |
|
11
|
|
|
|
|
12
|
|
|
class MoveFiles extends BuildTask |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritDoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $title = 'Copy (large) files to assets'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritDoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'Move public/new-files to assets/new-files where new-files can be set as you see fit.'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $enabled = true; |
|
28
|
|
|
|
|
29
|
|
|
private static $folder_name = 'new-files'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function run($request) |
|
35
|
|
|
{ |
|
36
|
|
|
$folderName = self::config()->get('folder_name'); |
|
37
|
|
|
$oldPath = Controller::join_links(ASSETS_PATH, '..', $folderName); |
|
38
|
|
|
$newPath = Controller::join_links(ASSETS_PATH, $folderName); |
|
39
|
|
|
if (! file_exists($newPath)) { |
|
40
|
|
|
if (file_exists($oldPath)) { |
|
41
|
|
|
DB::alteration_message('Moved ' . $oldPath . ' to ' . $newPath . ''); |
|
42
|
|
|
rename($oldPath, $newPath); |
|
43
|
|
|
} else { |
|
44
|
|
|
DB::alteration_message('Could not move ' . $oldPath . ' to ' . $newPath . ' because ' . $oldPath . ' does not exist.'); |
|
45
|
|
|
} |
|
46
|
|
|
} else { |
|
47
|
|
|
DB::alteration_message('Could not move ' . $oldPath . ' to ' . $newPath . ' because ' . $newPath . ' already exists.'); |
|
48
|
|
|
} |
|
49
|
|
|
$this->addToDb($newPath); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function addToDb(string $newFolderPath) |
|
53
|
|
|
{ |
|
54
|
|
|
DB::alteration_message('scanning ' . $newFolderPath); |
|
55
|
|
|
$paths = scandir($newFolderPath); |
|
56
|
|
|
if ($paths && is_array($paths) && count($paths) < 100) { |
|
|
|
|
|
|
57
|
|
|
foreach ($paths as $path) { |
|
58
|
|
|
$path = Controller::join_links($newFolderPath, $path); |
|
59
|
|
|
DB::alteration_message('considering ' . $path); |
|
60
|
|
|
if (! is_dir($path)) { |
|
61
|
|
|
$fileName = basename($path); |
|
62
|
|
|
$folderPath = dirname($path); |
|
63
|
|
|
$folder = Folder::find_or_make($folderPath); |
|
64
|
|
|
$filter = ['Name' => $fileName, 'ParentID' => $folder->ID]; |
|
65
|
|
|
$file = File::get()->filter($filter)->first(); |
|
66
|
|
|
if (! $file) { |
|
67
|
|
|
DB::alteration_message('New file: ' . $path); |
|
68
|
|
|
$file = File::create(); |
|
69
|
|
|
$file->setFromLocalFile($path); |
|
70
|
|
|
$file->ParentID = $folder->ID; |
|
71
|
|
|
$file->write(); |
|
72
|
|
|
$file->doPublish(); |
|
73
|
|
|
} else { |
|
74
|
|
|
DB::alteration_message('existing file: ' . $path); |
|
75
|
|
|
} |
|
76
|
|
|
} else { |
|
77
|
|
|
DB::alteration_message('skipping ' . $path); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
DB::alteration_message('nothing to add'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.