-
Notifications
You must be signed in to change notification settings - Fork 4
/
FlysystemUrlTrait.php
64 lines (55 loc) · 1.43 KB
/
FlysystemUrlTrait.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
<?php
namespace Drupal\flysystem\Plugin;
use Drupal\Core\Url;
use League\Flysystem\Util;
/**
* Helper trait for generating URLs from adapter plugins.
*/
trait FlysystemUrlTrait {
/**
* Returns a web accessible URL for the resource.
*
* This function should return a URL that can be embedded in a web page
* and accessed from a browser. For example, the external URL of
* "youtube://xIpLd0WQKCY" might be
* "http://www.youtube.com/watch?v=xIpLd0WQKCY".
*
* @param string $uri
* The URI to provide a URL for.
*
* @return string
* Returns a string containing a web accessible URL for the resource.
*/
public function getExternalUrl($uri) {
$path = str_replace('\\', '/', $this->getTarget($uri));
$arguments = [
'scheme' => $this->getScheme($uri),
'filepath' => $path,
];
return Url::fromRoute('flysystem.serve', $arguments, ['absolute' => TRUE])->toString();
}
/**
* Returns the target file path of a URI.
*
* @param string $uri
* The URI.
*
* @return string
* The file path of the URI.
*/
protected function getTarget($uri) {
return Util::normalizePath(substr($uri, strpos($uri, '://') + 3));
}
/**
* Returns the scheme from the internal URI.
*
* @param string $uri
* The URI.
*
* @return string
* The scheme.
*/
protected function getScheme($uri) {
return substr($uri, 0, strpos($uri, '://'));
}
}