/** * testProcessVersion * * @return void */ public function testProcessVersion() { $this->Image->create(); $result = $this->Image->save(array('foreign_key' => 'test-1', 'model' => 'Test', 'file' => array('name' => 'titus.jpg', 'size' => 332643, 'tmp_name' => CakePlugin::path('FileStorage') . DS . 'Test' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg', 'error' => 0))); $result = $this->Image->find('first', array('conditions' => array('id' => $this->Image->getLastInsertId()))); $this->assertTrue(!empty($result) && is_array($result)); $this->assertTrue(file_exists($this->testPath . $result['ImageStorage']['path'])); $path = $this->testPath . $result['ImageStorage']['path']; $Folder = new Folder($path); $folderResult = $Folder->read(); $this->assertEqual(count($folderResult[1]), 3); Configure::write('Media.imageSizes.Test', array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200)))); ClassRegistry::init('FileStorage.ImageStorage')->generateHashes(); $Event = new CakeEvent('ImageVersion.createVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200))))); CakeEventManager::instance()->dispatch($Event); $path = $this->testPath . $result['ImageStorage']['path']; $Folder = new Folder($path); $folderResult = $Folder->read(); $this->assertEqual(count($folderResult[1]), 4); $Event = new CakeEvent('ImageVersion.removeVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200))))); CakeEventManager::instance()->dispatch($Event); $path = $this->testPath . $result['ImageStorage']['path']; $Folder = new Folder($path); $folderResult = $Folder->read(); $this->assertEqual(count($folderResult[1]), 3); }
/** * Wrapper around the singleton call to StorageManager::adapter() * * Makes it easy to mock the adapter in tests. * * @throws \InvalidArgumentException * @param string $configName * @return array */ public function storageAdapter($configName, $renewObject = false) { if (empty($configName) || !is_string($configName)) { throw new \InvalidArgumentException('First arg must be a non empty string!'); } return StorageManager::adapter($configName, $renewObject); }
/** * testAdapter * * @todo more tests * @return void */ public function testAdapter() { $result = StorageManager::adapter('Local'); $this->assertEqual(get_class($result), 'Gaufrette\\Filesystem'); $result = StorageManager::activeAdapter(); $this->assertEqual($result, 'Local'); $result = StorageManager::activeAdapter('invalid-adapter'); $this->assertFalse($result); $result = StorageManager::config(); }
public function onBeforeDelete($Event) { if (!$this->_check($Event)) { return true; } $model = $Event->subject(); $fields = array('adapter', 'filename'); $data = $model->findById($model->id, $fields); $asset =& $data['AssetsAsset']; $adapter = StorageManager::adapter($asset['adapter']); if ($adapter->has($asset['filename'])) { $adapter->delete($asset['filename']); } return $model->deleteAll(array('parent_asset_id' => $model->id), true, true); }
public function onBeforeDelete($Event) { $model = $Event->subject(); if (!$this->_check($Event)) { return true; } $model = $Event->subject(); $fields = array('adapter', 'path'); $data = $model->findById($model->id, $fields); $asset =& $data['AssetsAsset']; $filesystem = StorageManager::adapter($asset['adapter']); $key = str_replace('/assets', '', $asset['path']); if ($filesystem->has($key)) { $filesystem->delete($key); } return $model->deleteAll(array('parent_asset_id' => $model->id), true, true); }
/** * afterSave * * @param CakeEvent $Event * @return void */ public function afterSave(CakeEvent $Event) { if ($this->_checkEvent($Event)) { $Model = $Event->subject(); $record = $Model->data[$Model->alias]; $Storage = StorageManager::adapter($record['adapter']); try { $id = $record[$Model->primaryKey]; $filename = $Model->stripUuid($id); $file = $record['file']; $record['path'] = $Model->fsPath('files' . DS . $record['model'], $id); $result = $Storage->write($record['path'] . $filename . '.' . $record['extension'], file_get_contents($file['tmp_name']), true); $Model->save(array($Model->alias => $record), array('validate' => false, 'callbacks' => false)); } catch (Exception $e) { $this->log($e->getMessage(), 'file_storage'); } } }
/** * Copy an existing attachment and resize with width: $w and height: $h * * @param integer $id Attachment Id * @param integer $w New Width * @param integer $h New Height * @param array $options Options array */ public function createResized($id, $w, $h, $options = array()) { $options = Hash::merge(array('uploadsDir' => 'assets'), $options); $imagine = $this->imagineObject(); $this->recursive = -1; $this->contain(array('AssetsAsset')); $attachment = $this->findById($id); $asset =& $attachment['AssetsAsset']; $path = rtrim(WWW_ROOT, '/') . $asset['path']; $image = $imagine->open($path); $size = $image->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); if (empty($h) && !empty($w)) { $scale = $w / $width; $newSize = $size->scale($scale); } elseif (empty($w) && !empty($h)) { $scale = $h / $height; $newSize = $size->scale($scale); } else { $scaleWidth = $w / $width; $scaleHeight = $h / $height; $scale = $scaleWidth > $scaleHeight ? $scaleWidth : $scaleHeight; $newSize = $size->scale($scale); } $newWidth = $newSize->getWidth(); $newHeight = $newSize->getHeight(); $image->resize($newSize); $tmpName = tempnam('/tmp', 'qq'); $image->save($tmpName, array('format' => $asset['extension'])); $fp = fopen($tmpName, 'r'); $stat = fstat($fp); fclose($fp); $raw = file_get_contents($tmpName); unlink($tmpName); $info = pathinfo($asset['path']); $ind = sprintf('.resized-%dx%d.', $newWidth, $newHeight); $uploadsDir = str_replace('/' . $options['uploadsDir'] . '/', '', dirname($asset['path'])) . '/'; $filename = $info['filename'] . $ind . $info['extension']; $writePath = $uploadsDir . $filename; $adapter = $asset['adapter']; $filesystem = StorageManager::adapter($adapter); $filesystem->write($writePath, $raw); $data = $this->AssetsAsset->create(array('filename' => $filename, 'path' => dirname($asset['path']) . '/' . $filename, 'model' => $asset['model'], 'extension' => $asset['extension'], 'parent_asset_id' => $asset['id'], 'foreign_key' => $asset['foreign_key'], 'adapter' => $adapter, 'mime_type' => $asset['mime_type'], 'width' => $newWidth, 'height' => $newHeight, 'filesize' => $stat[7])); $asset = $this->AssetsAsset->save($data); return $asset; }
/** * afterSave * * @param CakeEvent $Event * @return void */ public function afterSave(CakeEvent $Event) { if ($this->_checkEvent($Event)) { $Model = $Event->subject(); $Storage = StorageManager::adapter($Model->data[$Model->alias]['adapter']); $record = $Model->data[$Model->alias]; try { $id = $record[$Model->primaryKey]; $filename = $Model->stripUuid($id); $file = $record['file']; $record['path'] = $Model->fsPath('images' . DS . $record['model'], $id); if ($this->options['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']); } $result = $Storage->write($path, file_get_contents($file['tmp_name']), true); $data = $Model->save(array($Model->alias => $record), array('validate' => false, 'callbacks' => false)); $operations = Configure::read('Media.imageSizes.' . $record['model']); if (!empty($operations)) { $this->_createVersions($Model, $record, $operations); } $Model->data = $data; } catch (Exception $e) { $this->log($e->getMessage(), 'file_storage'); } } }
/** * Wrapper around the singleton call to StorageManager::config * * @param string $configName * @return Object */ public function getAdapter($configName) { return StorageManager::adapter($configName); }
/** * Get a storage adapter from the StorageManager * * @param string $adapterName * @param boolean $renewObject * @return \Gaufrette\Adapter */ public function getStorageAdapter($adapterName, $renewObject = false) { return StorageManager::adapter($adapterName, $renewObject); }
protected function _loop($action, $model, $operations = array()) { if (!in_array($action, array('generate', 'remove'))) { $this->_stop(); } $this->totaleImageCount = $this->Model->find('count', array('recursive' => -1, 'contain' => array(), 'conditions' => array($this->Model->alias . '.model' => $model, $this->Model->alias . '.extension' => array('jpg', 'png')))); if ($this->totaleImageCount > 0) { $this->out(__d('file_storage', '%d image files will be processed' . "\n", $this->totaleImageCount)); $processed = 0; $options = array('recursive' => -1, 'contain' => array(), 'conditions' => array($this->Model->alias . '.model' => $model, $this->Model->alias . '.extension' => array('jpg', 'png'))); $offset = 0; $limit = $this->limit; do { $options['limit'] = $limit; $options['offset'] = $offset; $images = $this->Model->find('all', $options); if (!empty($images)) { foreach ($images as $image) { $Storage = StorageManager::adapter($image[$this->Model->alias]['adapter']); if ($Storage === false) { $this->out(__d('file_storage'), 'Cant load adapter config %s for record %s', $image[$this->Model->alias]['adapter'], $image[$this->Model->alias][$this->Model->primaryKey]); } else { $payload = array('record' => $image, 'storage' => $Storage, 'operations' => $operations); if ($action == 'generate') { $Event = new CakeEvent('ImageVersion.createVersion', $this->Model, $payload); CakeEventManager::instance()->dispatch($Event); } if ($action == 'remove') { $Event = new CakeEvent('ImageVersion.removeVersion', $this->Model, $payload); CakeEventManager::instance()->dispatch($Event); } $this->out(__('%s processed', $image[$this->Model->alias]['id'])); } } } $offset += $limit; } while (!empty($images)); } else { $this->out(__d('file_storage', 'No Images for model %s found', $model)); } }