1
|
|
|
<?php namespace Anomaly\Streams\Platform\Image; |
2
|
|
|
|
3
|
|
|
use Anomaly\FilesModule\File\Contract\FileInterface; |
4
|
|
|
use Anomaly\Streams\Platform\Application\Application; |
5
|
|
|
use Illuminate\Config\Repository; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ImagePaths |
10
|
|
|
* |
11
|
|
|
* @link http://anomaly.is/streams-platform |
12
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
13
|
|
|
* @author Ryan Thompson <[email protected]> |
14
|
|
|
* @package Anomaly\Streams\Platform\Image |
15
|
|
|
*/ |
16
|
|
|
class ImagePaths |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Predefined paths. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $paths = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The config repository. |
28
|
|
|
* |
29
|
|
|
* @var Repository |
30
|
|
|
*/ |
31
|
|
|
protected $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The request object. |
35
|
|
|
* |
36
|
|
|
* @var Request |
37
|
|
|
*/ |
38
|
|
|
protected $request; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The application object. |
42
|
|
|
* |
43
|
|
|
* @var Application |
44
|
|
|
*/ |
45
|
|
|
protected $application; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create a new ImagePaths instance. |
49
|
|
|
* |
50
|
|
|
* @param Repository $config |
51
|
|
|
* @param Request $request |
52
|
|
|
* @param Application $application |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function __construct(Repository $config, Request $request, Application $application) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->config = $config; |
57
|
|
|
$this->request = $request; |
58
|
|
|
$this->application = $application; |
59
|
|
|
|
60
|
|
|
$this->paths = $config->get('streams::images.paths', []); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the paths. |
65
|
|
|
* |
66
|
|
|
* @return array|mixed |
67
|
|
|
*/ |
68
|
|
|
public function getPaths() |
69
|
|
|
{ |
70
|
|
|
return $this->paths; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the paths. |
75
|
|
|
* |
76
|
|
|
* @param array $paths |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setPaths(array $paths) |
80
|
|
|
{ |
81
|
|
|
$this->paths = $paths; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Add an image path hint. |
88
|
|
|
* |
89
|
|
|
* @param $namespace |
90
|
|
|
* @param $path |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function addPath($namespace, $path) |
94
|
|
|
{ |
95
|
|
|
$this->paths[$namespace] = $path; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the real path for a given path. |
102
|
|
|
* |
103
|
|
|
* @param $path |
104
|
|
|
* @return string |
105
|
|
|
* @throws \Exception |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function realPath($path) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
if (str_contains($path, '::')) { |
110
|
|
|
|
111
|
|
|
list($namespace, $path) = explode('::', $path); |
112
|
|
|
|
113
|
|
|
if (!isset($this->paths[$namespace])) { |
114
|
|
|
throw new \Exception("Path hint [{$namespace}::{$path}] does not exist!"); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return rtrim($this->paths[$namespace], '/') . '/' . $path; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $path; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return the output path for an image. |
125
|
|
|
* |
126
|
|
|
* @param $path |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function outputPath(Image $image) |
130
|
|
|
{ |
131
|
|
|
$path = $image->getImage(); |
132
|
|
|
|
133
|
|
|
if ($path instanceof FileInterface) { |
|
|
|
|
134
|
|
|
$path = $path->path(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* If the path is already public |
139
|
|
|
* then just use it as it is. |
140
|
|
|
*/ |
141
|
|
|
if (str_contains($path, public_path())) { |
142
|
|
|
return str_replace(public_path(), '', $path); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* If the path is a file or file path then |
147
|
|
|
* put it in /app/{$application}/files/disk/folder/filename.ext |
148
|
|
|
*/ |
149
|
|
|
if (is_string($path) && str_is('*://*', $path)) { |
150
|
|
|
|
151
|
|
|
$application = $this->application->getReference(); |
152
|
|
|
|
153
|
|
|
list($disk, $folder, $filename) = explode('/', str_replace('://', '/', $path)); |
154
|
|
|
|
155
|
|
|
if ($rename = $image->getFilename()) { |
156
|
|
|
|
157
|
|
|
$filename = $rename; |
158
|
|
|
|
159
|
|
|
if (strpos($filename, DIRECTORY_SEPARATOR)) { |
160
|
|
|
$directory = null; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return "/app/{$application}/files/{$disk}/{$folder}/{$filename}"; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the real path relative to our installation. |
169
|
|
|
*/ |
170
|
|
|
$path = str_replace(base_path(), '', $this->realPath($path)); |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Build out path parts. |
174
|
|
|
*/ |
175
|
|
|
$filename = basename($path); |
176
|
|
|
$directory = ltrim(dirname($path), '/\\') . '/'; |
177
|
|
|
$application = $this->application->getReference(); |
178
|
|
|
|
179
|
|
|
if ($image->getAlterations() || $image->getQuality()) { |
|
|
|
|
180
|
|
|
$filename = md5( |
181
|
|
|
var_export([$path, $image->getAlterations()], true) . $image->getQuality() |
182
|
|
|
) . '.' . $image->getExtension(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($rename = $image->getFilename()) { |
186
|
|
|
|
187
|
|
|
$directory = null; |
188
|
|
|
$filename = ltrim($rename, '/\\'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return "/app/{$application}/assets/{$directory}{$filename}"; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return the path prefix. |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function prefix() |
200
|
|
|
{ |
201
|
|
|
return rtrim(array_get(parse_url($this->request->root()), 'path'), '/'); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|