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

Fixed _buildAmazonS3Path in ImageProcessingListener #123

Merged
merged 1 commit into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Event/ImageProcessingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ protected function _buildAwsS3Path($Event) {
protected function _buildAmazonS3Path(Event $Event) {
extract($Event->data);

$path = $this->_buildPath($image, true, $hash);
$image['path'] = '/' . $path;
$path = '/' . $this->_buildPath($image, true, $hash);

$config = StorageManager::config($Event->data['image']['adapter']);
$bucket = $config['adapterOptions'][1];
Expand All @@ -367,10 +366,10 @@ protected function _buildAmazonS3Path(Event $Event) {
$http = 'https';
}

$image['path'] = str_replace('\\', '/', $image['path']);
$path = str_replace('\\', '/', $path);
$bucketPrefix = !empty($Event->data['options']['bucketPrefix']) && $Event->data['options']['bucketPrefix'] === true;

$Event->data['path'] = $Event->result = $this->_buildCloudFrontDistributionUrl($http, $image['path'], $bucket, $bucketPrefix, $cfDist);
$Event->data['path'] = $Event->result = $this->_buildCloudFrontDistributionUrl($http, $path, $bucket, $bucketPrefix, $cfDist);
$Event->stopPropagation();
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Fixture/FileStorageFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ class FileStorageFixture extends TestFixture {
'adapter' => 'Local',
'created' => '2012-01-01 12:00:00',
'modified' => '2012-01-01 12:00:00',
),
array(
'id' => 'file-storage-4',
'user_id' => 'user-1',
'foreign_key' => 'item-4',
'model' => 'Item',
'filename' => 'titus.jpg',
'filesize' => '335872',
'mime_type' => 'image/jpg',
'extension' => 'jpg',
'hash' => '09d82a31',
'path' => null,
'adapter' => 'S3',
'created' => '2012-01-01 12:00:00',
'modified' => '2012-01-01 12:00:00',
)
);
}
44 changes: 44 additions & 0 deletions tests/TestCase/Event/ImageProcessingListenerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Burzum\FileStorage\Test\TestCase\Event;

use Burzum\FileStorage\Storage\StorageManager;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Burzum\FileStorage\TestSuite\FileStorageTestCase;
use Burzum\FileStorage\Event\ImageProcessingListener;
Expand All @@ -10,6 +12,9 @@ class TestImageProcessingListener extends ImageProcessingListener {
public function buildPath($image, $extension = true, $hash = null) {
return $this->_buildPath($image, $extension, $hash);
}
public function buildAwsS3Path($Event) {
$this->_buildAwsS3Path($Event);
}
}

/**
Expand Down Expand Up @@ -88,4 +93,43 @@ public function testBuildPath() {
), true, '5gh2hf');
$this->assertEquals($result, '/xx/xx/xx/uuid/foobar.5gh2hf.jpg');
}

/**
* testBuildAwsS3Path
*
* @return void
*/
public function testBuildAwsS3Path() {
$this->Listener = new TestImageProcessingListener(array(
'preserveFilename' => true,
));

$image = $this->FileStorage->get('file-storage-4');

StorageManager::config($image->get('adapter'), [
'adapterOptions' => [null, 'bucket1'],
]);

$eventOptions = [
'hash' => 'abc',
'image' => $image,
'version' => 'small',
'options' => [],
'pathType' => 'url'
];

$event = new Event('FileStorage.ImageHelper.imagePath', $this, $eventOptions);

$expected = 'http://s3.amazonaws.com/bucket1/titus.abc.jpg';

$this->Listener->buildAwsS3Path($event);
$this->assertEquals($expected, $event->data['path']);

// Make sure it returns same path if called more than once
$this->Listener->buildAwsS3Path($event);
$this->assertEquals($expected, $event->data['path']);

// Make sure it doesn't change path property
$this->assertNull($image->get('path'));
}
}