|
10 | 10 | use Hyde\Foundation\HydeKernel; |
11 | 11 | use Hyde\Support\Filesystem\MediaFile; |
12 | 12 | use Hyde\Framework\Exceptions\FileNotFoundException; |
13 | | -use Illuminate\Support\Str; |
14 | 13 |
|
15 | 14 | use function str_ends_with; |
16 | 15 | use function str_starts_with; |
17 | 16 | use function substr_count; |
18 | | -use function file_exists; |
19 | 17 | use function str_replace; |
20 | 18 | use function str_repeat; |
21 | 19 | use function substr; |
@@ -89,39 +87,17 @@ public function relativeLink(string $destination): string |
89 | 87 | } |
90 | 88 |
|
91 | 89 | /** |
92 | | - * Gets a relative web link to the given file stored in the _site/media folder. |
93 | | - * |
94 | | - * An exception will be thrown if the file does not exist in the _media directory, |
95 | | - * and the second argument is set to true. |
96 | | - */ |
97 | | - public function mediaLink(string $destination, bool $validate = false): string |
98 | | - { |
99 | | - if ($validate && ! file_exists($sourcePath = "{$this->kernel->getMediaDirectory()}/$destination")) { |
100 | | - throw new FileNotFoundException($sourcePath); |
101 | | - } |
102 | | - |
103 | | - return $this->withCacheBusting($this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination"), $destination); |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * Gets a relative web link to the given file stored in the `_site/media` folder. |
108 | | - * If the image is already qualified (starts with `http`) it will be returned as is. |
| 90 | + * Gets a MediaAsset instance for the given file stored in the `_site/media` folder. |
| 91 | + * The returned value can be cast into a string in Blade views to resole the URL. |
109 | 92 | * |
110 | 93 | * If a base URL is configured, the image will be returned with a qualified absolute URL. |
| 94 | + * Otherwise, a relative path will be returned based on the rendered page's location. |
| 95 | + * |
| 96 | + * @throws FileNotFoundException If the file does not exist in the `_media` directory in order to make the issue clear. |
111 | 97 | */ |
112 | | - public function asset(string $name): string |
| 98 | + public function asset(string $name): MediaFile |
113 | 99 | { |
114 | | - if (static::isRemote($name)) { |
115 | | - return $name; |
116 | | - } |
117 | | - |
118 | | - $name = Str::start($name, "{$this->kernel->getMediaOutputDirectory()}/"); |
119 | | - |
120 | | - if ($this->hasSiteUrl()) { |
121 | | - return $this->withCacheBusting($this->url($name), $name); |
122 | | - } |
123 | | - |
124 | | - return $this->withCacheBusting($this->relativeLink($name), $name); |
| 100 | + return MediaFile::get($name); |
125 | 101 | } |
126 | 102 |
|
127 | 103 | /** |
@@ -181,12 +157,4 @@ public static function isRemote(string $url): bool |
181 | 157 | { |
182 | 158 | return str_starts_with($url, 'http') || str_starts_with($url, '//'); |
183 | 159 | } |
184 | | - |
185 | | - /** |
186 | | - * Apply cache to the URL if enabled in the configuration. |
187 | | - */ |
188 | | - protected function withCacheBusting(string $url, string $file): string |
189 | | - { |
190 | | - return $url.MediaFile::getCacheBustKey($file); |
191 | | - } |
192 | 160 | } |
0 commit comments