kuvalda-file/console/ResizeController.php

130 lines
4.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace dominion\file\console;
use yii\console\ExitCode;
use dominion\file\Config;
use dominion\file\File;
/**
* пересоздание миниатю
*
*/
class ResizeController extends \yii\console\Controller
{
/**
* @var integer начать ресайз не с начала указанного смешения
*/
public $beginFrom = 0;
/**
* @var tring стисок id файлов через запятую
* По умочанию все afqks. Пример 25760,25762,25761
*/
public $onlyFile = '';
public function options($actionID)
{
$output = parent::options($actionID);
$output[] = 'beginFrom';
$output[] = 'onlyFile';
return $output;
}
public function optionAliases()
{
$output = parent::optionAliases();
$output['begin_from'] = 'beginFrom';
$output['only_file'] = 'onlyFile';
return $output;
}
/**
* обязательно id конфига миниатьры
* @param int $id
* @return type
*/
public function actionIndex(int $id)
{
$config = Config::findOne(['id' => $id]);
if ($config)
{
$fileTemplate = $config->file ? [
'filePatch' => $config->file->getFilePath(false, true),
'border_top' => $config->border_top,
'border_right' => $config->border_right,
'border_left' => $config->border_left,
'border_bottom' => $config->border_bottom,
] : [];
if ($config->file)
{
$config->file->downloadOriginal();
}
$flag = true;
$offset = $this->beginFrom;
$limit = 100;//000;
while ($flag)
{
$query = File::find()->andWhere(['module' => $config->module])->orderBy(['id'=>SORT_ASC])->offset($offset)->limit($limit);
if(!empty($this->onlyFile))
{
$query->andWhere(['id' => explode(',', $this->onlyFile)]);
}
$files = (array) $query->all();
$offset = $offset + count($files);
$flag = empty($files) || count($files) < $limit ? false :true;
$fileDelete = [];
foreach ($files as $file)
{
try
{
$file->downloadOriginal();
$file->resize($config->type, $config->height, $config->width, $config->crop, $config->cut, $config->addBorder, true, $fileTemplate, $config->quality);
if ($file->cropFile)
{
try
{
unlink($file->cropFile);
}
catch (\Exception $ex)
{
$fileDelete[] = $file->cropFile;
}
}
if (\Yii::$app->has('s3'))
{
$s3 = \Yii::$app->get('s3');
if (file_exists($file->getFilePath($config->type, true)))
{
$s3->upload($file->getFilePath($config->type), $file->getFilePath($config->type, true));
try
{
unlink($file->getFilePath($config->type, true));
}
catch (\Exception $ex)
{
$fileDelete[] = $file->getFilePath($config->type, true);
}
}
}
} catch (\Exception | \ValueError $ex)
{
echo "fileId {$file->id}; Exception: {$ex->getMessage()} \n";
}
}
echo "обработано: {$offset} \n";
foreach ($fileDelete as $value)
{
@unlink($value);
}
}
}
return ExitCode::OK;
}
}