Skip to content

Commit

Permalink
Allow decompilation of local assets that match local URL
Browse files Browse the repository at this point in the history
wintercms/winter#552 introduced a change where all backend assets are converted to URLs to provide support for the `app.asset_url` configuration variable. This change allows asset decompilation to still work by converting a (local) URL back to a relative path. If a URL doesn't match the root URL, then it is passed through as is with no decompilation taking place.

Fixes wintercms/winter#562.
  • Loading branch information
bennothommo authored May 25, 2022
1 parent 734cfe8 commit 08c8e60
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions helpers/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ public function decompileAsset(string $file, bool $skinAsset = false)
{
$assets = $this->parseAsset($file, $skinAsset);

if (!$assets) {
// Return URL-based assets as is
if (starts_with($file, ['https://', 'http://'])) {
return [$file];
}

// Resolve relative asset paths
if ($skinAsset) {
$assetPath = base_path(substr(Skin::getActive()->getPath($file, true), 1));
} else {
$assetPath = base_path($file);
}
$relativePath = File::localToPublic(realpath($assetPath));

return [Url::asset($relativePath)];
}

return array_map(function ($asset) use ($skinAsset) {
// Resolve relative asset paths
if ($skinAsset) {
Expand All @@ -217,6 +234,15 @@ public function decompileAsset(string $file, bool $skinAsset = false)
*/
protected function parseAsset($file, $skinAsset)
{
if (starts_with($file, ['https://', 'http://'])) {
$rootUrl = Url::to('/');
if (!starts_with($file, $rootUrl)) {
return false;
}

$file = str_replace($rootUrl, '', $file);
}

if ($skinAsset) {
$assetFile = base_path(substr(Skin::getActive()->getPath($file, true), 1));
} else {
Expand Down

0 comments on commit 08c8e60

Please sign in to comment.