-
Notifications
You must be signed in to change notification settings - Fork 68
/
PublicAssetAdapter.php
76 lines (67 loc) · 1.94 KB
/
PublicAssetAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace SilverStripe\Assets\Flysystem;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Convert;
class PublicAssetAdapter extends AssetAdapter implements PublicAdapter
{
/**
* Prefix between the root url and base of the assets folder
* Used for generating public urls
*
* @var string
*/
protected $parentUrlPrefix = null;
/**
* Server specific configuration necessary to block http traffic to a local folder
*
* @config
* @var array Mapping of server configurations to configuration files necessary
*/
private static $server_configuration = [
'apache' => [
'.htaccess' => self::class . '_HTAccess'
],
'microsoft-iis' => [
'web.config' => self::class . '_WebConfig'
]
];
protected function findRoot($root)
{
if ($root) {
$path = parent::findRoot($root);
} else {
$path = ASSETS_PATH;
}
// Assign prefix based on path
$this->initParentURLPrefix($path);
return $path;
}
/**
* Provide downloadable url
*
* @param string $path
* @return string|null
*/
public function getPublicUrl($path)
{
return Controller::join_links(Director::baseURL(), $this->parentUrlPrefix, $path);
}
/**
* Initialise parent URL prefix
*
* @param string $path base path
*/
protected function initParentURLPrefix($path)
{
// Detect segment between web root directory and assets root
$path = Convert::slashes($path, '/');
$basePath = Convert::slashes(Director::publicFolder(), '/');
if (stripos($path ?? '', $basePath ?? '') === 0) {
$prefix = substr($path ?? '', strlen($basePath ?? ''));
} else {
$prefix = ASSETS_DIR;
}
$this->parentUrlPrefix = ltrim($prefix ?? '', '/');
}
}