/app/commands/UploadFeedImages.php
PHP | 283 lines | 215 code | 38 blank | 30 comment | 26 complexity | e6e4bfb2c101a5b4480f8122a3312f44 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- use Illuminate\Console\Command;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Input\InputArgument;
- use Parsers\Tools\ParserTools;
- class UploadFeedImages extends Command {
- /**
- * The console command name.
- *
- * @var string
- */
- protected $name = 'feeds:upload';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Upload feed files by url.';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function fire()
- {
- $type = $this->argument('upload-type');
- $option = $this->option('option');
- $partner = $this->option('partner');
- if ($type == 'images') {
- if ($option == 'delete')
- {
- if (empty($partner))
- {
- $this->deleteImages();
- } else {
- $this->deleteImagesByPartner($partner);
- }
- } else {
- $this->uploadImages($partner);
- }
- }
- }
- public function uploadImages($partner = null) {
- $UploadInst = $this->getUploadFileInst();
- $staticUrl = Config::get('static.public_url');
- $limit = 500;
- $countUploaded = 0;
- $filename = 'pictures/';
- do {
- $productsObj = \DB::table('products')
- ->select(array(\DB::raw('sql_calc_found_rows products.*, pi.id AS id_image, pi.url AS image_url')))
- ->join('products_images AS pi', function($join)
- {
- $join->on('pi.id_product', '=', 'products.id')
- ->where('pi.is_local', '!=', DB::raw(1))
- ->where('pi.primary', '=', DB::raw(1));
- });
- if (!empty($partner))
- {
- $productsObj->join('shops AS s', 's.id', '=', 'products.id_shop')
- ->where('s.slug', $partner);
- }
- $products = $productsObj->groupBy('products.id')
- ->havingRaw('COUNT(pi.id) > 0')
- ->limit($limit)
- ->get();
- if (empty($totalCount)) {
- $totalCount = (int) DB::select(DB::raw("SELECT FOUND_ROWS() AS count;"))[0]->count;
- }
- if (!empty($products)) {
- DB::beginTransaction();
- $rows = [];
- foreach($products AS $product)
- {
- if (empty($product->image_url))
- {
- continue;
- }
- $ext = pathinfo($product->image_url, PATHINFO_EXTENSION);
- $tmp = $this->loadFile($product->image_url);
- if (file_exists($tmp))
- {
- $slugName = slug($product->name.' '.$product->season,
- ['transliterate' => true, 'delimiter' => '_']);
- $newName = $filename.$product->id.'/'
- .base_convert($product->id_image, 10, 36)
- .'_'.$slugName.'.'.$ext;
- if ($UploadInst->uploadFile($tmp, $newName))
- {
- $rows[] = [
- 'id_product' => $product->id,
- 'url' => $staticUrl.'/'.$newName,
- 'is_local' => 1,
- 'primary' => 1
- ];
- DB::table('products_images')
- ->where('id', $product->id_image)
- ->update(['primary' => 0]);
- } else {
- $this->error('File '.$product->url.'
- of product '.$product->id.' hasn\'t uploaded to cloud');
- }
- unlink($tmp);
- }
- }
- DB::table('products_images')->insert($rows);
- DB::commit();
- $countUploaded += count($products);
- $this->info( $countUploaded .' images have uploaded');
- if ($countUploaded >= $totalCount) {
- break;
- }
- }
- } while(!empty($products));
- }
- public function deleteImages()
- {
- $UploadInst = $this->getUploadFileInst();
- $staticUrl = Config::get('static.public_url');
- $limit = 500;
- $countUploaded = 0;
- $totalCount = DB::table('old_files')->select('name','id')->count();
- do
- {
- $files = DB::table('old_files')->select('name','id')->limit($limit)->get();
- foreach($files AS $file)
- {
- if (preg_match('~^'.$staticUrl.'\/(.*)$~i', $file->name, $match))
- {
- $filename = trim($match[1]);
- if ($UploadInst->deleteFile($filename)) {
- DB::table('old_files')->where('id', $file->id)->delete();
- }
- }
- }
- $countUploaded += count($files);
- $this->info( $countUploaded .' items have deleted');
- if ($countUploaded > $totalCount) {
- break;
- }
- } while (!empty($files));
- }
- public function deleteImagesByPartner($partner)
- {
- $UploadInst = $this->getUploadFileInst();
- $staticUrl = Config::get('static.public_url');
- $limit = 500;
- $countUploaded = 0;
- $totalCount = DB::table('products_images AS pi')
- ->select('id')
- ->join('products AS p', 'p.id', '=', 'pi.id_product')
- ->join('shops AS s', 's.id', '=', 'p.id_shop')
- ->where('s.slug', $partner)
- ->where('pi.is_local', 1)
- ->count();
- do
- {
- $files = DB::table('products_images AS pi')
- ->select('pi.url', 'pi.id', 'p.id AS id_product')
- ->join('products AS p', 'p.id', '=', 'pi.id_product')
- ->join('shops AS s', 's.id', '=', 'p.id_shop')
- ->where('s.slug', $partner)
- ->where('pi.is_local', 1)
- ->limit($limit)
- ->get();
- foreach($files AS $file)
- {
- if (preg_match('~^'.$staticUrl.'\/(.*)$~i', $file->url, $match))
- {
- $filename = trim($match[1]);
- if ($UploadInst->deleteFile($filename)) {
- DB::table('products_images')->where('id', $file->id)->delete();
- $id_update = DB::table('products_images AS pi')->select('id')
- ->where('id_product', $file->id_product)->first()->id;
- DB::table('products_images')->where('id', $id_update)->update(['primary' => 1]);
- }
- }
- }
- $countUploaded += count($files);
- $this->info( $countUploaded .' items have deleted');
- if ($countUploaded > $totalCount) {
- break;
- }
- } while (!empty($files));
- }
- protected function getUploadFileInst()
- {
- $UploadInst = UploadFile::init( Config::get('static.host'),
- Config::get('static.login'),
- Config::get('static.pass'));
- $UploadInst->setFolder(Config::get('static.container'));
- $errors = $UploadInst->getError();
- if(!empty($errors))
- {
- throw new Exception(implode(PHP_EOL, $errors));
- }
- return $UploadInst;
- }
- protected function loadFile($url) {
- $tmp = '/tmp/'.uniqid();
- ParserTools::getFileByUrl($url, $tmp);
- $Image= Image::make($tmp);
- if ($Image->width() > $Image->height())
- {
- Image::make($tmp)->resize(getConst('image_product_width'), null, function ($constraint) {
- $constraint->aspectRatio();
- })->save($tmp);
- } else {
- Image::make($tmp)->fit(getConst('image_product_width'), getConst('image_product_height'))->save($tmp);
- }
- return $tmp;
- }
- /**
- * Get the console command arguments.
- *
- * @return array
- */
- protected function getArguments()
- {
- return array(
- array('upload-type', InputArgument::REQUIRED, '[images].'),
- );
- }
- /**
- * Get the console command options.
- *
- * @return array
- */
- protected function getOptions()
- {
- return array(
- array('option', 'option', InputOption::VALUE_OPTIONAL, 'option upload', null),
- array('partner', 'p', InputOption::VALUE_OPTIONAL, 'option upload', null),
- );
- }
- }