/**
  * Simple Method for detecting what model to save to
  * by file mime_type
  */
 protected function _detectModelByFileType($mime_type)
 {
     if (empty($mime_type)) {
         return false;
     }
     return FileStorageUtils::detectModelByFileType($mime_type);
 }
 /**
  * Generates an image url based on the image record data and the used Gaufrette adapter to store it
  *
  * @param array $image FileStorage array record or whatever else table that matches this helpers needs without the model, we just want the record fields
  * @param string $version Image version string
  * @param array $options HtmlHelper::image(), 2nd arg options array
  * @return string
  */
 public function display($file, $options = array())
 {
     $className = FileStorageUtils::detectModelByFileType($file['mime_type']);
     if (class_exists($className)) {
         // eg. $this->ImageStorage->display();
         return $this->{$className}->display($file, $options);
     }
     return $this->ImageStorage->fallback($options);
 }
 /**
  * testNormalizePath
  *
  * @return void
  */
 public function testNormalizePath()
 {
     if (DS == '\\') {
         $result = FileStorageUtils::normalizePath('/nice/path/test');
         $this->assertEqual($result, '\\nice\\path\\test');
     } else {
         $result = FileStorageUtils::normalizePath('\\nice\\path\\test');
         $this->assertEqual($result, '/nice/path/test');
     }
 }
 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;
     }
 }
Пример #5
0
 /**
  * Generates a semi-random file system path
  *
  * @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 . FileStorageUtils::randomPath($string);
     if ($idFolder) {
         $path .= $string . DS;
     }
     return $path;
 }
 public function isImage($data)
 {
     if ($model = FileStorageUtils::detectModelByFileType($data['mime_type'])) {
         if ($model == "ImageStorage") {
             return true;
         }
     }
     return false;
 }