Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit fba5c8f

Browse files
committed
Usage of URL routes for BucketInterface::getFileUrl() provided
1 parent a9d6a4b commit fba5c8f

File tree

11 files changed

+142
-51
lines changed

11 files changed

+142
-51
lines changed

BaseBucket.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Yii;
1111
use yii\base\Object;
12+
use yii\helpers\Url;
1213
use yii\log\Logger;
1314

1415
/**
@@ -29,7 +30,7 @@ abstract class BaseBucket extends Object implements BucketInterface
2930
/**
3031
* @var StorageInterface file storage, which owns the bucket.
3132
*/
32-
private $_storage = null;
33+
private $_storage;
3334

3435

3536
/**
@@ -86,4 +87,33 @@ public function getStorage()
8687
{
8788
return $this->_storage;
8889
}
90+
91+
/**
92+
* @inheritdoc
93+
*/
94+
public function getFileUrl($fileName)
95+
{
96+
$baseUrl = $this->getStorage()->getBaseUrl();
97+
if (is_array($baseUrl)) {
98+
$url = $baseUrl;
99+
$url['bucket'] = $this->getName();
100+
$url['filename'] = $fileName;
101+
return Url::to($url);
102+
}
103+
104+
return $this->composeFileUrl($baseUrl, $fileName);
105+
}
106+
107+
/**
108+
* Composes file URL from the base URL and filename.
109+
* This method is invoked at [[getFileUrl()]] in case base URL does not specify a URL route.
110+
* @param string|null $baseUrl storage base URL.
111+
* @param string $fileName self file name.
112+
* @return string file web URL.
113+
* @since 1.1.0
114+
*/
115+
protected function composeFileUrl($baseUrl, $fileName)
116+
{
117+
return $baseUrl . '/' . urlencode($this->getName()) . '/' . $fileName;
118+
}
89119
}

BaseStorage.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@
2121
*
2222
* @property BucketInterface[] $buckets list of buckets.
2323
* @property string $bucketClassName name of the bucket class.
24+
* @property string|array $baseUrl web URL, which is basic for all buckets at [[BucketInterface::getFileUrl()]].
2425
*
2526
* @author Paul Klimov <klimov.paul@gmail.com>
2627
* @since 1.0
2728
*/
2829
abstract class BaseStorage extends Component implements StorageInterface
2930
{
31+
/**
32+
* @var string name of the bucket class.
33+
*/
34+
public $bucketClassName = 'yii2tech\filestorage\BaseBucket';
35+
3036
/**
3137
* @var BucketInterface[] list of buckets.
3238
*/
3339
private $_buckets = [];
3440
/**
35-
* @var string name of the bucket class.
41+
* @var string|array web URL, which is basic for all buckets.
42+
* You can setup this field as array, which will be treated as a route specification for [[\yii\helpers\Url::to()]].
43+
* @since 1.1.0
3644
*/
37-
public $bucketClassName = 'yii2tech\filestorage\BaseBucket';
45+
private $_baseUrl;
3846

3947

4048
/**
@@ -153,4 +161,23 @@ public function hasBucket($bucketName)
153161
{
154162
return array_key_exists($bucketName, $this->_buckets);
155163
}
164+
165+
/**
166+
* @inheritdoc
167+
*/
168+
public function setBaseUrl($baseUrl)
169+
{
170+
if (is_string($baseUrl)) {
171+
$baseUrl = Yii::getAlias($baseUrl);
172+
}
173+
$this->_baseUrl = $baseUrl;
174+
}
175+
176+
/**
177+
* @inheritdoc
178+
*/
179+
public function getBaseUrl()
180+
{
181+
return $this->_baseUrl;
182+
}
156183
}

BucketSubDirTemplate.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace yii2tech\filestorage;
99

10-
use CException;
11-
use CFileHelper;
1210
use yii\base\Exception;
1311

