/**
  * @param $model
  * @param $type
  * @param $id
  * @param $fileName
  * @param $ext
  *
  * @throws \CHttpException
  */
 public function actionResize($model, $type, $id, $fileName, $ext)
 {
     $file = FPM::getOriginalFilePath($id, $fileName, $ext);
     if (file_exists($file)) {
         $meta = FPM::transfer()->getMetaData($id);
         if (!(is_array($meta) && $fileName === $meta['real_name'])) {
             throw new CHttpException(404, 'File not found');
         }
         $config = isset(FPM::m()->imageSections[$model]) && isset(FPM::m()->imageSections[$model][$type]) ? FPM::m()->imageSections[$model][$type] : null;
         if (!$config) {
             throw new CHttpException(400, 'Incorrect request');
         }
         $thumbFile = FPM::getCachedImagePath($id, $model, $type, $id . '-' . $meta['real_name'] . '.' . $meta['extension']);
         FPM::createCacheDir($id, $model, $type);
         /** @var $ih \fileProcessor\extensions\imageHandler\drivers\MDriverAbstract|\fileProcessor\extensions\imageHandler\MImageHandler */
         $ih = Yii::createComponent(FPM::m()->getImageHandler());
         $ih->init();
         $ih->load($file);
         if (isset($config['do'])) {
             switch ($config['do']) {
                 case 'adaptiveResize':
                     $ih->adaptiveThumb($config['width'], $config['height']);
                     break;
                 case 'resize':
                     $ih->resize($config['width'], $config['height']);
                     break;
                 case 'copy':
                     // do nothing, create copy of the file
                     break;
                 case 'resizeCanvas':
                     $ih->resizeCanvas($config['width'], $config['height']);
                     break;
                 case 'rhombusResizeCanvas':
                     $ih->resizeCanvas(floor($config['width'] / 2), floor($config['height'] / 2))->resizeCanvas($config['width'], $config['height']);
                     break;
                 default:
                     throw new CHttpException(400, 'Incorrect action');
                     break;
             }
         } else {
             $ih->adaptiveThumb($config['width'], $config['height']);
         }
         $ih->save($thumbFile, false, $config['quality']);
         $ih->show(false, $config['quality']);
     } else {
         throw new CHttpException(404, 'File not found');
     }
     Yii::app()->end();
 }
 /**
  * Parses a URL based on this rule.
  *
  * @param CUrlManager $manager the URL manager
  * @param CHttpRequest $request the request object
  * @param string $pathInfo path info part of the URL (URL suffix is already removed based on {@link CUrlManager::urlSuffix})
  * @param string $rawPathInfo path info that contains the potential URL suffix
  *
  * @return mixed the route that consists of the controller ID and action ID. False if this rule does not apply.
  */
 public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
 {
     if (!preg_match('#^' . FPM::m()->cachedImagesBaseDir . '\\/#', $pathInfo)) {
         return false;
     }
     foreach ($this->urlParams as $pattern => $params) {
         if (preg_match('#^' . FPM::m()->cachedImagesBaseDir . $pattern . '#', $pathInfo, $matches)) {
             foreach ($params as $key => $paramName) {
                 $_GET[$paramName] = $matches[$key];
             }
             return $this->controllerId . '/resize';
         }
     }
     return false;
 }
 /**
  * Delete cached images.
  *
  * @param integer $id image id
  *
  * @internal param bool|string $ext
  *
  * @return void
  */
 public function delete($id)
 {
     $metaData = FPM::transfer()->getMetaData($id);
     if (!in_array($metaData['extension'], array('png', 'jpeg', 'jpg', 'gif'), true)) {
         return;
     }
     $config = FPM::m()->imageSections;
     foreach ($config as $modelKey => $model) {
         foreach ($model as $typeKey => $type) {
             $fileName = FPM::getCachedImagePath($id, $modelKey, $typeKey, $metaData['real_name']);
             if (is_file($fileName)) {
                 unlink($fileName);
             }
         }
     }
 }
 public function down()
 {
     $this->dropTable(FPM::m()->tableName);
 }
예제 #5
0
 protected function getPublicPath()
 {
     return FPM::m()->host . FPM::m()->originalBaseDir;
 }
