Skip to content

Commit

Permalink
Added support for volume subpaths (#79)
Browse files Browse the repository at this point in the history
* Added support for volume subpaths

* 4.0.2 release
  • Loading branch information
joeforshaw authored Mar 29, 2024
1 parent cb0564d commit 554bde3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
15 changes: 6 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
# Release Notes for Servd Assets and Helpers

## 4.0.4 - 2024-03-13
## 4.0.2 - 2024-03-29

### Added

- Support for Craft 5 volume subpaths

### Updated

- Moved image do-not-upscale logic over to asset platform

## 4.0.3 - 2024-03-12
- Merged recent v3.x plugin changes

### Fixed

- Fixed a bug when purging static cache URLs with no defined path

## 4.0.2 - 2024-03-11

### Updated

- Merged recent v3.x plugin changes

## 4.0.1 - 2024-03-08

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "servd/craft-asset-storage",
"description": "Servd Asset Storage and Helpers integration for Craft CMS",
"version": "4.0.4",
"version": "4.0.2",
"type": "craft-plugin",
"keywords": [
"cms",
Expand Down
11 changes: 8 additions & 3 deletions src/AssetsPlatform/AssetsPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,13 @@ public function getFileUrl(Asset $asset)
{

$settings = Plugin::$plugin->getSettings();
$fs = $asset->getVolume()->getFs();
$volume = $asset->getVolume();
$fs = $volume->getFs();


$normalizedCustomSubfolder = App::parseEnv($fs->customSubfolder);
$normalizedSubpath = trim(App::parseEnv($volume->getSubpath()) , "/");
$normalizedSubpath = strlen($normalizedSubpath) > 0 ? $normalizedSubpath . '/' : '';

//Special handling for videos
$assetIsVideo = AssetsHelper::getFileKindByExtension($asset->filename) === Asset::KIND_VIDEO
Expand All @@ -298,6 +302,7 @@ public function getFileUrl(Asset $asset)
return 'https://servd-' . $settings->getProjectSlug() . '.b-cdn.net/' .
$settings->getAssetsEnvironment() . '/' .
(strlen(trim($normalizedCustomSubfolder, "/")) > 0 ? (trim($normalizedCustomSubfolder, "/") . '/') : '') .
$normalizedSubpath .
$asset->getPath();
}

Expand All @@ -308,7 +313,7 @@ public function getFileUrl(Asset $asset)
"environment" => $settings->getAssetsEnvironment(),
"projectSlug" => $settings->getProjectSlug(),
"subfolder" => trim($normalizedCustomSubfolder, "/"),
"filePath" => $asset->getPath(),
"filePath" => $normalizedSubpath . $asset->getPath(),
];
$finalUrl = $customPattern;
foreach ($variables as $key => $value) {
Expand All @@ -317,7 +322,7 @@ public function getFileUrl(Asset $asset)
//Apply rawurlencode to match AssetsHelper::generateUrl behaviour
$urlParts = parse_url($finalUrl);
$finalUrl = $urlParts['scheme'] . '://' . $urlParts['host'] . implode('/', array_map('rawurlencode', explode('/', $urlParts['path'])));

} else {
$finalUrl = AssetsHelper::generateUrl($asset);
}
Expand Down
15 changes: 11 additions & 4 deletions src/AssetsPlatform/ImageTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function transformUrl(Asset $asset, TransformOptions $transform)
{

$settings = Plugin::$plugin->getSettings();
$fs = $asset->getVolume()->getFs();
$volume = $asset->getVolume();
$fs = $volume->getFs();

if (get_class($fs) !== Fs::class) {
return null;
Expand All @@ -50,6 +51,8 @@ public function transformUrl(Asset $asset, TransformOptions $transform)
$params['s'] = $signingKey;

$normalizedCustomSubfolder = App::parseEnv($fs->customSubfolder);
$normalizedSubpath = trim(App::parseEnv($volume->getSubpath()) , "/");
$normalizedSubpath = strlen($normalizedSubpath) > 0 ? $normalizedSubpath . '/' : '';

// Use a custom URL template if one has been provided
$customPattern = App::parseEnv($fs->optimiseUrlPattern);
Expand All @@ -58,7 +61,7 @@ public function transformUrl(Asset $asset, TransformOptions $transform)
"environment" => $settings->getAssetsEnvironment(),
"projectSlug" => $settings->getProjectSlug(),
"subfolder" => trim($normalizedCustomSubfolder, "/"),
"filePath" => $this->encodeFilenameInFilePath($asset->getPath()),
"filePath" => $this->encodeFilenameInFilePath($normalizedSubpath . $asset->getPath()),
"params" => '?' . http_build_query($params),
];
$finalUrl = $customPattern;
Expand Down Expand Up @@ -106,10 +109,14 @@ public function getFullPathForAssetAndTransform(Asset $asset, $params)
return;
}

$volume = $asset->getVolume();
/** @var \servd\AssetStorage\AssetsPlatform\Fs */
$fs = $asset->getVolume()->getFs();
$fs = $volume->getFs();

$filePath = $this->encodeFilenameInFilePath($asset->getPath());
$normalizedSubpath = trim(App::parseEnv($volume->getSubpath()) , "/");
$normalizedSubpath = strlen($normalizedSubpath) > 0 ? $normalizedSubpath . '/' : '';

$filePath = $this->encodeFilenameInFilePath($normalizedSubpath . $asset->getPath());

$base = rtrim($fs->_subfolder(), '/') . '/';
$base = ltrim($base, '/');
Expand Down

0 comments on commit 554bde3

Please sign in to comment.