Source of file S3ContentReader.php
Size: 4,923 Bytes - Last Modified: 2021-12-23T10:34:06+00:00
/var/www/docs.ssmods.com/process/src/code/content/S3ContentReader.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 | <?php /** * Read content from haylix cdn * * @author marcus@symbiote.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ class S3ContentReader extends ContentReader { public $bucket = 'bucket'; /** * @var S3Client */ public $s3Service; /** * * @var Guzzle\Service\Resource\Model */ private $s3Info; /** * The base URL to use with the s3 managed asset. Allows the use * of CloudFront base urls instead. * * @var string */ public $baseUrl = 'https://s3.amazonaws.com'; /** * * @return Guzzle\Service\Resource\Model */ public function getInfo() { if (!$this->s3Info) { $id = $this->getId(); if (substr($id, -1) == '/') { $this->s3Info = $this->listId($id); } else { $this->s3Info = $this->s3Service->getObject(array( 'Bucket' => $this->bucket, 'Key' => $id )); } } return $this->s3Info; } /** * * Set the S3 information about this item directly if available * * @param Guzzle\Service\Resource\Model $data */ public function setS3Info($data) { $this->s3Info = $data; } public function isReadable() { if (!parent::isReadable()) { return; } return strlen($this->getURL()); } /** * An S3 object is listable if its content type is a directory * * @return boolean */ public function isListable() { $result = $this->getInfo(); if ($result && (isset($result['CommonPrefixes']) || isset($result['Contents']))) { return true; } return false; } protected $listedItems = null; protected function listId($id) { if ($this->listedItems) { return $this->listedItems; } if ($id === '/') { $id = ''; } $this->listedItems = $this->s3Service->listObjects(array( "Bucket" => $this->bucket, "Prefix" => $id, 'return_prefixes' => true, 'Delimiter' => '/', )); return $this->listedItems; } /** * Returns a list of content readers for a given s3 folder * * @return \S3ContentReader */ public function getList() { if ($this->isListable()) { $list = array(); $objects = $this->listId($this->getId()); if (isset($objects['CommonPrefixes'])) { foreach ($objects['CommonPrefixes'] as $folder) { $id = $this->getSourceIdentifier() . ContentService::SEPARATOR . $folder['Prefix']; $new = singleton('ContentService')->getReader($id); $new->s3Service = $this->s3Service; $new->bucket = $this->bucket; $new->baseUrl = $this->baseUrl; $list[] = $new; } } if (isset($objects['Contents'])) { foreach ($objects['Contents'] as $object) { $name = $object['Key']; if ($name == $this->getId()) { continue; } $id = $this->getSourceIdentifier() . ContentService::SEPARATOR . $name; $new = singleton('ContentService')->getReader($id); $new->s3Service = $this->s3Service; $new->bucket = $this->bucket; $new->baseUrl = $this->baseUrl; $new->setS3Info($object); $list[] = $new; } } return $list; } return array(); } public function getBaseUrl() { return $this->baseUrl; } /** * Get a url to this piece of content * * @return string */ public function getURL() { $id = $this->getId(); $parts = explode('/', $id); array_walk($parts, function (&$segment) { $segment = rawurlencode($segment); }); return $this->getBaseUrl() . '/' . implode('/', $parts); } public function getBaseHost() { $base = $this->baseUrl; $bit = parse_url($base, PHP_URL_HOST); return $bit; } /** * Get a secure URL set to expire in $expires seconds time * * @param int $expires */ public function getSecureURL($expires = 60) { $cmd = $this->s3Service->getCommand('GetObject', [ 'Host' => $this->getBaseHost(), 'Bucket' => $this->bucket, 'Key' => $this->getId() ]); $request = $this->s3Service->createPresignedRequest($cmd, '+' . $expires . ' seconds'); $url = (string) $request->getUri(); return $url; } /** * Read this content as a string * * @return string */ public function read() { try { $result = $this->s3Service->getObject(array( 'Bucket' => $this->bucket, 'Key' => $this->getId() )); return $result['Body']; } catch (Exception $ex) { SS_Log::log($ex, SS_Log::WARN); throw $ex; } } /** * Check that the object exists remotely * * @return boolean */ public function exists() { $exists = $this->s3Service->doesObjectExist($this->bucket, $this->getId()); return $exists; } public function providerOfUrl($url) { if (strpos($url, $this->baseUrl) !== false) { $prefix = substr($url, strlen($this->baseUrl) + 1); $id = $this->getSourceIdentifier() . ContentService::SEPARATOR . $prefix; $new = singleton('ContentService')->getReader($id); return $new; } return false; } } |