|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\AssetsOverview\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
|
6
|
|
|
use SilverStripe\Control\Director; |
|
7
|
|
|
|
|
8
|
|
|
trait FilesystemRelatedTraits |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $baseFolder = ''; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $publicBaseFolder = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $assetsBaseFolder = ''; |
|
24
|
|
|
|
|
25
|
|
|
protected function humanFileSize(int $bytes, int $decimals = 0): string |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
return File::format_size($bytes); |
|
28
|
|
|
// $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
|
29
|
|
|
// $factor = floor((strlen( (string) $bytes) - 1) / 3); |
|
30
|
|
|
// |
|
31
|
|
|
// return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getExtension(string $path): string |
|
35
|
|
|
{ |
|
36
|
|
|
return pathinfo($path, PATHINFO_EXTENSION); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function getBaseFolder(): string |
|
40
|
|
|
{ |
|
41
|
|
|
if (! $this->baseFolder) { |
|
42
|
|
|
$this->baseFolder = rtrim(Director::baseFolder(), DIRECTORY_SEPARATOR); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->baseFolder; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getPublicBaseFolder(): string |
|
49
|
|
|
{ |
|
50
|
|
|
if (! $this->publicBaseFolder) { |
|
51
|
|
|
$this->publicBaseFolder = rtrim(Director::publicFolder(), DIRECTORY_SEPARATOR); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->publicBaseFolder; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getAssetsBaseFolder(): string |
|
58
|
|
|
{ |
|
59
|
|
|
if (! $this->assetsBaseFolder) { |
|
60
|
|
|
$this->assetsBaseFolder = rtrim(ASSETS_PATH, DIRECTORY_SEPARATOR); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->assetsBaseFolder; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.