1412
/**
@@ -37,7 +35,10 @@ abstract class BucketSubDirTemplate extends BaseBucket
3735
* For example:
3836
* if file name equal to 54321.tmp, placeholder {^name} will be resolved as "5", {^^name} - as "4" and so on.
3937
* Example value:
38+
*
39+
* ```
4040
* '{^name}/{^^name}'
41+
* ```
4142
*/
4243
public $fileSubDirTemplate = '';
4344

@@ -137,4 +138,19 @@ public function getFileNameWithSubDir($fileName)
137138
}
138139
return $fullFileName;
139140
}
140-
}
141+
142+
/**
143+
* @inheritdoc
144+
*/
145+
protected function composeFileUrl($baseUrl, $fileName)
146+
{
147+
$baseUrl = $baseUrl . '/' . urlencode($this->getName());
148+
149+
$fileSubDir = $this->getFileSubDir($fileName);
150+
if (!empty($fileSubDir)) {
151+
$baseUrl .= '/' . $fileSubDir;
152+
}
153+
154+
return $baseUrl . '/' . $fileName;
155+
}
156+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Yii 2 File Storage extension Change Log
55
-----------------------
66

77
- Enh #4: Added `yii2tech\filestorage\BucketInterface::openFile()` allowing to open file as a PHP resource (klimov-paul)
8+
- Enh #7: Usage of URL routes for `BucketInterface::getFileUrl()` provided (klimov-paul)
89

910

1011
1.0.1, April 25, 2016

StorageInterface.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,30 @@ public function addBucket($bucketName, $bucketData = []);
5252
* @return boolean success.
5353
*/
5454
public function hasBucket($bucketName);
55+
56+
/**
57+
* Sets the base URL, which should be used by [[BucketInterface::getFileUrl()]].
58+
* Example values:
59+
*
60+
* ```php
61+
* 'http://files.domain.com',
62+
* '//files.domain.com',
63+
* '@web/files',
64+
* ['/file/download'],
65+
* ```
66+
*
67+
* @param string|array|null $baseUrl web URL, which is basic for all buckets.
68+
* If string given, the URL will be composed by pattern: `{baseUrl}/{bucketName}/{fileName}`.
69+
* If array given, it is considered as a route for URL creation via [[\yii\web\UrlManager]],
70+
* bucket name will be added as `bucket` param, and file name will be added as `filename`.
71+
* @since 1.1.0
72+
*/
73+
public function setBaseUrl($baseUrl);
74+
75+
/**
76+
* Returns the base URL, which should be used by [[BucketInterface::getFileUrl()]].
77+
* @return string|array|null web URL, which is basic for all buckets.
78+
* @since 1.1.0
79+
*/
80+
public function getBaseUrl();
5581
}

