PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/app/commands/UploadFeedImages.php

https://bitbucket.org/grayrf/vishops
PHP | 283 lines | 215 code | 38 blank | 30 comment | 26 complexity | e6e4bfb2c101a5b4480f8122a3312f44 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. use Illuminate\Console\Command;
  3. use Symfony\Component\Console\Input\InputOption;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Parsers\Tools\ParserTools;
  6. class UploadFeedImages extends Command {
  7. /**
  8. * The console command name.
  9. *
  10. * @var string
  11. */
  12. protected $name = 'feeds:upload';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Upload feed files by url.';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function fire()
  34. {
  35. $type = $this->argument('upload-type');
  36. $option = $this->option('option');
  37. $partner = $this->option('partner');
  38. if ($type == 'images') {
  39. if ($option == 'delete')
  40. {
  41. if (empty($partner))
  42. {
  43. $this->deleteImages();
  44. } else {
  45. $this->deleteImagesByPartner($partner);
  46. }
  47. } else {
  48. $this->uploadImages($partner);
  49. }
  50. }
  51. }
  52. public function uploadImages($partner = null) {
  53. $UploadInst = $this->getUploadFileInst();
  54. $staticUrl = Config::get('static.public_url');
  55. $limit = 500;
  56. $countUploaded = 0;
  57. $filename = 'pictures/';
  58. do {
  59. $productsObj = \DB::table('products')
  60. ->select(array(\DB::raw('sql_calc_found_rows products.*, pi.id AS id_image, pi.url AS image_url')))
  61. ->join('products_images AS pi', function($join)
  62. {
  63. $join->on('pi.id_product', '=', 'products.id')
  64. ->where('pi.is_local', '!=', DB::raw(1))
  65. ->where('pi.primary', '=', DB::raw(1));
  66. });
  67. if (!empty($partner))
  68. {
  69. $productsObj->join('shops AS s', 's.id', '=', 'products.id_shop')
  70. ->where('s.slug', $partner);
  71. }
  72. $products = $productsObj->groupBy('products.id')
  73. ->havingRaw('COUNT(pi.id) > 0')
  74. ->limit($limit)
  75. ->get();
  76. if (empty($totalCount)) {
  77. $totalCount = (int) DB::select(DB::raw("SELECT FOUND_ROWS() AS count;"))[0]->count;
  78. }
  79. if (!empty($products)) {
  80. DB::beginTransaction();
  81. $rows = [];
  82. foreach($products AS $product)
  83. {
  84. if (empty($product->image_url))
  85. {
  86. continue;
  87. }
  88. $ext = pathinfo($product->image_url, PATHINFO_EXTENSION);
  89. $tmp = $this->loadFile($product->image_url);
  90. if (file_exists($tmp))
  91. {
  92. $slugName = slug($product->name.' '.$product->season,
  93. ['transliterate' => true, 'delimiter' => '_']);
  94. $newName = $filename.$product->id.'/'
  95. .base_convert($product->id_image, 10, 36)
  96. .'_'.$slugName.'.'.$ext;
  97. if ($UploadInst->uploadFile($tmp, $newName))
  98. {
  99. $rows[] = [
  100. 'id_product' => $product->id,
  101. 'url' => $staticUrl.'/'.$newName,
  102. 'is_local' => 1,
  103. 'primary' => 1
  104. ];
  105. DB::table('products_images')
  106. ->where('id', $product->id_image)
  107. ->update(['primary' => 0]);
  108. } else {
  109. $this->error('File '.$product->url.'
  110. of product '.$product->id.' hasn\'t uploaded to cloud');
  111. }
  112. unlink($tmp);
  113. }
  114. }
  115. DB::table('products_images')->insert($rows);
  116. DB::commit();
  117. $countUploaded += count($products);
  118. $this->info( $countUploaded .' images have uploaded');
  119. if ($countUploaded >= $totalCount) {
  120. break;
  121. }
  122. }
  123. } while(!empty($products));
  124. }
  125. public function deleteImages()
  126. {
  127. $UploadInst = $this->getUploadFileInst();
  128. $staticUrl = Config::get('static.public_url');
  129. $limit = 500;
  130. $countUploaded = 0;
  131. $totalCount = DB::table('old_files')->select('name','id')->count();
  132. do
  133. {
  134. $files = DB::table('old_files')->select('name','id')->limit($limit)->get();
  135. foreach($files AS $file)
  136. {
  137. if (preg_match('~^'.$staticUrl.'\/(.*)$~i', $file->name, $match))
  138. {
  139. $filename = trim($match[1]);
  140. if ($UploadInst->deleteFile($filename)) {
  141. DB::table('old_files')->where('id', $file->id)->delete();
  142. }
  143. }
  144. }
  145. $countUploaded += count($files);
  146. $this->info( $countUploaded .' items have deleted');
  147. if ($countUploaded > $totalCount) {
  148. break;
  149. }
  150. } while (!empty($files));
  151. }
  152. public function deleteImagesByPartner($partner)
  153. {
  154. $UploadInst = $this->getUploadFileInst();
  155. $staticUrl = Config::get('static.public_url');
  156. $limit = 500;
  157. $countUploaded = 0;
  158. $totalCount = DB::table('products_images AS pi')
  159. ->select('id')
  160. ->join('products AS p', 'p.id', '=', 'pi.id_product')
  161. ->join('shops AS s', 's.id', '=', 'p.id_shop')
  162. ->where('s.slug', $partner)
  163. ->where('pi.is_local', 1)
  164. ->count();
  165. do
  166. {
  167. $files = DB::table('products_images AS pi')
  168. ->select('pi.url', 'pi.id', 'p.id AS id_product')
  169. ->join('products AS p', 'p.id', '=', 'pi.id_product')
  170. ->join('shops AS s', 's.id', '=', 'p.id_shop')
  171. ->where('s.slug', $partner)
  172. ->where('pi.is_local', 1)
  173. ->limit($limit)
  174. ->get();
  175. foreach($files AS $file)
  176. {
  177. if (preg_match('~^'.$staticUrl.'\/(.*)$~i', $file->url, $match))
  178. {
  179. $filename = trim($match[1]);
  180. if ($UploadInst->deleteFile($filename)) {
  181. DB::table('products_images')->where('id', $file->id)->delete();
  182. $id_update = DB::table('products_images AS pi')->select('id')
  183. ->where('id_product', $file->id_product)->first()->id;
  184. DB::table('products_images')->where('id', $id_update)->update(['primary' => 1]);
  185. }
  186. }
  187. }
  188. $countUploaded += count($files);
  189. $this->info( $countUploaded .' items have deleted');
  190. if ($countUploaded > $totalCount) {
  191. break;
  192. }
  193. } while (!empty($files));
  194. }
  195. protected function getUploadFileInst()
  196. {
  197. $UploadInst = UploadFile::init( Config::get('static.host'),
  198. Config::get('static.login'),
  199. Config::get('static.pass'));
  200. $UploadInst->setFolder(Config::get('static.container'));
  201. $errors = $UploadInst->getError();
  202. if(!empty($errors))
  203. {
  204. throw new Exception(implode(PHP_EOL, $errors));
  205. }
  206. return $UploadInst;
  207. }
  208. protected function loadFile($url) {
  209. $tmp = '/tmp/'.uniqid();
  210. ParserTools::getFileByUrl($url, $tmp);
  211. $Image= Image::make($tmp);
  212. if ($Image->width() > $Image->height())
  213. {
  214. Image::make($tmp)->resize(getConst('image_product_width'), null, function ($constraint) {
  215. $constraint->aspectRatio();
  216. })->save($tmp);
  217. } else {
  218. Image::make($tmp)->fit(getConst('image_product_width'), getConst('image_product_height'))->save($tmp);
  219. }
  220. return $tmp;
  221. }
  222. /**
  223. * Get the console command arguments.
  224. *
  225. * @return array
  226. */
  227. protected function getArguments()
  228. {
  229. return array(
  230. array('upload-type', InputArgument::REQUIRED, '[images].'),
  231. );
  232. }
  233. /**
  234. * Get the console command options.
  235. *
  236. * @return array
  237. */
  238. protected function getOptions()
  239. {
  240. return array(
  241. array('option', 'option', InputOption::VALUE_OPTIONAL, 'option upload', null),
  242. array('partner', 'p', InputOption::VALUE_OPTIONAL, 'option upload', null),
  243. );
  244. }
  245. }