2018-03-27 05:44:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Helpers;
|
|
|
|
|
2019-12-22 23:41:38 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2018-03-27 05:44:14 +00:00
|
|
|
use Illuminate\Filesystem\FilesystemManager;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
2022-10-31 16:29:10 +00:00
|
|
|
use Pterodactyl\Exceptions\ManifestDoesNotExistException;
|
2018-03-27 05:44:14 +00:00
|
|
|
|
|
|
|
class AssetHashService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Location of the manifest file generated by gulp.
|
|
|
|
*/
|
|
|
|
public const MANIFEST_PATH = './assets/manifest.json';
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private Filesystem $filesystem;
|
2018-03-27 05:44:14 +00:00
|
|
|
|
2022-10-14 17:00:10 +00:00
|
|
|
protected static mixed $manifest = null;
|
2018-03-27 05:44:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AssetHashService constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(FilesystemManager $filesystem)
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
|
|
|
$this->filesystem = $filesystem->createLocalDriver(['root' => public_path()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify a URL to append the asset hash.
|
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
public function url(string $resource): string
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
|
|
|
$file = last(explode('/', $resource));
|
2019-12-22 23:41:38 +00:00
|
|
|
$data = Arr::get($this->manifest(), $file) ?? $file;
|
2018-03-27 05:44:14 +00:00
|
|
|
|
2019-12-22 23:41:38 +00:00
|
|
|
return str_replace($file, Arr::get($data, 'src') ?? $file, $resource);
|
2018-06-04 02:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the data integrity hash for a resource.
|
|
|
|
*/
|
|
|
|
public function integrity(string $resource): string
|
|
|
|
{
|
|
|
|
$file = last(explode('/', $resource));
|
|
|
|
$data = array_get($this->manifest(), $file, $file);
|
|
|
|
|
2019-12-22 23:41:38 +00:00
|
|
|
return Arr::get($data, 'integrity') ?? '';
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 20:52:11 +00:00
|
|
|
* Return a built CSS import using the provided URL.
|
2018-03-27 05:44:14 +00:00
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
public function css(string $resource): string
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
2020-09-02 02:37:05 +00:00
|
|
|
$attributes = [
|
|
|
|
'href' => $this->url($resource),
|
|
|
|
'rel' => 'stylesheet preload',
|
|
|
|
'as' => 'style',
|
|
|
|
'crossorigin' => 'anonymous',
|
|
|
|
'referrerpolicy' => 'no-referrer',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (config('pterodactyl.assets.use_hash')) {
|
|
|
|
$attributes['integrity'] = $this->integrity($resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '<link';
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
|
|
$output .= " $key=\"$value\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output . '>';
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 20:52:11 +00:00
|
|
|
* Return a built JS import using the provided URL.
|
2018-03-27 05:44:14 +00:00
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
public function js(string $resource): string
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
2020-09-02 02:37:05 +00:00
|
|
|
$attributes = [
|
|
|
|
'src' => $this->url($resource),
|
|
|
|
'crossorigin' => 'anonymous',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (config('pterodactyl.assets.use_hash')) {
|
|
|
|
$attributes['integrity'] = $this->integrity($resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '<script';
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
|
|
$output .= " $key=\"$value\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output . '></script>';
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 20:52:11 +00:00
|
|
|
* Get the asset manifest and store it in the cache for quicker lookups.
|
2018-03-27 05:44:14 +00:00
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
protected function manifest(): array
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
2022-10-14 17:00:10 +00:00
|
|
|
if (static::$manifest === null) {
|
|
|
|
self::$manifest = json_decode(
|
|
|
|
$this->filesystem->get(self::MANIFEST_PATH),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:29:10 +00:00
|
|
|
$manifest = static::$manifest;
|
|
|
|
if ($manifest === null) {
|
|
|
|
throw new ManifestDoesNotExistException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $manifest;
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
}
|