Source of file ContentServiceAsset.php
Size: 3,600 Bytes - Last Modified: 2021-12-23T10:28:12+00:00
/var/www/docs.ssmods.com/process/src/src/Symbiote/ContentServiceAssets/ContentServiceAsset.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php namespace Symbiote\ContentServiceAssets; /** * A non-file asset which is served from an external content source. */ class ContentServiceAsset extends \DataObject { private static $db = array( 'Filename' => 'Varchar(255)', 'FilePointer' => 'FileContent' ); private static $has_one = array( 'Parent' => 'File', 'Source' => 'File', ); private static $indexes = array( 'Filename' => true ); /* * This automatically migrates any records from the previous namespace to the current namespace, since the * table names actually contain these. */ public function requireDefaultRecords() { parent::requireDefaultRecords(); // The tables to migrate. $from = 'SilverStripeAustralia\ContentServiceAssets\ContentServiceAsset'; $to = 'Symbiote\ContentServiceAssets\ContentServiceAsset'; $escaped = 'Symbiote\\\\ContentServiceAssets\\\\ContentServiceAsset'; // This only ever needs to run once, so we need to do an efficient check here. if( !\DB::query("SELECT EXISTS(SELECT 1 FROM \"{$to}\" LIMIT 1);")->value() && \ClassInfo::hasTable($from) && \DB::query("SELECT EXISTS(SELECT 1 FROM \"{$from}\" LIMIT 1);")->value() ) { // Copy the records across, and make sure the class name is correct. \DB::query("INSERT INTO \"{$to}\" SELECT * FROM \"{$from}\";"); \DB::query("UPDATE \"{$to}\" SET ClassName = '{$escaped}';"); } } public function getSecureURL($expires = 60) { /** @var \FileContent $pointer */ $pointer = $this->obj('FilePointer'); if($pointer->exists() && $pointer->getValue()) { $reader = $pointer->getReader(); if ($reader) { if ($this->ParentID) { // check for things first $parent = $this->Parent(); if ($parent && $parent->ID && !$parent->canView()) { return; } } return method_exists($reader, 'getSecureURL') ? $reader->getSecureURL($expires) : ''; } } } /** * @return ContentReader */ public function reader() { $pointer = $this->obj('FilePointer'); if ($pointer && $pointer->getValue()) { return $pointer->getReader(); } } public function getURL() { $pointer = $this->obj('FilePointer'); $controller = \Controller::has_curr() ? \Controller::curr() : null; if ($controller instanceof \CMSMain) { return $this->Filename; } if($pointer->exists() && $pointer->getValue()) { $reader = $pointer->getReader(); if ($reader) { return $reader->getURL(); } } } public function canView($member = null) { $parent = $this->Parent(); if ($parent && $parent->ID) { return $parent->canView($member); } return parent::canView($member); } public function getViewType() { $parent = $this->Parent(); if ($parent && $parent->ID) { return $parent->getViewType(); } return \CDNFile::ANYONE_PERM; } public function onAfterDelete() { $obj = $this->obj('FilePointer'); if ($obj) { try { $writer = $obj->getReader()->getWriter(); $writer->delete(); } catch (Exception $ex) { // not much that can be done really? SS_Log::log($ex, SS_Log::WARN); } } } } |