Source of file CloudinaryStorage.php
Size: 5,224 Bytes - Last Modified: 2021-12-23T10:02:36+00:00
/var/www/docs.ssmods.com/process/src/src/Storage/CloudinaryStorage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 | <?php namespace MadeHQ\Cloudinary\Storage; use MadeHQ\Cloudinary\Model\File; use SilverStripe\Assets\Storage; use Cloudinary; use Cloudinary\Uploader; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Configurable; class CloudinaryStorage implements Storage\AssetStore, Storage\AssetStoreRouter { use Configurable; private static $upload_preset; /** * @inheritdoc */ public function getCapabilities() { return array( 'visibility' => array( self::VISIBILITY_PUBLIC, ), 'conflict' => array( // self::CONFLICT_EXCEPTION, // self::CONFLICT_OVERWRITE, // self::CONFLICT_RENAME, // self::CONFLICT_USE_EXISTING ) ); } /** * @inheritdoc */ public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { // intentionally empty } /** * This is called from the URL `/assets/{$Filename}` * @inheritdoc */ public function getResponseFor($asset) { $file = File::getOneByPublicId($asset); $controller = \SilverStripe\Control\Controller::curr(); if ($file) { return $controller->redirect($file->AbsoluteURL, 301); } return $controller->httpError(404, sprintf('Unable to find [%s]', $asset)); } /** * @inheritdoc */ public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { $uploadPath = str_replace('\\', '/', $filename); $parts = explode('/', $uploadPath); $justFileName = array_pop($parts); $pathParts = pathinfo($justFileName); $extension = $pathParts['extension']; // Copy the uploaded files to a tmp location with the correct name, so we can let cloudinary deal // with generating a publicID using the actual filename $tmpFile = str_replace("//", "/", sys_get_temp_dir() . "/" . $justFileName); if(!move_uploaded_file($path, $tmpFile)) { throw new \Exception('Could not copy uploaded file to ' . $tmpFile); } $options = [ 'folder' => implode('/', $parts), 'resource_type' => 'auto', ]; if ($preset = static::config()->get('upload_preset')) { $options['upload_preset'] = $preset; } $response = Uploader::upload($tmpFile, $options); return [ 'Filename' => $response['public_id'], 'PublicID' => $response['public_id'], 'Format' => isset($response['format']) ? $response['format'] : $extension, 'SecureURL' => $response['secure_url'], 'ResourceType' => $response['resource_type'], 'Type' => $response['type'], ]; } /** * @inheritdoc */ public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { // intentionally empty } /** * @inheritdoc */ public function getAsString($filename, $hash, $variant = null) { // intentionally empty } /** * @inheritdoc */ public function getAsStream($filename, $hash, $variant = null) { // intentionally empty } /** * @inheritdoc */ public function getAsURL($filename, $hash, $variant = null, $grant = true) { return Cloudinary::cloudinary_url($filename); } /** * @inheritdoc */ public function getMetadata($filename, $hash, $variant = null) { return null; } /** * @inheritdoc */ public function getMimeType($filename, $hash, $variant = null) { return 'image/png'; } /** * @inheritdoc */ public function getVisibility($filename, $hash) { return self::VISIBILITY_PUBLIC; } /** * @inheritdoc */ public function exists($filename, $hash, $variant = null) { header(sprintf('Checking-%s: %s', $hash, $filename)); return true; } /** * @inheritdoc */ public function delete($filename, $hash) { // intentionally empty } /** * @inheritdoc */ public function rename($filename, $hash, $newName) { // intentionally empty } /** * @inheritdoc */ public function copy($filename, $hash, $newName) { // intentionally empty } /** * @inheritdoc */ public function publish($filename, $hash) { // intentionally empty } /** * Not able to protect files in Cloudinary * @inheritdoc */ public function protect($filename, $hash) { // intentionally empty } /** * @inheritdoc */ public function grant($filename, $hash) { // intentionally empty } /** * @inheritdoc */ public function revoke($filename, $hash) { // intentionally empty } /** * @inheritdoc */ public function canView($filename, $hash) { // intentionally empty } } |