/** * 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.'); } }
/** * testGetFileHashInvalidArgumentException * * @expectedException \InvalidArgumentException */ public function testGetFileHashInvalidArgumentException() { StorageUtils::getFileHash($this->fileFixtures . 'titus.jpg', 'invalid-hash-method!'); }
/** * testGetImageVersions * * @return void */ public function testGetImageVersions() { Configure::write('FileStorage.imageSizes', ['Item' => ['t100' => ['thumbnail' => ['width' => 300, 'height' => 300]], 'crop50' => ['centercrop' => ['width' => 300, 'height' => 300]]]]); StorageUtils::generateHashes(); $entity = $this->Image->get('file-storage-2'); $entity->path = 'images' . DS . '30' . DS . '20' . DS . '10' . DS; $result = $this->Image->getImageVersions($entity); $expected = ['t100' => '/images/30/20/10/filestorage2.ead9ceef.jpg', 'crop50' => '/images/30/20/10/filestorage2.9aade7aa.jpg', 'original' => '/images/30/20/10/filestorage2.jpg']; $this->assertEquals($result, $expected); }
/** * Generates a semi-random file system path * * @deprecated Don't use this anymore but it is still used by two legacy listeners :( * @param string $type * @param string $string * @param boolean $idFolder * @return string */ public function fsPath($type, $string, $idFolder = true) { $string = str_replace('-', '', $string); $path = $type . DS . StorageUtils::randomPath($string); if ($idFolder) { $path .= $string . DS; } return $path; }
/** * Gets the hash of a file. * * You can use this to compare if you got two times the same file uploaded. * * @param string $file Path to the file on your local machine. * @param string $method 'md5' or 'sha1' * @throws \InvalidArgumentException * @link http://php.net/manual/en/function.md5-file.php * @link http://php.net/manual/en/function.sha1-file.php * @link http://php.net/manual/en/function.sha1-file.php#104748 * @return string */ public static function getFileHash($file, $method = 'sha1') { return StorageUtils::getFileHash($file, $method); }
/** * Calculates the hash of a file. * * You can use this to compare if you got two times the same file uploaded. * * @param string $file Path to the file on your local machine. * @param string $method 'md5' or 'sha1' * @throws \InvalidArgumentException * @link http://php.net/manual/en/function.md5-file.php * @link http://php.net/manual/en/function.sha1-file.php * @link http://php.net/manual/en/function.sha1-file.php#104748 * @return string */ public function calculateFileHash($file, $method = 'sha1') { return StorageUtils::getFileHash($file, $method); }
/** * beforeSave * * @param Event $Event * @return void */ public function beforeSave(Event $Event) { if ($this->_checkEvent($Event)) { if (in_array($Event->data['record']['model'], (array) $this->config('autoRotate'))) { $imageFile = $Event->data['record']['file']['tmp_name']; $format = StorageUtils::fileExtension($Event->data['record']['file']['name']); $this->_autoRotate($imageFile, $format); } } }