public function onBeforeSave($Event) { if (!$this->_check($Event)) { return true; } $model = $Event->subject(); $storage =& $model->data[$model->alias]; if (empty($storage['file'])) { if (isset($storage['path']) && empty($storage['filename'])) { $path = rtrim(WWW_ROOT, '/') . $storage['path']; $imageInfo = $this->__getImageInfo($path); $fp = fopen($path, 'r'); $stat = fstat($fp); $storage['filesize'] = $stat[7]; $storage['filename'] = basename($path); $storage['hash'] = sha1_file($path); $storage['mime_type'] = $imageInfo['mimeType']; $storage['width'] = $imageInfo['width']; $storage['height'] = $imageInfo['height']; $storage['extension'] = substr($path, strrpos($path, '.') + 1); } return true; } $file = $storage['file']; $filesystem = StorageManager::adapter($storage['adapter']); try { $raw = file_get_contents($file['tmp_name']); $key = sha1($raw); $extension = strtolower(FileStorageUtils::fileExtension($file['name'])); $imageInfo = $this->__getImageInfo($file['tmp_name']); if (isset($imageInfo['mimeType'])) { $mimeType = $imageInfo['mimeType']; } else { $mimeType = $file['type']; } if (empty($storage['path'])) { $prefix = FileStorageUtils::trimPath(FileStorageUtils::randomPath($file['name'])); } $fullpath = $prefix . '/' . $key . '.' . $extension; $result = $filesystem->write($fullpath, $raw); $storage['path'] = '/assets/' . $fullpath; $storage['filename'] = $file['name']; $storage['filesize'] = $file['size']; $storage['hash'] = sha1($raw); $storage['mime_type'] = $mimeType; $storage['width'] = $imageInfo['width']; $storage['height'] = $imageInfo['height']; $storage['extension'] = $extension; return $result; } catch (Exception $e) { $this->log($e->getMessage()); return false; } }
/** * testTrimPath * * @return void */ public function testTrimPath() { $result = FileStorageUtils::trimPath('foobar/'); $this->assertEqual($result, 'foobar'); }