-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add AmazonS3Resolver and ResolverInterface::remove #66
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Imagine\Cache\Resolver; | ||
|
||
use \AmazonS3; | ||
|
||
use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface, | ||
Liip\ImagineBundle\Imagine\Cache\CacheManager; | ||
|
||
use Symfony\Component\HttpFoundation\Request, | ||
Symfony\Component\HttpFoundation\RedirectResponse, | ||
Symfony\Component\HttpFoundation\Response; | ||
|
||
class AmazonS3Resolver implements ResolverInterface, CacheManagerAwareInterface | ||
{ | ||
/** | ||
* @var AmazonS3 | ||
*/ | ||
protected $storage; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $bucket; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $acl; | ||
|
||
/** | ||
* @var CacheManager | ||
*/ | ||
protected $cacheManager; | ||
|
||
/** | ||
* Constructs a cache resolver storing images on Amazon S3. | ||
* | ||
* @throws \S3_Exception While checking for existence of the bucket. | ||
* | ||
* @param \AmazonS3 $storage The Amazon S3 storage API. It's required to know authentication information. | ||
* @param string $bucket The bucket name to operate on. | ||
* @param string $acl The ACL to use when storing new objects. Default: owner read/write, public read | ||
*/ | ||
public function __construct(AmazonS3 $storage, $bucket, $acl = AmazonS3::ACL_PUBLIC) | ||
{ | ||
$this->storage = $storage; | ||
$this->storage->if_bucket_exists($bucket); | ||
|
||
$this->bucket = $bucket; | ||
$this->acl = $acl; | ||
} | ||
|
||
/** | ||
* @param CacheManager $cacheManager | ||
*/ | ||
public function setCacheManager(CacheManager $cacheManager) | ||
{ | ||
$this->cacheManager = $cacheManager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function resolve(Request $request, $path, $filter) | ||
{ | ||
return $this->getObjectPath($path, $filter); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function store(Response $response, $targetPath, $filter) | ||
{ | ||
$storageResponse = $this->storage->create_object($this->bucket, $targetPath, array( | ||
'body' => $response->getContent(), | ||
'contentType' => $response->headers->get('Content-Type'), | ||
'length' => strlen($response->getContent()), | ||
'acl' => $this->acl, | ||
)); | ||
|
||
if ($storageResponse->isOK()) { | ||
$response->setStatusCode(301); | ||
$response->headers->set('Location', $this->storage->get_object_url($this->bucket, $targetPath)); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getBrowserPath($targetPath, $filter, $absolute = false) | ||
{ | ||
$objectPath = $this->getObjectPath($targetPath, $filter); | ||
if ($this->storage->if_object_exists($this->bucket, $objectPath)) { | ||
return $this->storage->get_object_url($this->bucket, $objectPath); | ||
} | ||
|
||
$params = array('path' => ltrim($targetPath, '/')); | ||
|
||
return str_replace( | ||
urlencode($params['path']), | ||
urldecode($params['path']), | ||
$this->cacheManager->getRouter()->generate('_imagine_'.$filter, $params, $absolute) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function remove($targetPath, $filter) | ||
{ | ||
$objectPath = $this->getObjectPath($targetPath, $filter); | ||
if (!$this->storage->if_object_exists($this->bucket, $objectPath)) { | ||
// A non-existing object to delete: done! | ||
return true; | ||
} | ||
|
||
return $this->storage->delete_object($this->bucket, $objectPath)->isOK(); | ||
} | ||
|
||
/** | ||
* Returns the object path within the bucket. | ||
* | ||
* @param string $path The base path of the resource. | ||
* @param string $filter The name of the imagine filter in effect. | ||
* | ||
* @return string The path of the object on S3. | ||
*/ | ||
protected function getObjectPath($path, $filter) | ||
{ | ||
return $filter.'/'.$path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't this method be moved to
AbstractFilesystemResolver
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it can; will update it.