예제 #6
0
 public static function getAllFileInfoList($modelID, $modelName, $key = null)
 {
     $bindKey = empty($key) ? array() : array('file_key = :file_key');
     $paramKey = empty($key) ? array() : array(':file_key' => $key);
     return FPM::m()->getDb()->createCommand()->select()->from(XFiles::w()->xuploadViewName)->where(CMap::mergeArray(array('and', 'model_id = :mid', 'model_class = :mclass'), $bindKey), CMap::mergeArray(array(':mid' => $modelID, ':mclass' => $modelName), $paramKey))->queryAll();
 }
 protected function getPath2Dir($id)
 {
     if ($id > 0) {
         $dirName = floor($id / FPM::m()->filesPerDir);
         $path2Dir = FPM::m()->baseDir . FPM::m()->originalBaseDir . DIRECTORY_SEPARATOR . $dirName;
         if (!is_dir($path2Dir)) {
             FPM::mkdir($path2Dir, 0777, true);
         }
         return $path2Dir;
     }
     return null;
 }
 /**
  * Delete file
  *
  * @param integer $id file id
  *
  * @return boolean
  */
 public function deleteFile($id)
 {
     if (!(int) $id) {
         return false;
     }
     $dirName = $this->getBaseDestinationDir() . DIRECTORY_SEPARATOR . floor($id / $this->getMaxFilesPerDir());
     $meta = FPM::transfer()->getMetaData($id);
     $fileName = $dirName . DIRECTORY_SEPARATOR . $id . '-' . $meta['real_name'] . '.' . $meta['extension'];
     if (is_file($fileName)) {
         $result = unlink($fileName) && $this->deleteMetaData($id) ? true : false;
     } else {
         $result = false;
     }
     return $result;
 }
 /**
  * Delete file meta information.
  *
  * @param integer $id file id.
  *
  * @return boolean
  */
 public function deleteMetaData($id)
 {
     if (!empty(FPM::m()->cache) && Yii::app()->hasComponent(FPM::m()->cache)) {
         /** @var $cache \CDummyCache */
         $cache = Yii::app()->getComponent(FPM::m()->cache);
         $cache->delete(FPM::m()->CACHE_PREFIX . '#' . $id);
     }
     return (bool) FPM::m()->getDb()->createCommand()->delete(FPM::m()->tableName, 'id = :id', array(':id' => $id));
 }
 /**
  * commands will be executed in transaction
  */
 public function safeUp()
 {
     $this->createTable($this->tableName, array('id' => 'INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT', 'fpm_id' => 'INT UNSIGNED NOT NULL', 'label' => 'VARCHAR(200) NULL DEFAULT NULL COMMENT "Заголовок"', 'description' => 'TEXT NULL DEFAULT NULL', 'size' => 'INT UNSIGNED NOT NULL DEFAULT 0', 'mime' => 'CHAR(10) NULL DEFAULT NULL', 'file_key' => 'VARCHAR(45) NULL DEFAULT NULL', 'INDEX key_file_key (file_key)', 'CONSTRAINT fk_xupload_fpm_fpm_id_to_fpm_file_id FOREIGN KEY (fpm_id) REFERENCES ' . FPM::m()->relatedTableName . ' (id) ON DELETE CASCADE ON UPDATE CASCADE'), 'ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_unicode_ci');
 }
 /**
  * Return array of file ids
  *
  * @return array
  */
 public function getRelatedFiles()
 {
     /** @var ActiveRecord $owner */
     $owner = $this->getOwner();
     $files = FPM::m()->getDb()->createCommand()->select(array('file_id'))->from(FPM::m()->relatedTableName)->where('model_id = :mid AND model_class = :mclass')->queryColumn(array(':mid' => $owner->getPrimaryKey(), ':mclass' => $owner->getClassName()));
     return $files;
 }
 /**
  * commands will be executed in transaction
  */
 public function safeUp()
 {
     $this->execute('CREATE VIEW ' . $this->viewName . ' AS SELECT ' . $this->columns . ' FROM ' . FPM::m()->tableName . ' tn, ' . FPM::m()->relatedTableName . ' rtn, ' . $this->xuploadTableName . ' xtn WHERE tn.id = rtn.file_id AND tn.id = xtn.fpm_id');
 }
 private function deleteFile()
 {
     /** @var $owner CActiveRecord */
     $owner = $this->getOwner();
     FPM::deleteFiles($owner->{$this->attributeName});
 }
예제 #14
0
 /**
  * @param $id
  * @param $model
  * @param $type
  *
  * @throws CException
  */
 public static function createCacheDir($id, $model, $type)
 {
     $dirName = FPM::getBasePath() . FPM::m()->cachedImagesBaseDir . DIRECTORY_SEPARATOR . floor($id / FPM::m()->filesPerDir) . DIRECTORY_SEPARATOR . $model . '_' . $type;
     if (!is_dir($dirName)) {
         FPM::mkdir($dirName, 0777, true);
     }
 }