/** * Get file meta data * * @param integer $id file id * * @return \metalguardian\fileProcessor\models\File|null * @throws \Exception */ public function getData($id) { $model = \metalguardian\fileProcessor\models\File::findOne($id); if (!$model) { throw new \Exception(\metalguardian\fileProcessor\Module::t('exception', 'Missing meta data for file')); } return $model; }
/** * @param string $module * * @throws \yii\base\InvalidConfigException * @return \metalguardian\fileProcessor\Module */ public static function m($module = null) { if (is_null($module)) { $module = static::$moduleName; } if (!\Yii::$app->hasModule($module)) { throw new \yii\base\InvalidConfigException(\metalguardian\fileProcessor\Module::t('exception', 'Wrong module name! You need call this method with right fileProcessor module name.')); } return \Yii::$app->getModule($module); }
/** * @param $sub * @param $module * @param $size * @param $id * @param $baseName * @param $extension * * @return int * @throws InvalidConfigException * @throws NotFoundHttpException * @throws \Exception */ public function actionProcess($sub, $module, $size, $id, $baseName, $extension) { $directory = (string) floor($id / FPM::getFilesPerDirectory()); if ($sub !== $directory) { throw new NotFoundHttpException(Module::t('exception', 'Wrong generated link')); } $fileName = FPM::getOriginalDirectory($id) . DIRECTORY_SEPARATOR . FPM::getOriginalFileName($id, $baseName, $extension); if (file_exists($fileName)) { $data = FPM::transfer()->getData($id); if (strtolower($baseName) !== strtolower($data->base_name)) { throw new NotFoundHttpException(Module::t('exception', 'File not found')); } $config = isset(FPM::m()->imageSections[$module][$size]) ? FPM::m()->imageSections[$module][$size] : null; if (!is_array($config)) { throw new NotFoundHttpException(Module::t('exception', 'Incorrect request')); } $thumbnailFile = FPM::getThumbnailDirectory($id, $module, $size) . DIRECTORY_SEPARATOR . FPM::getThumbnailFileName($id, $baseName, $extension); FileHelper::createDirectory(FPM::getThumbnailDirectory($id, $module, $size)); if (isset($config['action'])) { switch ($config['action']) { case FPM::ACTION_ADAPTIVE_THUMBNAIL: Image::thumbnail($fileName, $config['width'], $config['height'])->save($thumbnailFile)->show($extension); break; case FPM::ACTION_THUMBNAIL: Image::thumbnail($fileName, $config['width'], $config['height'], ManipulatorInterface::THUMBNAIL_INSET)->save($thumbnailFile)->show($extension); break; case FPM::ACTION_CROP: Image::crop($fileName, $config['width'], $config['height'], $config['startX'], $config['startY'])->save($thumbnailFile)->show($extension); break; case FPM::ACTION_CANVAS_THUMBNAIL: Image::canvasThumbnail($fileName, $config['width'], $config['height'])->save($thumbnailFile)->show($extension); break; case FPM::ACTION_FRAME: Image::frame($fileName, 50, 'F00')->save($thumbnailFile)->show($extension); break; case FPM::ACTION_COPY: if (FPM::m()->symLink) { symlink($fileName, $thumbnailFile); } else { copy($fileName, $thumbnailFile); } \Yii::$app->response->sendFile($thumbnailFile); break; default: throw new InvalidConfigException(Module::t('exception', 'Action is incorrect')); break; } } else { throw new InvalidConfigException(Module::t('exception', 'Action not defined')); } } else { throw new NotFoundHttpException(Module::t('exception', 'File not found')); } }
/** * @inheritdoc */ public function attributeLabels() { return ['id' => Module::t('model', 'ID'), 'extension' => Module::t('model', 'Extension'), 'base_name' => Module::t('model', 'Base name'), 'created_at' => Module::t('model', 'Created At')]; }