/** * testProcessVersion * * @return void */ public function testProcessVersion() { $entity = $this->Image->newEntity(['foreign_key' => 'test-1', 'model' => 'Test', 'file' => ['name' => 'titus.jpg', 'size' => 332643, 'tmp_name' => Plugin::path('Burzum/FileStorage') . DS . 'tests' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg', 'error' => 0]], ['accessibleFields' => ['*' => true]]); $this->Image->save($entity); $result = $this->Image->find()->where(['id' => $entity->id])->first(); $this->assertTrue(!empty($result) && is_a($result, '\\Cake\\ORM\\Entity')); $this->assertTrue(file_exists($this->testPath . $result['path'])); $path = $this->testPath . $result['path']; $Folder = new Folder($path); $folderResult = $Folder->read(); $this->assertEquals(count($folderResult[1]), 3); Configure::write('FileStorage.imageSizes.Test', array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200)))); StorageUtils::generateHashes(); $Event = new Event('ImageVersion.createVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200))))); EventManager::instance()->dispatch($Event); $path = $this->testPath . $result['path']; $Folder = new Folder($path); $folderResult = $Folder->read(); $this->assertEquals(count($folderResult[1]), 4); $Event = new Event('ImageVersion.removeVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200))))); EventManager::instance()->dispatch($Event); $path = $this->testPath . $result['path']; $Folder = new Folder($path); $folderResult = $Folder->read(); $this->assertEquals(count($folderResult[1]), 3); }
/** * testFlush * * @return void */ public function testFlush() { $config = StorageManager::config('Local'); $result = StorageManager::flush('Local'); $this->assertTrue($result); $result = StorageManager::flush('Does not exist'); $this->assertFalse($result); StorageManager::config('Local', $config); }
/** * Setup test folders and files * * @return void */ public function setUp() { parent::setUp(); $this->_setupListeners(); $this->testPath = TMP . 'file-storage-test' . DS; $this->fileFixtures = Plugin::path('Burzum/FileStorage') . 'tests' . DS . 'Fixture' . DS . 'File' . DS; if (!is_dir($this->testPath)) { mkdir($this->testPath); } Configure::write('FileStorage.basePath', $this->testPath); Configure::write('FileStorage.imageSizes', array('Test' => array('t50' => array('thumbnail' => array('mode' => 'outbound', 'width' => 50, 'height' => 50)), 't150' => array('thumbnail' => array('mode' => 'outbound', 'width' => 150, 'height' => 150))), 'UserAvatar' => ['small' => array('thumbnail' => array('mode' => 'inbound', 'width' => 80, 'height' => 80))])); StorageUtils::generateHashes(); StorageManager::config('Local', array('adapterOptions' => [$this->testPath, true], 'adapterClass' => '\\Gaufrette\\Adapter\\Local', 'class' => '\\Gaufrette\\Filesystem')); $this->FileStorage = TableRegistry::get('Burzum/FileStorage.FileStorage'); $this->ImageStorage = TableRegistry::get('Burzum/FileStorage.ImageStorage'); }
/** * Store a local file via command line in any storage backend. * * @return void */ public function store() { $model = $this->loadModel($this->params['model']); if (empty($this->args[0])) { $this->error('No file provided!'); } if (!file_exists($this->args[0])) { $this->error('The file does not exist!'); } $adapterConfig = StorageManager::config($this->params['adapter']); if (empty($adapterConfig)) { $this->error(sprintf('Invalid adapter config `%s` provided!', $this->params['adapter'])); } $fileData = StorageUtils::fileToUploadArray($this->args[0]); $entity = $model->newEntity(['adapter' => $this->params['adapter'], 'file' => $fileData, 'filename' => $fileData['name']]); if ($model->save($entity)) { $this->out('File successfully saved!'); $this->out('UUID: ' . $entity->id); $this->out('Path: ' . $entity->path()); } else { $this->error('Failed to save the file.'); } }
/** * Gets the bucket from the adapter configuration. * * @param string Storage adapter config name. * @return string */ protected function _getBucket($adapter) { $config = StorageManager::config($adapter); return $config['adapterOptions'][1]; }
/** * Method used for handling image file thumbnails creation and removal. * * Note that the code on this method was borrowed fromBurzum/FileStorage * plugin, ImageVersionShell Class _loop method. * * @param \Cake\ORM\Entity $entity File Entity * @param string $eventName Event name * @return bool */ protected function _handleThumbnails(Entity $entity, $eventName) { if (!in_array(strtolower($entity->extension), $this->_imgExtensions)) { return false; } $operations = Configure::read('FileStorage.imageSizes.' . $entity->model); // @NOTE: if we don't have a predefined setup for the field // image versions, we add it dynamically with default thumbnail versions. if (empty($operations)) { Configure::write('FileStorage.imageSizes.' . $entity->model, Configure::read('ThumbnailVersions')); $operations = Configure::read('FileStorage.imageSizes.' . $entity->model); } $storageTable = TableRegistry::get('Burzum/FileStorage.ImageStorage'); $result = true; foreach ($operations as $version => $operation) { $payload = ['record' => $entity, 'storage' => StorageManager::adapter($entity->adapter), 'operations' => [$version => $operation], 'versions' => [$version], 'table' => $storageTable, 'options' => []]; $event = new Event($eventName, $storageTable, $payload); EventManager::instance()->dispatch($event); if ('error' === $event->result[$version]['status']) { $result = false; } } return $result; }
/** * afterSave * * @param Event $event * @return void */ public function afterSave(Event $event) { if ($this->_checkEvent($event) && $event->data['record']->isNew()) { $table = $event->subject(); $entity = $event->data['record']; $Storage = StorageManager::adapter($entity['adapter']); try { $filename = $this->buildFileName($table, $entity); $entity->path = $this->buildPath($table, $entity); $Storage->write($entity['path'] . $filename, file_get_contents($entity['file']['tmp_name']), true); $table->save($entity, array('validate' => false, 'callbacks' => false)); } catch (\Exception $e) { $this->log($e->getMessage()); } } }
/** * StorageAdapter * * @param mixed $adapterName string of adapter configuration or array of settings * @param boolean $renewObject Creates a new instance of the given adapter in the configuration * @throws RuntimeException * @return Gaufrette object as configured by first argument */ public static function adapter($adapterName, $renewObject = false) { return NewStorageManager::adapter($adapterName, $renewObject); }
/** * Loops through image records and performs requested operation on them. * * @param string $action * @param $model * @param array $operations */ protected function _loop($action, $model, $operations = [], $options = []) { if (!in_array($action, array('generate', 'remove', 'regenerate'))) { $this->_stop(); } $totalImageCount = $this->_getCount($model); if ($totalImageCount === 0) { $this->out(__d('file_storage', 'No Images for model {0} found', $model)); $this->_stop(); } $this->out(__d('file_storage', '{0} image file(s) will be processed' . "\n", $totalImageCount)); $offset = 0; $limit = $this->limit; do { $images = $this->_getRecords($model, $limit, $offset); if (!empty($images)) { foreach ($images as $image) { $Storage = StorageManager::adapter($image->adapter); if ($Storage === false) { $this->out(__d('file_storage', 'Cant load adapter config {0} for record {1}', $image->adapter, $image->id)); } else { $payload = array('record' => $image, 'storage' => $Storage, 'operations' => $operations, 'versions' => array_keys($operations), 'table' => $this->Table, 'options' => $options); if ($action == 'generate' || $action == 'regenerate') { $Event = new Event('ImageVersion.createVersion', $this->Table, $payload); EventManager::instance()->dispatch($Event); } if ($action == 'remove') { $Event = new Event('ImageVersion.removeVersion', $this->Table, $payload); EventManager::instance()->dispatch($Event); } $this->out(__('{0} processed', $image->id)); } } } $offset += $limit; } while ($images->count() > 0); }
/** * Wrapper around the singleton call to StorageManager::config * Makes it easy to mock the adapter in tests. * * @param string $configName * @return Object */ public function getAdapter($configName) { return StorageManager::adapter($configName); }
/** * Gets the adapter class name from the adapter configuration key * * @param string * @return string|false */ public function getAdapterClassName($adapterConfigName) { $config = StorageManager::config($adapterConfigName); switch ($config['adapterClass']) { case '\\Gaufrette\\Adapter\\Local': $this->adapterClass = 'Local'; return $this->adapterClass; case '\\Gaufrette\\Adapter\\AwsS3': $this->adapterClass = 'AwsS3'; return $this->adapterClass; case '\\Gaufrette\\Adapter\\AmazonS3': $this->adapterClass = 'AwsS3'; return $this->adapterClass; case '\\Gaufrette\\Adapter\\AwsS3': $this->adapterClass = 'AwsS3'; return $this->adapterClass; default: return false; } }
<?php use Burzum\FileStorage\Lib\FileStorageUtils; use Burzum\FileStorage\Storage\Listener\BaseListener; use Burzum\FileStorage\Storage\StorageManager; use Cake\Core\Configure; use Cake\Event\EventManager; /* default thumbnail setup for all $entity->model entities for file_storage */ Configure::write('ThumbnailVersions', ['huge' => ['thumbnail' => ['width' => 2000, 'height' => 2000]], 'large' => ['thumbnail' => ['width' => 1024, 'height' => 1024]], 'medium' => ['thumbnail' => ['width' => 500, 'height' => 500]], 'small' => ['thumbnail' => ['width' => 150, 'height' => 150]], 'tiny' => ['thumbnail' => ['width' => 50, 'height' => 50]]]); Configure::write('FileStorage', ['pathBuilderOptions' => ['pathBuilderOptions' => ['pathPrefix' => '/uploads']], 'association' => 'UploadDocuments', 'imageSizes' => ['file_storage' => ['huge' => ['thumbnail' => ['width' => 2000, 'height' => 2000]], 'large' => ['thumbnail' => ['width' => 1024, 'height' => 1024]], 'medium' => ['thumbnail' => ['width' => 500, 'height' => 500]], 'small' => ['thumbnail' => ['width' => 150, 'height' => 150]], 'tiny' => ['thumbnail' => ['width' => 50, 'height' => 50]]]]]); /** * @todo find if we need this or not */ FileStorageUtils::generateHashes(); StorageManager::config('Local', ['adapterOptions' => [WWW_ROOT, true], 'adapterClass' => '\\Gaufrette\\Adapter\\Local', 'class' => '\\Gaufrette\\Filesystem']); $listener = new BaseListener(Configure::read('FileStorage.pathBuilderOptions')); EventManager::instance()->on($listener);