Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Fixing the S3 Path Builder.
Browse files Browse the repository at this point in the history
There was a missing / after the bucket and the file name was missing as well.
  • Loading branch information
Florian Krämer committed Jan 21, 2016
1 parent 9bece86 commit 11412d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Storage/PathBuilder/BasePathBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ protected function _randomPathSha1($string, $level) {
*/
public function ensureSlash($string, $position, $ds = null) {
if (!in_array($position, ['before', 'after', 'both'])) {
throw new \InvalidArgumentException(sprintf('Invalid position `%s`!', $position));
$method = get_class($this) . '::ensureSlash(): ';
throw new \InvalidArgumentException(sprintf($method . 'Invalid position `%s`!', $position));
}
if (is_null($ds)) {
$ds = DS;
Expand Down
24 changes: 22 additions & 2 deletions src/Storage/PathBuilder/S3PathBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,40 @@
use Burzum\FileStorage\Storage\StorageManager;
use Cake\Datasource\EntityInterface;

/**
* The S3 Path Builder.
*/
class S3PathBuilder extends BasePathBuilder {

/**
* {@inheritdoc}
*/
public function __construct(array $config = []) {
$this->_defaultConfig['https'] = false;
$this->_defaultConfig['modelFolder'] = true;
$this->_defaultConfig['s3Url'] = 's3.amazonaws.com';
parent::__construct($config);
}

/**
* Gets the bucket from the adapter configuration.
*
* @param string Storage adapter config name.
* @return string
*/
protected function _getBucket($adapter) {
$config = StorageManager::config($adapter);
return $config['adapterOptions'][1];
}

/**
* Builds the cloud base URL for the given bucket and location.
*
* @param string $bucket
* @param string $bucketPrefix
* @param string $cfDist
* @return string
*/
protected function _buildCloudUrl($bucket, $bucketPrefix = null, $cfDist = null) {
$path = $this->config('https') === true ? 'https://' : 'http://';
if ($cfDist) {
Expand All @@ -49,9 +69,9 @@ protected function _buildCloudUrl($bucket, $bucketPrefix = null, $cfDist = null)
*/
public function url(EntityInterface $entity, array $options = []) {
$bucket = $this->_getBucket($entity->adapter);
$pathPrefix = $this->_buildCloudUrl($bucket);
$pathPrefix = $this->ensureSlash($this->_buildCloudUrl($bucket), 'after');
$path = parent::path($entity);
$path = str_replace('\\', '/', $path);
return $pathPrefix . $path;
return $pathPrefix . $path . $this->filename($entity, $options);
}
}

0 comments on commit 11412d2

Please sign in to comment.