Skip to content

Commit bde0639

Browse files
committed
Merge pull request #1932 from hydephp/improve-the-media-file-instance-api
[2.x] Improve the media file instance API
2 parents 620c972 + ff18348 commit bde0639

34 files changed

+1054
-239
lines changed

docs/_data/partials/hyde-pages-api/hyde-kernel-hyperlink-methods.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<section id="hyde-kernel-hyperlink-methods">
22

33
<!-- Start generated docs for Hyde\Foundation\Concerns\ForwardsHyperlinks -->
4-
<!-- Generated by HydePHP DocGen script at 2024-07-26 18:20:22 in 0.13ms -->
4+
<!-- Generated by HydePHP DocGen script at 2024-09-04 19:44:45 in 0.10ms -->
55

66
#### `formatLink()`
77

@@ -19,20 +19,12 @@ No description provided.
1919
Hyde::relativeLink(string $destination): string
2020
```
2121

22-
#### `mediaLink()`
23-
24-
No description provided.
25-
26-
```php
27-
Hyde::mediaLink(string $destination, bool $validate): string
28-
```
29-
3022
#### `asset()`
3123

3224
No description provided.
3325

3426
```php
35-
Hyde::asset(string $name): string
27+
Hyde::asset(string $name): Hyde\Support\Filesystem\MediaFile
3628
```
3729

3830
#### `url()`

docs/architecture-concepts/the-hydekernel.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,6 @@ No description provided.
205205
Hyde::relativeLink(string $destination): string
206206
```
207207

208-
#### `mediaLink()`
209-
210-
No description provided.
211-
212-
```php
213-
Hyde::mediaLink(string $destination, bool $validate): string
214-
```
215-
216208
#### `asset()`
217209

218210
No description provided.

