/** * 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); }
/** * testAdapter * * @return void */ public function testAdapter() { $result = StorageManager::adapter('Local'); $this->assertEquals(get_class($result), 'Gaufrette\\Filesystem'); try { StorageManager::adapter('Does Not Exist'); $this->fail('Exception not thrown!'); } catch (\RuntimeException $e) { } }
/** * 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); }
/** * afterSave * * @param Event $Event * @return void */ public function afterSave(Event $Event) { if ($this->_checkEvent($Event)) { $table = $Event->subject(); $record = $Event->data['record']; $Storage = StorageManager::adapter($record->adapter); try { $id = $record->{$table->primaryKey()}; $filename = $this->stripDashes($id); $file = $record['file']; $record['path'] = $this->fsPath('images' . DS . $record['model'], $id); if ($this->_config['preserveFilename'] === true) { $path = $record['path'] . $record['filename']; } else { $path = $record['path'] . $filename . '.' . $record['extension']; } if ($this->adapterClass === 'AmazonS3' || $this->adapterClass === 'AwsS3') { $path = str_replace('\\', '/', $path); $record['path'] = str_replace('\\', '/', $record['path']); } $Storage->write($path, file_get_contents($file['tmp_name']), true); $data = $table->save($record, array('validate' => false, 'callbacks' => false)); $operations = Configure::read('FileStorage.imageSizes.' . $record['model']); if (!empty($operations)) { $this->_createVersions($table, $record, $operations); } $table->data = $data; } catch (\Exception $e) { $this->log($e->getMessage()); } } }