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

Commit 62fee83

Browse files
committed
Added yii2tech\filestorage\BucketInterface::openFile()
1 parent 7afab0a commit 62fee83

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

BucketInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,16 @@ public function moveFileInternal($srcFile, $destFile);
155155
* @return string file web URL.
156156
*/
157157
public function getFileUrl($fileName);
158+
159+
/**
160+
* Opens a file as stream resource, e.g. like `fopen()` function.
161+
* @param string $fileName - file name.
162+
* @param string $mode - the type of access you require to the stream, e.g. `r`, `w`, `a` and so on.
163+
* You should prefer usage of simple modes like `r` and `w`, avoiding complex ones like `w+`, as they
164+
* may not supported by some storages.
165+
* @param resource|null $context - stream context to be used.
166+
* @return resource file pointer resource on success, or `false` on error.
167+
* @since 1.1.0
168+
*/
169+
public function openFile($fileName, $mode, $context = null);
158170
}

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Yii 2 File Storage extension Change Log
22
=======================================
33

4+
1.1.0 under development
5+
-----------------------
6+
7+
- Enh #4: Added `yii2tech\filestorage\BucketInterface::openFile()` allowing to open file as a PHP resource (klimov-paul)
8+
9+
410
1.0.1, April 25, 2016
511
---------------------
612

amazon/Bucket.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,30 @@ public function getFileUrl($fileName)
499499
return $amazonS3->getObjectUrl($this->getUrlName(), $fileName);
500500
}
501501

502+
/**
503+
* @inheritdoc
504+
*/
505+
public function openFile($fileName, $mode, $context = null)
506+
{
507+
$this->getStorage()->registerStreamWrapper();
508+
509+
$streamPath = 's3://' . $this->getUrlName() . '/' . $fileName;
510+
511+
if ($mode === 'r' && func_num_args() < 3) {
512+
$context = stream_context_create([
513+
's3' => [
514+
'seekable' => true
515+
]
516+
]);
517+
}
518+
519+
if ($context === null) {
520+
// avoid PHP warning: fopen() expects parameter 4 to be resource, null given
521+
return fopen($streamPath, $mode);
522+
}
523+
return fopen($streamPath, $mode, null, $context);
524+
}
525+
502526
// Batch files upload :
503527

504528
/**

amazon/Storage.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ class Storage extends BaseStorage
6767
* If constant 'AWS_SECRET_KEY' has been defined, this field can be left blank.
6868
*/
6969
public $awsSecretKey = '';
70+
7071
/**
7172
* @var S3Client instance of the Amazon S3 client.
7273
*/
7374
private $_amazonS3;
75+
/**
76+
* @var boolean whether `s3` stream wrapper has been already registered.
77+
*/
78+
private $streamWrapperRegistered = false;
7479

7580

7681
/**
@@ -108,4 +113,17 @@ protected function createAmazonS3()
108113
];
109114
return S3Client::factory($amazonS3Options);
110115
}
116+
117+
/**
118+
* Registers Amazon S3 stream wrapper for the `s3` protocol.
119+
* @param boolean $force whether to enforce registration even wrapper has been already registered.
120+
* @since 1.1.0
121+
*/
122+
public function registerStreamWrapper($force = false)
123+
{
124+
if ($force || !$this->streamWrapperRegistered) {
125+
$this->getAmazonS3()->registerStreamWrapper();
126+
$this->streamWrapperRegistered = true;
127+
}
128+
}
111129
}

local/Bucket.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,17 @@ public function getFileUrl($fileName)
401401
$fileUrl = $baseUrl . '/' . $fileName;
402402
return $fileUrl;
403403
}
404+
405+
/**
406+
* @inheritdoc
407+
*/
408+
public function openFile($fileName, $mode, $context = null)
409+
{
410+
$fullFileName = $this->resolveFullFileName($fileName);
411+
if ($context === null) {
412+
// avoid PHP warning: fopen() expects parameter 4 to be resource, null given
413+
return fopen($fullFileName, $mode);
414+
}
415+
return fopen($fullFileName, $mode, null, $context);
416+
}
404417
}

tests/amazon/BucketTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,35 @@ public function testGetFileUrl()
459459
$this->assertEquals( $expectedFileUrl, $returnedFileUrl, 'Wrong file URL returned!' );*/
460460
}
461461

462+
/**
463+
* @depends testGetFileContent
464+
*/
465+
public function testOpenFile()
466+
{
467+
$bucket = $this->createFileStorageBucket('test-bucket-open-file');
468+
469+
$testFileName = 'test_read_file_name.tmp';
470+
$testFileContent = 'Test read file content';
471+
$bucket->saveFileContent($testFileName, $testFileContent);
472+
473+
$resource = $bucket->openFile($testFileName, 'r');
474+
$this->assertTrue(is_resource($resource));
475+
476+
$this->assertEquals($testFileContent, stream_get_contents($resource));
477+
fclose($resource);
478+
479+
$testFileName = 'test_write_file_name.tmp';
480+
$testFileContent = 'Test write file content';
481+
482+
$resource = $bucket->openFile($testFileName, 'w');
483+
$this->assertTrue(is_resource($resource));
484+
485+
fwrite($resource, $testFileContent);
486+
fclose($resource);
487+
488+
$this->assertEquals($testFileContent, $bucket->getFileContent($testFileName));
489+
}
490+
462491
/**
463492
* @depends testSaveFileContent
464493
*/

tests/local/BucketTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,35 @@ public function testSaveFileNameWithDirSeparator()
483483
$bucketFileName = $bucket->getFullFileName($testFileName);
484484
$this->assertTrue(file_exists($bucketFileName), 'Unable to create file with name, containing dir separator, in the bucket!');
485485
}
486+
487+
/**
488+
* @depends testGetFileContent
489+
*/
490+
public function testOpenFile()
491+
{
492+
$bucket = $this->createFileStorageBucket();
493+
$testBucketName = 'test_get_file_content_bucket';
494+
$bucket->setName($testBucketName);
495+
496+
$testFileName = 'test_read_file_name.tmp';
497+
$testFileContent = 'Test read file content';
498+
$bucket->saveFileContent($testFileName, $testFileContent);
499+
500+
$resource = $bucket->openFile($testFileName, 'r');
501+
$this->assertTrue(is_resource($resource));
502+
503+
$this->assertEquals($testFileContent, stream_get_contents($resource));
504+
fclose($resource);
505+
506+
$testFileName = 'test_write_file_name.tmp';
507+
$testFileContent = 'Test write file content';
508+
509+
$resource = $bucket->openFile($testFileName, 'w');
510+
$this->assertTrue(is_resource($resource));
511+
512+
fwrite($resource, $testFileContent);
513+
fclose($resource);
514+
515+
$this->assertEquals($testFileContent, $bucket->getFileContent($testFileName));
516+
}
486517
}

0 commit comments

Comments
 (0)