ide.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@
1515
}
1616
]
1717
},
18+
{
19+
"complete": "directoryFiles",
20+
"options": {
21+
"directory": "/_media"
22+
},
23+
"condition": [
24+
{
25+
"methodNames": ["get", "hasMediaFile"],
26+
"place": "parameter",
27+
"classFqn": ["\\Hyde\\Facades\\Asset"],
28+
"parameters": [1]
29+
},
30+
{
31+
"methodNames": ["__construct", "get", "make", "sourcePath", "outputPath"],
32+
"place": "parameter",
33+
"classFqn": ["Hyde\\Support\\Filesystem\\MediaFile"],
34+
"parameters": [1]
35+
}, {
36+
"methodNames": ["asset"],
37+
"place": "parameter",
38+
"classFqn": ["Hyde\\Foundation\\HydeKernel", "\\Hyde\\Hyde", "Hyde"],
39+
"parameters": [1]
40+
}, {
41+
"functionNames": ["asset"],
42+
"place": "parameter",
43+
"parameters": [1]
44+
}
45+
]
46+
},
1847
{
1948
"complete": "configKey",
2049
"condition": [

packages/framework/resources/views/layouts/scripts.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{-- The compiled Laravel Mix scripts --}}
22
@if(Asset::hasMediaFile('app.js'))
3-
<script defer src="{{ Asset::mediaLink('app.js') }}"></script>
3+
<script defer src="{{ Asset::get('app.js') }}"></script>
44
@endif
55

66
{{-- Alpine.js --}}

packages/framework/resources/views/layouts/styles.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@if(config('hyde.load_app_styles_from_cdn', false))
66
<link rel="stylesheet" href="{{ HydeFront::cdnLink('app.css') }}">
77
@elseif(Asset::hasMediaFile('app.css'))
8-
<link rel="stylesheet" href="{{ Asset::mediaLink('app.css') }}">
8+
<link rel="stylesheet" href="{{ Asset::get('app.css') }}">
99
@endif
1010

1111
{{-- Dynamic TailwindCSS Play CDN --}}

packages/framework/src/Facades/Asset.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@
1717
*/
1818
class Asset
1919
{
20-
public static function get(string $file): string
20+
public static function get(string $file): MediaFile
2121
{
2222
return hyde()->asset($file);
2323
}
2424

25-
public static function mediaLink(string $file): string
26-
{
27-
return hyde()->mediaLink($file);
28-
}
29-
3025
public static function hasMediaFile(string $file): bool
3126
{
3227
return file_exists(MediaFile::sourcePath($file));

packages/framework/src/Facades/Filesystem.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,42 @@ public static function putContents(string $path, string $contents, bool $lock =
129129
return self::put($path, $contents, $lock);
130130
}
131131

132+
/**
133+
* Improved mime type detection for the given path that can be relative to the project root,
134+
* or a remote URL where this will try to guess the mime type based on the file extension.
135+
*
136+
* @param string $path The path to the file, relative to the project root or a remote URL.
137+
*/
138+
public static function findMimeType(string $path): string
139+
{
140+
$extension = self::extension($path);
141+
142+
$lookup = [
143+
'txt' => 'text/plain',
144+
'md' => 'text/markdown',
145+
'html' => 'text/html',
146+
'css' => 'text/css',
147+
'svg' => 'image/svg+xml',
148+
'png' => 'image/png',
149+
'jpg' => 'image/jpeg',
150+
'jpeg' => 'image/jpeg',
151+
'gif' => 'image/gif',
152+
'json' => 'application/json',
153+
'js' => 'application/javascript',
154+
'xml' => 'application/xml',
155+
];
156+
157+
if (isset($lookup[$extension])) {
158+
return $lookup[$extension];
159+
}
160+
161+
if (extension_loaded('fileinfo') && self::exists($path)) {
162+
return self::mimeType($path);
163+
}
164+
165+
return 'text/plain';
166+
}
167+
132168
protected static function filesystem(): \Illuminate\Filesystem\Filesystem
133169
{
134170
return app(\Illuminate\Filesystem\Filesystem::class);

packages/framework/src/Foundation/Concerns/ForwardsHyperlinks.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hyde\Foundation\Concerns;
66

77
use Hyde\Support\Models\Route;
8+
use Hyde\Support\Filesystem\MediaFile;
89

910
/**
1011
* @internal Single-use trait for the HydeKernel class.
@@ -23,12 +24,7 @@ public function relativeLink(string $destination): string
2324
return $this->hyperlinks->relativeLink($destination);
2425
}
2526

26-
public function mediaLink(string $destination, bool $validate = false): string
27-
{
28-
return $this->hyperlinks->mediaLink($destination, $validate);
29-
}
30-
31-
public function asset(string $name): string
27+
public function asset(string $name): MediaFile
3228
{
3329
return $this->hyperlinks->asset($name);
3430
}

packages/framework/src/Foundation/Kernel/Hyperlinks.php

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
use Hyde\Foundation\HydeKernel;
1111
use Hyde\Support\Filesystem\MediaFile;
1212
use Hyde\Framework\Exceptions\FileNotFoundException;
13-
use Illuminate\Support\Str;
1413

1514
use function str_ends_with;
1615
use function str_starts_with;
1716
use function substr_count;
18-
use function file_exists;
1917
use function str_replace;
2018
use function str_repeat;
2119
use function substr;
@@ -89,39 +87,17 @@ public function relativeLink(string $destination): string
8987
}
9088

9189
/**
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.
10992
*
11093
* 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.
11197
*/
112-
public function asset(string $name): string
98+
public function asset(string $name): MediaFile
11399
{
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);
125101
}
126102

127103
/**
@@ -181,12 +157,4 @@ public static function isRemote(string $url): bool
181157
{
182158
return str_starts_with($url, 'http') || str_starts_with($url, '//');
183159
}
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-
}
192160
}

packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getSource(): string
8383
{
8484
if ($this->type === self::TYPE_LOCAL) {
8585
// Return value is always resolvable from a compiled page in the _site directory.
86-
return Hyde::mediaLink($this->source);
86+
return (string) Hyde::asset($this->source);
8787
}
8888

8989
return $this->source;

0 commit comments

Comments
 (0)