amazon/Bucket.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,17 @@ public function moveFileInternal($srcFile, $destFile)
489489
/**
490490
* @inheritdoc
491491
*/
492-
public function getFileUrl($fileName)
492+
protected function composeFileUrl($baseUrl, $fileName)
493493
{
494-
if (!$this->exists()) {
495-
$this->create();
494+
if ($baseUrl === null) {
495+
if (!$this->exists()) {
496+
$this->create();
497+
}
498+
$fileName = $this->getFullFileName($fileName);
499+
$amazonS3 = $this->getStorage()->getAmazonS3();
500+
return $amazonS3->getObjectUrl($this->getUrlName(), $fileName);
496501
}
497-
$fileName = $this->getFullFileName($fileName);
498-
$amazonS3 = $this->getStorage()->getAmazonS3();
499-
return $amazonS3->getObjectUrl($this->getUrlName(), $fileName);
502+
return parent::composeFileUrl($baseUrl, $fileName);
500503
}
501504

502505
/**

local/Bucket.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,10 @@ public function moveFileInternal($srcFile, $destFile)
386386
}
387387

388388
/**
389-
* Gets web URL of the file.
390-
* @param string $fileName - self file name.
391-
* @return string file web URL.
389+
* @inheritdoc
392390
*/
393-
public function getFileUrl($fileName)
391+
protected function composeFileUrl($baseUrl, $fileName)
394392
{
395-
$baseUrl = $this->getStorage()->getBaseUrl();
396393
$baseUrl .= '/' . $this->getBaseSubPath();
397394
$fileSubDir = $this->getFileSubDir($fileName);
398395
if (!empty($fileSubDir)) {

local/Storage.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
* @see Bucket
3838
*
3939
* @property string $basePath file system path, which is basic for all buckets.
40-
* @property string $baseUrl web URL, which is basic for all buckets.
4140
* @method Bucket getBucket($bucketName)
4241
*
4342
* @author Paul Klimov <klimov.paul@gmail.com>
@@ -58,10 +57,6 @@ class Storage extends BaseStorage
5857
* @var string file system path, which is basic for all buckets.
5958
*/
6059
private $_basePath = '';
61-
/**
62-
* @var string web URL, which is basic for all buckets.
63-
*/
64-
private $_baseUrl = '';
6560

6661

6762
/**
@@ -79,20 +74,4 @@ public function getBasePath()
7974
{
8075
return $this->_basePath;
8176
}
82-
83-
/**
84-
* @param string $baseUrl web URL, which is basic for all buckets.
85-
*/
86-
public function setBaseUrl($baseUrl)
87-
{
88-
$this->_baseUrl = Yii::getAlias($baseUrl);
89-
}
90-
91-
/**
92-
* @return string web URL, which is basic for all buckets.
93-
*/
94-
public function getBaseUrl()
95-
{
96-
return $this->_baseUrl;
97-
}
9877
}

mongodb/Bucket.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,6 @@ public function moveFileInternal($srcFile, $destFile)
279279
return true;
280280
}
281281

282-
/**
283-
* Gets web URL of the file.
284-
* @param string $fileName - self file name.
285-
* @return string file web URL.
286-
*/
287-
public function getFileUrl($fileName)
288-
{
289-
// TODO: Implement getFileUrl() method.
290-
}
291-
292282
/**
293283
* @inheritdoc
294284
*/

tests/BucketTestTrait.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,33 @@ public function testMoveFileInternalDifferentBuckets()
359359
*/
360360
public function testGetFileUrl()
361361
{
362-
$bucket = $this->createFileStorageBucket();
363-
$testBucketName = 'test_get_file_url_bucket';
364-
$bucket->setName($testBucketName);
362+
$bucket = $this->createFileStorageBucket(['name' => 'test_get_file_url_bucket']);
363+
364+
$testFileName = 'test_file_name.tmp';
365+
$testFileContent = 'Test file content';
366+
$bucket->saveFileContent($testFileName, $testFileContent);
367+
368+
$returnedFileUrl = $bucket->getFileUrl($testFileName);
369+
$this->assertTrue(!empty($returnedFileUrl), 'File URL is empty!');
370+
}
371+
372+
/**
373+
* @depends testGetFileUrl
374+
*/
375+
public function testCreateFileUrl()
376+
{
377+
$bucket = $this->createFileStorageBucket(['name' => 'test_create_file_url_bucket']);
378+
$bucket->getStorage()->setBaseUrl(['/site/download']);
365379

366380
$testFileName = 'test_file_name.tmp';
367381
$testFileContent = 'Test file content';
368382
$bucket->saveFileContent($testFileName, $testFileContent);
369383

370384
$returnedFileUrl = $bucket->getFileUrl($testFileName);
371385
$this->assertTrue(!empty($returnedFileUrl), 'File URL is empty!');
386+
$this->assertContains(urlencode('site/download'), $returnedFileUrl, 'File URL does not contain route!');
387+
$this->assertContains($bucket->getName(), $returnedFileUrl, 'File URL does not contain bucket name!');
388+
$this->assertContains($testFileName, $returnedFileUrl, 'File URL does not contain filename!');
372389
}
373390

374391
/**

0 commit comments

Comments
 (0)