diff --git a/repository/s3/S3.php b/repository/s3/S3.php index dadfb5a..ca55da3 100644 --- a/repository/s3/S3.php +++ b/repository/s3/S3.php @@ -732,6 +732,12 @@ class S3 { urlencode(self::__getHash("GET\n\n\n{$expires}\n/{$bucket}/{$uri}"))); } + public static function getFileURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false) { + $expires = time() + $lifetime; + $uri = str_replace('%2F', '/', rawurlencode($uri)); // URI should be encoded (thanks Sean O'Dea) + return sprintf(($https ? 'https' : 'http').'://%s/%s', $hostBucket ? $bucket : $bucket.'.s3.amazonaws.com', $uri); + } + /** * Create a CloudFront distribution diff --git a/repository/s3/lib.php b/repository/s3/lib.php index 0aa03fe..b264302 100644 --- a/repository/s3/lib.php +++ b/repository/s3/lib.php @@ -115,6 +115,21 @@ class repository_s3 extends repository { } /** + * Return file URL + * + * @param string $url the url of file + * @return string + */ + public function get_link($filepath, $file) { + $arr = explode('/', $filepath); + $bucket = $arr[0]; + $filename = $arr[1]; + $url = $this->s->getFileURL($bucket, $filename); + + return $url; + } + + /** * S3 doesn't require login * * @return bool @@ -151,6 +166,6 @@ class repository_s3 extends repository { * @return int */ public function supported_returntypes() { - return FILE_INTERNAL; + return (FILE_INTERNAL | FILE_EXTERNAL); } }