Created
September 30, 2013 23:59
-
-
Save datibbaw/6772059 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Class AwsUploadPolicy | |
* @see http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTForms.html | |
* @package Urahara | |
*/ | |
class AwsUploadPolicy implements \JsonSerializable | |
{ | |
const ACL_PRIVATE = 'private'; | |
const ACL_PUBLIC_READ = 'public-read'; | |
const ACL_PUBLIC_RW = 'public-read-write'; | |
const ACL_AUTH_READ = 'authenticated-read'; | |
const ACL_OWNER_READ = 'bucket-owner-read'; | |
const ACL_OWNER_FULL = 'bucket-owner-full-control'; | |
const MATCH_EXACT = 'eq'; | |
const MATCH_PREFIX = 'starts-with'; | |
private static $BUCKET = 'bucket'; | |
private static $ACL = 'acl'; | |
private static $KEY = 'key'; | |
private static $CONTENT_LENGTH_RANGE = 'content-length-range'; | |
private static $CONTENT_TYPE = 'Content-Type'; | |
/** | |
* @var \DateTime | |
*/ | |
private $expiry; | |
private $conditions = array(); | |
public function __construct(\DateTime $expiry) | |
{ | |
$this->expiry = $expiry; | |
} | |
public function setBucket($value) | |
{ | |
$this->addExactMatchCondition(self::$BUCKET, $value); | |
} | |
public function setACL($value) | |
{ | |
$this->addExactMatchCondition(self::$ACL, $value); | |
} | |
public function setKey($value, $prefix = self::MATCH_EXACT) | |
{ | |
if ($prefix === self::MATCH_PREFIX) { | |
$this->addStartsWithCondition(self::$KEY, $value); | |
} else { | |
$this->addExactMatchCondition(self::$KEY, $value); | |
} | |
} | |
public function setContentLength($value) | |
{ | |
$this->addRangeCondition(self::$CONTENT_LENGTH_RANGE, $value, $value); | |
} | |
public function setContentType($value, $prefix = self::MATCH_EXACT) | |
{ | |
if ($prefix === self::MATCH_PREFIX) { | |
$this->addStartsWithCondition(self::$CONTENT_TYPE, $value); | |
} else { | |
$this->addExactMatchCondition(self::$CONTENT_TYPE, $value); | |
} | |
} | |
public function setSecurityToken($token) | |
{ | |
$this->addExactMatchCondition('x-amz-security-token', $token); | |
} | |
protected function addExactMatchCondition($name, $value) | |
{ | |
$this->conditions[] = array( | |
'eq', | |
$this->getPrefixedFieldName($name), | |
$value, | |
); | |
} | |
protected function addStartsWithCondition($name, $prefix = '') | |
{ | |
$this->conditions[] = array( | |
'starts-with', | |
$this->getPrefixedFieldName($name), | |
$prefix, | |
); | |
} | |
protected function addRangeCondition($name, $low, $high) | |
{ | |
$this->conditions[] = array( | |
$name, | |
$low, | |
$high, | |
); | |
} | |
public function __toString() | |
{ | |
return base64_encode(json_encode(array( | |
'expiration' => substr_replace($this->expiry->format('c'), 'Z', -6), | |
'conditions' => $this->conditions, | |
))); | |
} | |
private function getPrefixedFieldName($name) | |
{ | |
return '$' . $name; | |
} | |
public function jsonSerialize() | |
{ | |
return $this->__toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment