/** * Upload photos to storage system. * * @param Illuminate\Http\Request $request * @return App\Product */ public function savePhotos(Request $request) { collect(['front', 'back', 'custom1', 'custom2'])->map(function ($name) use($request) { if (!$request->hasFile($name) || !$request->file($name)->isValid()) { return; } $content = file_get_contents($request->file($name)); $hash = md5($content); try { $photo = new ProductPhoto(); $photo->type = $name; $photo->product_id = $this->attributes['id']; $photo->location = $hash; Storage::disk()->put($hash, $content); $photo->save(); } catch (\Exception $e) { Log::error($e->getMessage()); } }); }
/** * Execute the job. * * @return void */ public function handle() { $dir = 'tmp/' . $this->_sessionId; if (!$this->exists($dir)) { Log::warning('No files uploaded for session ' . $this->_sessionId); return; } foreach (Storage::disk('local')->files($dir) as $file) { $type = pathinfo($file)['filename']; $content = file_get_contents(storage_path() . '/app/' . $file); $name = md5($content) . '.' . pathinfo($file, PATHINFO_EXTENSION); Storage::disk('s3')->put($name, $content); Storage::disk('local')->delete($file); $photo = new ProductPhoto(); $photo->type = $type; $photo->location = $name; $photo->product_id = $this->_productId; try { $photo->save(); } catch (Exception $e) { Log::error($e->getMessage()); } } }
public function getKivee() { set_time_limit(10000); $base_url = 'http://www.kiveeshop.com/'; $client = new Client(); $products = []; $template_replaced = '{{page}}'; $template_urls = [$base_url . '4-tops', $base_url . '14-outwears', $base_url . '15-dresses', $base_url . '13-bottoms']; foreach ($template_urls as $key => $template_url) { for ($i = 1; $i <= 5; $i++) { $url = str_replace($template_replaced, $i, $template_url); $header_url = get_headers($url, 1); if ($header_url[0] == 'HTTP/1.0 404 Not Found') { break; } $crawler = $client->request('GET', $url); $products_crawler = $crawler->filter('.ajax_block_product a'); foreach ($products_crawler as $key => $product_crawler) { $node = new Crawler($product_crawler); $url_page_link = $node->attr('href'); $page_crawler = $client->request('GET', $url_page_link); $name = $page_crawler->filter('#ContentPlaceHolderBody_ContentPlaceHolderBreadCrumb_lblNamaProduk')->text(); $price = $page_crawler->filter('#lblHargaAwal')->text(); $sale_price = $page_crawler->filter('#lblHarga')->text(); $desc = $page_crawler->filter('#cssmenu ul li div')->text(); $images = []; $images_obj = $page_crawler->filter('.slider-relative img'); foreach ($images_obj as $image_obj) { $images[] = $image_obj->getAttribute('src'); } // header(s'Content-Type: application/json'); $product = new Product(); $product->title = $name; $product->price = filter_var($price, FILTER_SANITIZE_NUMBER_INT); $product->description = preg_replace('/\\s+/', ' ', $desc); $product->sale_price = filter_var($sale_price, FILTER_SANITIZE_NUMBER_INT); $product->url = $url_page_link; $product->save(); foreach ($images as $key => $image) { $photo = new ProductPhoto(); $photo->photo_url = str_replace('./', $base_url, $image); $photo->thumbnail_url = str_replace('./', $base_url, $image); $photo->product_id = $product->id; $photo->save(); } $products[] = ['name' => $name, 'price' => $price, 'images' => $images]; } $next_page = $crawler->filter('.pagination_next.disabled'); if (sizeof($next_page) < 1) { break; } } } }