diff --git a/docs/configuration.md b/docs/configuration.md index 291db98..6dae992 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -69,6 +69,7 @@ Description: * **Bucket**: The bucket where you wish to store your objects. Every object in Amazon S3 is stored in a bucket. If the specified bucket doesn't exist Stapler will attempt to create it. The bucket name will not be interpolated. * **ACL**: This is a string/array that should be one of the canned access policies that S3 provides (private, public-read, public-read-write, authenticated-read, bucket-owner-read, bucket-owner-full-control). The default for Stapler is public-read. An associative array (style => permission) may be passed to specify permissions on a per style basis. * **path**: This is the key under the bucket in which the file will be stored. The URL will be constructed from the bucket and the path. This is what you will want to interpolate. Keys should be unique, like filenames, and despite the fact that S3 (strictly speaking) does not support directories, you can still use a / to separate parts of your file name. +* **s3_object_url_expires**: This is a string representing a default expiration time for object URLs. This adheres to the AWS expiration format (eg. +1 minutes) as described [here](http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl). This will almost always be used in conjunction with a 'private' ACL. Default values: * **s3_client_config** @@ -79,4 +80,5 @@ Default values: * **s3_object_config** * **Bucket**: '' * **ACL**: 'public-read' -* **path**: ':attachment/:id/:style/:filename' \ No newline at end of file +* **path**: ':attachment/:id/:style/:filename' +* **s3_object_url_expires**: null diff --git a/src/Attachment.php b/src/Attachment.php index 1afff77..2509e7b 100644 --- a/src/Attachment.php +++ b/src/Attachment.php @@ -298,7 +298,26 @@ public function __call($method, $parameters) public function url($styleName = '') { if ($this->originalFilename()) { - return $this->storageDriver->url($styleName, $this); + return $this->storageDriver->url($styleName, null, $this); + } + + return $this->defaultUrl($styleName); + } + + /** + * Generates a temporary url to an uploaded file (or a resized version of it). + * AWS Documentation for $expires format at + * http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl + * + * @param string $expires + * @param string $styleName + * + * @return string + */ + public function tempUrl($expires, $styleName = '') + { + if ($this->originalFilename()) { + return $this->storageDriver->url($styleName, $expires, $this); } return $this->defaultUrl($styleName); diff --git a/src/Interfaces/Attachment.php b/src/Interfaces/Attachment.php index 2d4ba7c..9c49665 100644 --- a/src/Interfaces/Attachment.php +++ b/src/Interfaces/Attachment.php @@ -134,6 +134,18 @@ public function setQueuedForDeletion($array); */ public function url($styleName = ''); + /** + * Generates a temporary url to an uploaded file (or a resized version of it). + * AWS Documentation for $expires format at + * http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl + * + * @param string $expires + * @param string $styleName + * + * @return string + */ + public function tempUrl($expires, $styleName = ''); + /** * Generates the file system path to an uploaded file (or a resized version of it). * This is used for saving files, etc. @@ -252,4 +264,4 @@ public function instanceWrite($property, $value); * attributes. */ public function clearAttributes(); -} \ No newline at end of file +} diff --git a/src/Interfaces/Storage.php b/src/Interfaces/Storage.php index 79221cd..7a55586 100644 --- a/src/Interfaces/Storage.php +++ b/src/Interfaces/Storage.php @@ -8,10 +8,11 @@ interface Storage * Return the url for a file upload. * * @param string $styleName + * @param string $expires * * @return string */ - public function url($styleName); + public function url($styleName, $expires = null); /** * For filesystem storage this method returns the path (on disk) of a file upload. @@ -39,4 +40,4 @@ public function remove(array $filePaths); * @param string $filePath */ public function move($file, $filePath); -} \ No newline at end of file +} diff --git a/src/Storage/Filesystem.php b/src/Storage/Filesystem.php index 75a0b02..e600e71 100644 --- a/src/Storage/Filesystem.php +++ b/src/Storage/Filesystem.php @@ -29,10 +29,11 @@ public function __construct(Attachment $attachedFile) * Return the url for a file upload. * * @param string $styleName + * @param string $expires * * @return string */ - public function url($styleName) + public function url($styleName, $expires = null) { return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->url, $this->attachedFile, $styleName); } diff --git a/src/Storage/S3.php b/src/Storage/S3.php index fb97026..52cc475 100644 --- a/src/Storage/S3.php +++ b/src/Storage/S3.php @@ -45,12 +45,17 @@ public function __construct(Attachment $attachedFile, S3Client $s3Client) * Return the url for a file upload. * * @param string $styleName + * @param string $expires * * @return string */ - public function url($styleName) + public function url($styleName, $expires = null) { - return $this->s3Client->getObjectUrl($this->attachedFile->s3_object_config['Bucket'], $this->path($styleName), null, ['PathStyle' => true]); + if (!$expires) { + $expires = $this->attachedFile->s3_object_url_expires; + } + + return $this->s3Client->getObjectUrl($this->attachedFile->s3_object_config['Bucket'], $this->path($styleName), $expires, ['PathStyle' => true]); } /**